index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // index.ts
  2. import { booleanIntersects as intersect } from "@turf/boolean-intersects";
  3. import {
  4. convertLength,
  5. featureCollection,
  6. polygon
  7. } from "@turf/helpers";
  8. function rectangleGrid(bbox, cellWidth, cellHeight, options = {}) {
  9. const results = [];
  10. const west = bbox[0];
  11. const south = bbox[1];
  12. const east = bbox[2];
  13. const north = bbox[3];
  14. const bboxWidth = east - west;
  15. const cellWidthDeg = convertLength(cellWidth, options.units, "degrees");
  16. const bboxHeight = north - south;
  17. const cellHeightDeg = convertLength(cellHeight, options.units, "degrees");
  18. const columns = Math.floor(Math.abs(bboxWidth) / cellWidthDeg);
  19. const rows = Math.floor(Math.abs(bboxHeight) / cellHeightDeg);
  20. const deltaX = (bboxWidth - columns * cellWidthDeg) / 2;
  21. const deltaY = (bboxHeight - rows * cellHeightDeg) / 2;
  22. let currentX = west + deltaX;
  23. for (let column = 0; column < columns; column++) {
  24. let currentY = south + deltaY;
  25. for (let row = 0; row < rows; row++) {
  26. const cellPoly = polygon(
  27. [
  28. [
  29. [currentX, currentY],
  30. [currentX, currentY + cellHeightDeg],
  31. [currentX + cellWidthDeg, currentY + cellHeightDeg],
  32. [currentX + cellWidthDeg, currentY],
  33. [currentX, currentY]
  34. ]
  35. ],
  36. options.properties
  37. );
  38. if (options.mask) {
  39. if (intersect(options.mask, cellPoly)) {
  40. results.push(cellPoly);
  41. }
  42. } else {
  43. results.push(cellPoly);
  44. }
  45. currentY += cellHeightDeg;
  46. }
  47. currentX += cellWidthDeg;
  48. }
  49. return featureCollection(results);
  50. }
  51. var turf_rectangle_grid_default = rectangleGrid;
  52. export {
  53. turf_rectangle_grid_default as default,
  54. rectangleGrid
  55. };
  56. //# sourceMappingURL=index.js.map