index.js 3.1 KB

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