index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { polygon } from "@turf/helpers";
  2. /**
  3. * Takes a bbox and returns an equivalent {@link Polygon|polygon}.
  4. *
  5. * @name bboxPolygon
  6. * @param {BBox} bbox extent in [minX, minY, maxX, maxY] order
  7. * @param {Object} [options={}] Optional parameters
  8. * @param {Properties} [options.properties={}] Translate properties to Polygon
  9. * @param {string|number} [options.id={}] Translate Id to Polygon
  10. * @returns {Feature<Polygon>} a Polygon representation of the bounding box
  11. * @example
  12. * var bbox = [0, 0, 10, 10];
  13. *
  14. * var poly = turf.bboxPolygon(bbox);
  15. *
  16. * //addToMap
  17. * var addToMap = [poly]
  18. */
  19. export default function bboxPolygon(bbox, options) {
  20. if (options === void 0) { options = {}; }
  21. // Convert BBox positions to Numbers
  22. // No performance loss for including Number()
  23. // https://github.com/Turfjs/turf/issues/1119
  24. var west = Number(bbox[0]);
  25. var south = Number(bbox[1]);
  26. var east = Number(bbox[2]);
  27. var north = Number(bbox[3]);
  28. if (bbox.length === 6) {
  29. throw new Error("@turf/bbox-polygon does not support BBox with 6 positions");
  30. }
  31. var lowLeft = [west, south];
  32. var topLeft = [west, north];
  33. var topRight = [east, north];
  34. var lowRight = [east, south];
  35. return polygon([[lowLeft, lowRight, topRight, topLeft, lowLeft]], options.properties, { bbox: bbox, id: options.id });
  36. }