turf-boolean-contains + turf-boolean-within: Fix line in polygon#2848
turf-boolean-contains + turf-boolean-within: Fix line in polygon#2848smallsaucepan merged 10 commits intoTurfjs:masterfrom
Conversation
|
Hey @samuelarbibe i've found an issue with this fix. I'll test out a fix and get back to you. |
I'm curious. Let me know :) |
|
@samuelarbibe I think you focused too much on advanced use cases and forgot the simple ones 😂 Sandbox link: https://turf-sandbox.netlify.app/?gist=6ce3e4e6d674e6dd9c92c5265b6a25e0 The issue is this line not returning any segments when there are no intersections. const lineSegments = turf.lineSplit(turf.feature(linestring), turf.feature(polygon));Here is a potential fix: // ...
const lineSegmentsOrLineString = lineSegments.features.length > 0 ? lineSegments.features : [turf.feature(linestring)];
for (const lineSegment of lineSegmentsOrLineString) {
const midpoint = turf.midpoint(
lineSegment.geometry.coordinates[0],
lineSegment.geometry.coordinates[1]
);
// ...Fix sandbox: https://turf-sandbox.netlify.app/?gist=57378d936e774ac353de86e975060bf9 |
|
Thank you.... got too focused on the complicated cases 😅. |
f9ac8eb to
0ebfa93
Compare
smallsaucepan
left a comment
There was a problem hiding this comment.
Looks really good thanks @samuelarbibe. Have asked for a few short code comments here and there, and some rewording of the documentation (will put in the PR conversation).
|
Would you mind doing some rewording of the documentation while you're here? The "exact opposite result" phrasing isn't very satisfying (and I'm not sure it's true anyway if taken literally). From
To
Happy to hear it if you have other suggestions as well! |
smallsaucepan
left a comment
There was a problem hiding this comment.
Awesome. Thanks @samuelarbibe 👍
|
Thanks for your work on this too @Janickvw ⭐ |
Sorry to be a pain, since I know this is already merged. But i've read this comment a few times and still don't understand it. Seems redundant to split a line segment that completely overlaps a boundary? Do you have a graphic or picture you could use to demonstrate? |
Ah ok interesting. Thanks for the reply @samuelarbibe! |
|
Thanks for doing that followup @Janickvw and @samuelarbibe |





This PR fixes a false-positive when running
boolean-contains(orboolean-within) oflineStringinside apolygon, as demonstrated in #2441.The current implementation of
boolean-containschecks if all the midpoints of the line segments are inside the polygon, and results in a false-positive in the following cases:This PR proposes the following logic:
This "waterfall" approach makes sure no extra computation is performed - unless necessary. Only quirky cases will require the extra computation of step 3 (In most cases, a single iteration of step 3 will suffice).
The only exception is that the line's points are checked to be contained instead of the midpoints - but it returns false-positives for pretty basic cases, so I find this to be a reasonable compromise.
A test case for the second image has been added.
Resolves #2441
Resolves #2049
Resolves #1443