Skip to content
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

turf-boolean-contains + turf-boolean-within: Fix line in polygon #2848

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

samuelarbibe
Copy link

@samuelarbibe samuelarbibe commented Feb 27, 2025

This PR fixes a false-positive when running boolean-contains (or boolean-within) of lineString inside a polygon, as demonstrated in #2441.

The current implementation of boolean-contains checks if all the midpoints of the line segments are inside the polygon, and results in a false-positive in the following cases:

Line midpoint in Polygon Line midpoint in Polygon

This PR proposes the following logic:

  1. The line and polygon's bboxes must overlap. (optimization)
  2. All line points must be contained by the polygon.
  3. If the line intersects with polygon - all segments must be contained by the polygon. At least one of them should be fully contained by the polygon. (optimization - midpoint is sufficient)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant