index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // index.ts
  2. import { booleanWithin as within } from "@turf/boolean-within";
  3. import { distance } from "@turf/distance";
  4. import { point, featureCollection } from "@turf/helpers";
  5. function pointGrid(bbox, cellSide, options = {}) {
  6. if (options.mask && !options.units) options.units = "kilometers";
  7. var results = [];
  8. var west = bbox[0];
  9. var south = bbox[1];
  10. var east = bbox[2];
  11. var north = bbox[3];
  12. var xFraction = cellSide / distance([west, south], [east, south], options);
  13. var cellWidth = xFraction * (east - west);
  14. var yFraction = cellSide / distance([west, south], [west, north], options);
  15. var cellHeight = yFraction * (north - south);
  16. var bboxWidth = east - west;
  17. var bboxHeight = north - south;
  18. var columns = Math.floor(bboxWidth / cellWidth);
  19. var rows = Math.floor(bboxHeight / cellHeight);
  20. var deltaX = (bboxWidth - columns * cellWidth) / 2;
  21. var deltaY = (bboxHeight - rows * cellHeight) / 2;
  22. var currentX = west + deltaX;
  23. while (currentX <= east) {
  24. var currentY = south + deltaY;
  25. while (currentY <= north) {
  26. var cellPt = point([currentX, currentY], options.properties);
  27. if (options.mask) {
  28. if (within(cellPt, options.mask)) results.push(cellPt);
  29. } else {
  30. results.push(cellPt);
  31. }
  32. currentY += cellHeight;
  33. }
  34. currentX += cellWidth;
  35. }
  36. return featureCollection(results);
  37. }
  38. var turf_point_grid_default = pointGrid;
  39. export {
  40. turf_point_grid_default as default,
  41. pointGrid
  42. };
  43. //# sourceMappingURL=index.js.map