Skip to content

Commit

Permalink
Added special cases for zero length lines. booleanPointOnLine was ret…
Browse files Browse the repository at this point in the history
…urning false positives for points on the same longitude as zero length lines.
  • Loading branch information
smallsaucepan committed Nov 20, 2024
1 parent 5d1e0ec commit 1dfb4c5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/turf-boolean-point-on-line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ function isPointOnLineSegment(
} else if (cross !== 0) {
return false;
}

// Special cases for zero length lines
// https://github.com/Turfjs/turf/issues/2750
if (Math.abs(dxl) === Math.abs(dyl) && Math.abs(dxl) === 0) {
// Zero length line.
if (excludeBoundary) {
// To be on a zero length line pt has to be on the start (and end), BUT we
// are excluding start and end from possible matches.
return false;
}
if (pt[0] === lineSegmentStart[0] && pt[1] === lineSegmentStart[1]) {
// If point is same as start (and end) it's on the line segment
return true;
} else {
// Otherwise point is somewhere else
return false;
}
}

if (!excludeBoundary) {
if (Math.abs(dxl) >= Math.abs(dyl)) {
return dxl > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1;
Expand Down
35 changes: 34 additions & 1 deletion packages/turf-boolean-point-on-line/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import path from "path";
import { fileURLToPath } from "url";
import test from "tape";
import { loadJsonFileSync } from "load-json-file";
import { booleanPointOnLine as pointOnLine } from "./index.js";
import { point, lineString } from "@turf/helpers";
import booleanPointOnLine, {
booleanPointOnLine as pointOnLine,
} from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -36,3 +39,33 @@ test("turf-boolean-point-on-line", (t) => {
});
t.end();
});

test("turf-boolean-point-on-line - issue 2750", (t) => {
// Issue 2750 was that in the first test below where point is on a different
// longitude to a zero length line booleanPointOnLine gave the correct result,
// while the second test where a point on the SAME longitude, but nowhere
// near, that zero length line incorrectly returned true.
t.false(
booleanPointOnLine(
point([2, 13]),
lineString([
[1, 1],
[1, 1],
])
),
"#2750 different longitude point not on zero length line"
);

t.false(
booleanPointOnLine(
point([1, 13]),
lineString([
[1, 1],
[1, 1],
])
),
"#2750 same longitude point not on zero length line"
);

t.end();
});

0 comments on commit 1dfb4c5

Please sign in to comment.