index.js 1.4 KB

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