-
Notifications
You must be signed in to change notification settings - Fork 991
turf-boolean-contains + turf-boolean-within: Fix line in polygon #2848
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
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 |
Janickvw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As commented above
|
Thank you.... got too focused on the complicated cases 😅. |
f9ac8eb to
0ebfa93
Compare
smallsaucepan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. Thanks @samuelarbibe 👍
|
Thanks for your work on this too @Janickvw ⭐ |

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