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

booleanPointInPolygon - Perf improvement + fix edge case #2821

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

Conversation

Pitouli
Copy link
Contributor

@Pitouli Pitouli commented Jan 18, 2025

Perf improvement

No breaking changes; it simply return early once it confirmed that the point is inside at least one polygon, instead of checking all the other polygons.

Fix edge case

If the point was on the edge of a polygon, the function was returning immediately "true" if the option ignoreBoundary was false, and false otherwise.

But the RFC does not say that polygons inside a multipolygon cannot overlap, it only says a multipolygon is an array of polygons.
https://datatracker.ietf.org/doc/html/rfc7946#section-3.1.7

So in case the point is on the border of one of the polygon and ignoreBoundary is true, the loop should continue to see if the point is inside an another polygon.

Note that some libraries seems to consider that multipolygon must not overlap
Leaflet/Leaflet#6173 (comment)
But this PR is not even a breaking change if we consider this definition, since for a non overlapping multipolygons the result of the function will stay the same ; the only difference is that it will indeed loop over all the polygons instead of exiting early.

Tests

The tests have been ran for this package, and a new case has been created to specifically test the situation with a multipolygon with a point on the borders of a small polygon inside a bigger one.

I let Github CI run the full tests.

Optimize booleanPointInPolygon to return "true" as soon as one polygon contains the point instead of looping over all the polygons
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