index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. var distance = require('@turf/distance');
  3. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  4. var distance__default = /*#__PURE__*/_interopDefaultLegacy(distance);
  5. /**
  6. * Takes a bounding box and calculates the minimum square bounding box that
  7. * would contain the input.
  8. *
  9. * @name square
  10. * @param {BBox} bbox extent in [west, south, east, north] order
  11. * @returns {BBox} a square surrounding `bbox`
  12. * @example
  13. * var bbox = [-20, -20, -15, 0];
  14. * var squared = turf.square(bbox);
  15. *
  16. * //addToMap
  17. * var addToMap = [turf.bboxPolygon(bbox), turf.bboxPolygon(squared)]
  18. */
  19. function square(bbox) {
  20. var west = bbox[0];
  21. var south = bbox[1];
  22. var east = bbox[2];
  23. var north = bbox[3];
  24. var horizontalDistance = distance__default['default'](bbox.slice(0, 2), [east, south]);
  25. var verticalDistance = distance__default['default'](bbox.slice(0, 2), [west, north]);
  26. if (horizontalDistance >= verticalDistance) {
  27. var verticalMidpoint = (south + north) / 2;
  28. return [
  29. west,
  30. verticalMidpoint - (east - west) / 2,
  31. east,
  32. verticalMidpoint + (east - west) / 2,
  33. ];
  34. } else {
  35. var horizontalMidpoint = (west + east) / 2;
  36. return [
  37. horizontalMidpoint - (north - south) / 2,
  38. south,
  39. horizontalMidpoint + (north - south) / 2,
  40. north,
  41. ];
  42. }
  43. }
  44. module.exports = square;
  45. module.exports.default = square;