Skip to content

Commit bf588dd

Browse files
Added special cases to booleanPointOnLine for zero length lines
booleanPointOnLine was returning false positives for points on the same longitude as zero length lines.
1 parent 9dc7a50 commit bf588dd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/turf-boolean-point-on-line/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ function isPointOnLineSegment(
9797
} else if (cross !== 0) {
9898
return false;
9999
}
100+
101+
// Special cases for zero length lines
102+
// https://github.com/Turfjs/turf/issues/2750
103+
if (Math.abs(dxl) === Math.abs(dyl) && Math.abs(dxl) === 0) {
104+
// Zero length line.
105+
if (excludeBoundary) {
106+
// To be on a zero length line pt has to be on the start (and end), BUT we
107+
// are excluding start and end from possible matches.
108+
return false;
109+
}
110+
if (pt[0] === lineSegmentStart[0] && pt[1] === lineSegmentStart[1]) {
111+
// If point is same as start (and end) it's on the line segment
112+
return true;
113+
} else {
114+
// Otherwise point is somewhere else
115+
return false;
116+
}
117+
}
118+
100119
if (!excludeBoundary) {
101120
if (Math.abs(dxl) >= Math.abs(dyl)) {
102121
return dxl > 0 ? x1 <= x && x <= x2 : x2 <= x && x <= x1;

packages/turf-boolean-point-on-line/test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import path from "path";
33
import { fileURLToPath } from "url";
44
import test from "tape";
55
import { loadJsonFileSync } from "load-json-file";
6-
import { booleanPointOnLine as pointOnLine } from "./index.js";
6+
import { point, lineString } from "@turf/helpers";
7+
import booleanPointOnLine, {
8+
booleanPointOnLine as pointOnLine,
9+
} from "./index.js";
710

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

@@ -36,3 +39,33 @@ test("turf-boolean-point-on-line", (t) => {
3639
});
3740
t.end();
3841
});
42+
43+
test("turf-boolean-point-on-line - issue 2750", (t) => {
44+
// Issue 2750 was that in the first test below where point is on a different
45+
// longitude to a zero length line booleanPointOnLine gave the correct result,
46+
// while the second test where a point on the SAME longitude, but nowhere
47+
// near, that zero length line incorrectly returned true.
48+
t.false(
49+
booleanPointOnLine(
50+
point([2, 13]),
51+
lineString([
52+
[1, 1],
53+
[1, 1],
54+
])
55+
),
56+
"#2750 different longitude point not on zero length line"
57+
);
58+
59+
t.false(
60+
booleanPointOnLine(
61+
point([1, 13]),
62+
lineString([
63+
[1, 1],
64+
[1, 1],
65+
])
66+
),
67+
"#2750 same longitude point not on zero length line"
68+
);
69+
70+
t.end();
71+
});

0 commit comments

Comments
 (0)