index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // index.ts
  2. import { booleanPointInPolygon as pointInPolygon } from "@turf/boolean-point-in-polygon";
  3. import { featureCollection, multiPoint } from "@turf/helpers";
  4. import { geomEach, featureEach, coordEach } from "@turf/meta";
  5. function pointsWithinPolygon(points, polygons) {
  6. const results = [];
  7. featureEach(points, function(point) {
  8. let contained = false;
  9. if (point.geometry.type === "Point") {
  10. geomEach(polygons, function(polygon) {
  11. if (pointInPolygon(point, polygon)) {
  12. contained = true;
  13. }
  14. });
  15. if (contained) {
  16. results.push(point);
  17. }
  18. } else if (point.geometry.type === "MultiPoint") {
  19. var pointsWithin = [];
  20. geomEach(polygons, function(polygon) {
  21. coordEach(point, function(pointCoord) {
  22. if (pointInPolygon(pointCoord, polygon)) {
  23. contained = true;
  24. pointsWithin.push(pointCoord);
  25. }
  26. });
  27. });
  28. if (contained) {
  29. results.push(
  30. multiPoint(pointsWithin, point.properties)
  31. );
  32. }
  33. } else {
  34. throw new Error("Input geometry must be a Point or MultiPoint");
  35. }
  36. });
  37. return featureCollection(results);
  38. }
  39. var turf_points_within_polygon_default = pointsWithinPolygon;
  40. export {
  41. turf_points_within_polygon_default as default,
  42. pointsWithinPolygon
  43. };
  44. //# sourceMappingURL=index.js.map