index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // index.ts
  2. import pip from "point-in-polygon-hao";
  3. import { getCoord, getGeom } from "@turf/invariant";
  4. function booleanPointInPolygon(point, polygon, options = {}) {
  5. if (!point) {
  6. throw new Error("point is required");
  7. }
  8. if (!polygon) {
  9. throw new Error("polygon is required");
  10. }
  11. const pt = getCoord(point);
  12. const geom = getGeom(polygon);
  13. const type = geom.type;
  14. const bbox = polygon.bbox;
  15. let polys = geom.coordinates;
  16. if (bbox && inBBox(pt, bbox) === false) {
  17. return false;
  18. }
  19. if (type === "Polygon") {
  20. polys = [polys];
  21. }
  22. let result = false;
  23. for (var i = 0; i < polys.length; ++i) {
  24. const polyResult = pip(pt, polys[i]);
  25. if (polyResult === 0) return options.ignoreBoundary ? false : true;
  26. else if (polyResult) result = true;
  27. }
  28. return result;
  29. }
  30. function inBBox(pt, bbox) {
  31. return bbox[0] <= pt[0] && bbox[1] <= pt[1] && bbox[2] >= pt[0] && bbox[3] >= pt[1];
  32. }
  33. var turf_boolean_point_in_polygon_default = booleanPointInPolygon;
  34. export {
  35. booleanPointInPolygon,
  36. turf_boolean_point_in_polygon_default as default
  37. };
  38. //# sourceMappingURL=index.js.map