index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import within from "@turf/boolean-within";
  2. import distance from "@turf/distance";
  3. import { point, featureCollection, } from "@turf/helpers";
  4. /**
  5. * Creates a {@link Point} grid from a bounding box, {@link FeatureCollection} or {@link Feature}.
  6. *
  7. * @name pointGrid
  8. * @param {Array<number>} bbox extent in [minX, minY, maxX, maxY] order
  9. * @param {number} cellSide the distance between points, in units
  10. * @param {Object} [options={}] Optional parameters
  11. * @param {string} [options.units='kilometers'] used in calculating cellSide, can be degrees, radians, miles, or kilometers
  12. * @param {Feature<Polygon|MultiPolygon>} [options.mask] if passed a Polygon or MultiPolygon, the grid Points will be created only inside it
  13. * @param {Object} [options.properties={}] passed to each point of the grid
  14. * @returns {FeatureCollection<Point>} grid of points
  15. * @example
  16. * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
  17. * var cellSide = 3;
  18. * var options = {units: 'miles'};
  19. *
  20. * var grid = turf.pointGrid(extent, cellSide, options);
  21. *
  22. * //addToMap
  23. * var addToMap = [grid];
  24. */
  25. function pointGrid(bbox, cellSide, options) {
  26. if (options === void 0) { options = {}; }
  27. // Default parameters
  28. if (options.mask && !options.units)
  29. options.units = "kilometers";
  30. // Containers
  31. var results = [];
  32. // Typescript handles the Type Validation
  33. // if (cellSide === null || cellSide === undefined) throw new Error('cellSide is required');
  34. // if (!isNumber(cellSide)) throw new Error('cellSide is invalid');
  35. // if (!bbox) throw new Error('bbox is required');
  36. // if (!Array.isArray(bbox)) throw new Error('bbox must be array');
  37. // if (bbox.length !== 4) throw new Error('bbox must contain 4 numbers');
  38. // if (mask && ['Polygon', 'MultiPolygon'].indexOf(getType(mask)) === -1) throw new Error('options.mask must be a (Multi)Polygon');
  39. var west = bbox[0];
  40. var south = bbox[1];
  41. var east = bbox[2];
  42. var north = bbox[3];
  43. var xFraction = cellSide / distance([west, south], [east, south], options);
  44. var cellWidth = xFraction * (east - west);
  45. var yFraction = cellSide / distance([west, south], [west, north], options);
  46. var cellHeight = yFraction * (north - south);
  47. var bboxWidth = east - west;
  48. var bboxHeight = north - south;
  49. var columns = Math.floor(bboxWidth / cellWidth);
  50. var rows = Math.floor(bboxHeight / cellHeight);
  51. // adjust origin of the grid
  52. var deltaX = (bboxWidth - columns * cellWidth) / 2;
  53. var deltaY = (bboxHeight - rows * cellHeight) / 2;
  54. var currentX = west + deltaX;
  55. while (currentX <= east) {
  56. var currentY = south + deltaY;
  57. while (currentY <= north) {
  58. var cellPt = point([currentX, currentY], options.properties);
  59. if (options.mask) {
  60. if (within(cellPt, options.mask))
  61. results.push(cellPt);
  62. }
  63. else {
  64. results.push(cellPt);
  65. }
  66. currentY += cellHeight;
  67. }
  68. currentX += cellWidth;
  69. }
  70. return featureCollection(results);
  71. }
  72. export default pointGrid;