Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update boolean-point-in-polygon to use point-in-polygon-hao library #1893

Merged
merged 8 commits into from Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 11 additions & 53 deletions packages/turf-boolean-point-in-polygon/index.ts
@@ -1,3 +1,4 @@
import pip from 'point-in-polygon-hao'
import { BBox, Coord, Feature, MultiPolygon, Polygon, Properties } from "@turf/helpers";
import { getCoord, getCoords, getGeom } from "@turf/invariant";

Expand Down Expand Up @@ -49,63 +50,20 @@ export default function booleanPointInPolygon<G extends Polygon | MultiPolygon,
if (bbox && inBBox(pt, bbox) === false) {
return false;
}
// normalize to multipolygon
if (type === "Polygon") {
polys = [polys];
}
let insidePoly = false;
for (let i = 0; i < polys.length && !insidePoly; i++) {
// check if it is in the outer ring first
if (inRing(pt, polys[i][0], options.ignoreBoundary)) {
let inHole = false;
let k = 1;
// check for the point in any of the holes
while (k < polys[i].length && !inHole) {
if (inRing(pt, polys[i][k], !options.ignoreBoundary)) {
inHole = true;
}
k++;
}
if (!inHole) {
insidePoly = true;
}
}
}
return insidePoly;
}

/**
* inRing
*
* @private
* @param {Array<number>} pt [x,y]
* @param {Array<Array<number>>} ring [[x,y], [x,y],..]
* @param {boolean} ignoreBoundary ignoreBoundary
* @returns {boolean} inRing
*/
function inRing(pt: number[], ring: number[][], ignoreBoundary?: boolean) {
let isInside = false;
if (ring[0][0] === ring[ring.length - 1][0] && ring[0][1] === ring[ring.length - 1][1]) {
ring = ring.slice(0, ring.length - 1);
if (type === 'Polygon') {
polys = [polys]
}
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
const xi = ring[i][0];
const yi = ring[i][1];
const xj = ring[j][0];
const yj = ring[j][1];
const onBoundary = (pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0) &&
((xi - pt[0]) * (xj - pt[0]) <= 0) && ((yi - pt[1]) * (yj - pt[1]) <= 0);
if (onBoundary) {
return !ignoreBoundary;
}
const intersect = ((yi > pt[1]) !== (yj > pt[1])) &&
(pt[0] < (xj - xi) * (pt[1] - yi) / (yj - yi) + xi);
if (intersect) {
isInside = !isInside;
}
let result = false
for (var i = 0; i < polys.length; ++i) {
const polyResult = pip(pt, polys[i])
if (polyResult === 0) return options.ignoreBoundary ? false : true
else if (polyResult) result = true
}
return isInside;

return result
}

/**
* inBBox
*
Expand Down
3 changes: 2 additions & 1 deletion packages/turf-boolean-point-in-polygon/package.json
Expand Up @@ -50,6 +50,7 @@
},
"dependencies": {
"@turf/helpers": "^6.2.0-alpha.1",
"@turf/invariant": "^6.2.0-alpha.1"
"@turf/invariant": "^6.2.0-alpha.1",
"point-in-polygon-hao": "0.0.7"
}
}