index.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 bbox_1 = __importDefault(require("@turf/bbox"));
  7. var boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
  8. var rbush_1 = __importDefault(require("rbush"));
  9. /**
  10. * Merges a specified property from a FeatureCollection of points into a
  11. * FeatureCollection of polygons. Given an `inProperty` on points and an `outProperty`
  12. * for polygons, this finds every point that lies within each polygon, collects the
  13. * `inProperty` values from those points, and adds them as an array to `outProperty`
  14. * on the polygon.
  15. *
  16. * @name collect
  17. * @param {FeatureCollection<Polygon>} polygons polygons with values on which to aggregate
  18. * @param {FeatureCollection<Point>} points points to be aggregated
  19. * @param {string} inProperty property to be nested from
  20. * @param {string} outProperty property to be nested into
  21. * @returns {FeatureCollection<Polygon>} polygons with properties listed based on `outField`
  22. * @example
  23. * var poly1 = turf.polygon([[[0,0],[10,0],[10,10],[0,10],[0,0]]]);
  24. * var poly2 = turf.polygon([[[10,0],[20,10],[20,20],[20,0],[10,0]]]);
  25. * var polyFC = turf.featureCollection([poly1, poly2]);
  26. * var pt1 = turf.point([5,5], {population: 200});
  27. * var pt2 = turf.point([1,3], {population: 600});
  28. * var pt3 = turf.point([14,2], {population: 100});
  29. * var pt4 = turf.point([13,1], {population: 200});
  30. * var pt5 = turf.point([19,7], {population: 300});
  31. * var pointFC = turf.featureCollection([pt1, pt2, pt3, pt4, pt5]);
  32. * var collected = turf.collect(polyFC, pointFC, 'population', 'values');
  33. * var values = collected.features[0].properties.values
  34. * //=values => [200, 600]
  35. *
  36. * //addToMap
  37. * var addToMap = [pointFC, collected]
  38. */
  39. function collect(polygons, points, inProperty, outProperty) {
  40. var rtree = rbush_1.default(6);
  41. var treeItems = points.features.map(function (item) {
  42. var _a;
  43. return {
  44. minX: item.geometry.coordinates[0],
  45. minY: item.geometry.coordinates[1],
  46. maxX: item.geometry.coordinates[0],
  47. maxY: item.geometry.coordinates[1],
  48. property: (_a = item.properties) === null || _a === void 0 ? void 0 : _a[inProperty],
  49. };
  50. });
  51. rtree.load(treeItems);
  52. polygons.features.forEach(function (poly) {
  53. if (!poly.properties) {
  54. poly.properties = {};
  55. }
  56. var bbox = bbox_1.default(poly);
  57. var potentialPoints = rtree.search({
  58. minX: bbox[0],
  59. minY: bbox[1],
  60. maxX: bbox[2],
  61. maxY: bbox[3],
  62. });
  63. var values = [];
  64. potentialPoints.forEach(function (pt) {
  65. if (boolean_point_in_polygon_1.default([pt.minX, pt.minY], poly)) {
  66. values.push(pt.property);
  67. }
  68. });
  69. poly.properties[outProperty] = values;
  70. });
  71. return polygons;
  72. }
  73. exports.default = collect;