index.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var boolean_intersects_1 = __importDefault(require("@turf/boolean-intersects"));
  7. var distance_1 = __importDefault(require("@turf/distance"));
  8. var helpers_1 = require("@turf/helpers");
  9. /**
  10. * Creates a grid of rectangles from a bounding box, {@link Feature} or {@link FeatureCollection}.
  11. *
  12. * @name rectangleGrid
  13. * @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
  14. * @param {number} cellWidth of each cell, in units
  15. * @param {number} cellHeight of each cell, in units
  16. * @param {Object} [options={}] Optional parameters
  17. * @param {string} [options.units='kilometers'] units ("degrees", "radians", "miles", "kilometers") that the given cellWidth
  18. * and cellHeight are expressed in. Converted at the southern border.
  19. * @param {Feature<Polygon|MultiPolygon>} [options.mask] if passed a Polygon or MultiPolygon,
  20. * the grid Points will be created only inside it
  21. * @param {Object} [options.properties={}] passed to each point of the grid
  22. * @returns {FeatureCollection<Polygon>} a grid of polygons
  23. * @example
  24. * var bbox = [-95, 30 ,-85, 40];
  25. * var cellWidth = 50;
  26. * var cellHeight = 20;
  27. * var options = {units: 'miles'};
  28. *
  29. * var rectangleGrid = turf.rectangleGrid(bbox, cellWidth, cellHeight, options);
  30. *
  31. * //addToMap
  32. * var addToMap = [rectangleGrid]
  33. */
  34. function rectangleGrid(bbox, cellWidth, cellHeight, options) {
  35. if (options === void 0) { options = {}; }
  36. // Containers
  37. var results = [];
  38. var west = bbox[0];
  39. var south = bbox[1];
  40. var east = bbox[2];
  41. var north = bbox[3];
  42. var xFraction = cellWidth / distance_1.default([west, south], [east, south], options);
  43. var cellWidthDeg = xFraction * (east - west);
  44. var yFraction = cellHeight / distance_1.default([west, south], [west, north], options);
  45. var cellHeightDeg = yFraction * (north - south);
  46. // rows & columns
  47. var bboxWidth = east - west;
  48. var bboxHeight = north - south;
  49. var columns = Math.floor(bboxWidth / cellWidthDeg);
  50. var rows = Math.floor(bboxHeight / cellHeightDeg);
  51. // if the grid does not fill the bbox perfectly, center it.
  52. var deltaX = (bboxWidth - columns * cellWidthDeg) / 2;
  53. var deltaY = (bboxHeight - rows * cellHeightDeg) / 2;
  54. // iterate over columns & rows
  55. var currentX = west + deltaX;
  56. for (var column = 0; column < columns; column++) {
  57. var currentY = south + deltaY;
  58. for (var row = 0; row < rows; row++) {
  59. var cellPoly = helpers_1.polygon([
  60. [
  61. [currentX, currentY],
  62. [currentX, currentY + cellHeightDeg],
  63. [currentX + cellWidthDeg, currentY + cellHeightDeg],
  64. [currentX + cellWidthDeg, currentY],
  65. [currentX, currentY],
  66. ],
  67. ], options.properties);
  68. if (options.mask) {
  69. if (boolean_intersects_1.default(options.mask, cellPoly)) {
  70. results.push(cellPoly);
  71. }
  72. }
  73. else {
  74. results.push(cellPoly);
  75. }
  76. currentY += cellHeightDeg;
  77. }
  78. currentX += cellWidthDeg;
  79. }
  80. return helpers_1.featureCollection(results);
  81. }
  82. exports.default = rectangleGrid;