index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. var pointInPolygon = require('@turf/boolean-point-in-polygon');
  3. var helpers = require('@turf/helpers');
  4. var meta = require('@turf/meta');
  5. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  6. var pointInPolygon__default = /*#__PURE__*/_interopDefaultLegacy(pointInPolygon);
  7. /**
  8. * Finds {@link Points} or {@link MultiPoint} coordinate positions that fall within {@link (Multi)Polygon(s)}.
  9. *
  10. * @name pointsWithinPolygon
  11. * @param {Feature|FeatureCollection<Point|MultiPoint>} points Point(s) or MultiPoint(s) as input search
  12. * @param {FeatureCollection|Geometry|Feature<Polygon|MultiPolygon>} polygons (Multi)Polygon(s) to check if points are within
  13. * @returns {FeatureCollection<Point|MultiPoint>} Point(s) or MultiPoint(s) with positions that land within at least one polygon. The geometry type will match what was passsed in
  14. * @example
  15. * var points = turf.points([
  16. * [-46.6318, -23.5523],
  17. * [-46.6246, -23.5325],
  18. * [-46.6062, -23.5513],
  19. * [-46.663, -23.554],
  20. * [-46.643, -23.557]
  21. * ]);
  22. *
  23. * var searchWithin = turf.polygon([[
  24. * [-46.653,-23.543],
  25. * [-46.634,-23.5346],
  26. * [-46.613,-23.543],
  27. * [-46.614,-23.559],
  28. * [-46.631,-23.567],
  29. * [-46.653,-23.560],
  30. * [-46.653,-23.543]
  31. * ]]);
  32. *
  33. * var ptsWithin = turf.pointsWithinPolygon(points, searchWithin);
  34. *
  35. * //addToMap
  36. * var addToMap = [points, searchWithin, ptsWithin]
  37. * turf.featureEach(ptsWithin, function (currentFeature) {
  38. * currentFeature.properties['marker-size'] = 'large';
  39. * currentFeature.properties['marker-color'] = '#000';
  40. * });
  41. */
  42. function pointsWithinPolygon(points, polygons) {
  43. var results = [];
  44. meta.featureEach(points, function (point) {
  45. var contained = false;
  46. if (point.geometry.type === "Point") {
  47. meta.geomEach(polygons, function (polygon) {
  48. if (pointInPolygon__default['default'](point, polygon)) contained = true;
  49. });
  50. if (contained) {
  51. results.push(point);
  52. }
  53. } else if (point.geometry.type === "MultiPoint") {
  54. var pointsWithin = [];
  55. meta.geomEach(polygons, function (polygon) {
  56. meta.coordEach(point, function (pointCoord) {
  57. if (pointInPolygon__default['default'](pointCoord, polygon)) {
  58. contained = true;
  59. pointsWithin.push(pointCoord);
  60. }
  61. });
  62. });
  63. if (contained) {
  64. results.push(helpers.multiPoint(pointsWithin));
  65. }
  66. } else {
  67. throw new Error("Input geometry must be a Point or MultiPoint");
  68. }
  69. });
  70. return helpers.featureCollection(results);
  71. }
  72. module.exports = pointsWithinPolygon;
  73. module.exports.default = pointsWithinPolygon;