index.js 1007 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { coordEach } from "@turf/meta";
  2. /**
  3. * Takes a set of features, calculates the bbox of all input features, and returns a bounding box.
  4. *
  5. * @name bbox
  6. * @param {GeoJSON} geojson any GeoJSON object
  7. * @returns {BBox} bbox extent in [minX, minY, maxX, maxY] order
  8. * @example
  9. * var line = turf.lineString([[-74, 40], [-78, 42], [-82, 35]]);
  10. * var bbox = turf.bbox(line);
  11. * var bboxPolygon = turf.bboxPolygon(bbox);
  12. *
  13. * //addToMap
  14. * var addToMap = [line, bboxPolygon]
  15. */
  16. function bbox(geojson) {
  17. var result = [Infinity, Infinity, -Infinity, -Infinity];
  18. coordEach(geojson, function (coord) {
  19. if (result[0] > coord[0]) {
  20. result[0] = coord[0];
  21. }
  22. if (result[1] > coord[1]) {
  23. result[1] = coord[1];
  24. }
  25. if (result[2] < coord[0]) {
  26. result[2] = coord[0];
  27. }
  28. if (result[3] < coord[1]) {
  29. result[3] = coord[1];
  30. }
  31. });
  32. return result;
  33. }
  34. bbox["default"] = bbox;
  35. export default bbox;