Skip to content

Commit

Permalink
Fix edge case for booleanPointInPolygon when point is on border of a …
Browse files Browse the repository at this point in the history
…polygon and inside another one
  • Loading branch information
Pitouli committed Jan 18, 2025
1 parent 2049430 commit 69d6d71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/turf-boolean-point-in-polygon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ function booleanPointInPolygon<

for (var i = 0; i < polys.length; ++i) {
const polyResult = pip(pt, polys[i]);
if (polyResult === 0) return options.ignoreBoundary ? false : true;
// If being on the boundary doesn't count, stay in the loop to see if we're inside another polygon
// The RFC does not prevent polygons from overlapping; https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.7
if (polyResult === 0 && !options.ignoreBoundary) return true;
// If point is in the polygon, exit early the loop
else if (polyResult) return true;
}

Expand Down
23 changes: 22 additions & 1 deletion packages/turf-boolean-point-in-polygon/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import test from "tape";
import { point } from "@turf/helpers";
import { multiPolygon, point } from "@turf/helpers";
import { polygon } from "@turf/helpers";
import { booleanPointInPolygon } from "./index.js";

Expand Down Expand Up @@ -143,6 +143,26 @@ test("boolean-point-in-polygon -- Boundary test", function (t) {
[10, 20],
],
]);
var poly6 = multiPolygon([
[
[
[10, 20],
[20, 10],
[30, 20],
[20, 30],
[10, 20],
],
],
[
[
[0, 20],
[20, 0],
[40, 20],
[20, 40],
[0, 20],
],
],
]);
function runTest(t, ignoreBoundary) {
var isBoundaryIncluded = ignoreBoundary === false;
var tests = [
Expand Down Expand Up @@ -183,6 +203,7 @@ test("boolean-point-in-polygon -- Boundary test", function (t) {
[poly5, point([10, 20]), isBoundaryIncluded],
[poly5, point([15, 25]), isBoundaryIncluded],
[poly5, point([20, 20]), false],
[poly6, point([25, 25]), true], // Point on the boundary of the first polygon, but inside the second polygon
];

var testTitle =
Expand Down

0 comments on commit 69d6d71

Please sign in to comment.