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

Using booleanContains cannot accurately determine the inclusion result of line segments and concave convex polygons #2441

Open
Steve245270533 opened this issue May 31, 2023 · 2 comments

Comments

@Steve245270533
Copy link

Using booleanContains cannot accurately determine the inclusion result of line segments and concave convex polygons.

version: 6.5.0

import { lineString, polygon, booleanContains } from '@turf/turf';

const turf_polygon = polygon([
  [
    [0, 0],
    [4, 0],
    [4, 1],
    [3, 1],
    [3, 2],
    [2, 2],
    [2, 1],
    [1, 1],
    [1, 3],
    [0, 3],
    [0, 0],
  ],
]);

const turf_line = lineString([
  [1, 2],
  [4, 0],
]);

console.log(booleanContains(turf_polygon, turf_line));

The final result is true, and it is evident from the image that this line segment is not completely contained within the polygon.

I'm not sure if there's a problem with my usage. Is there any way to correctly determine the inclusion of line segments and polygons?

image

@oscar9
Copy link

oscar9 commented Jul 5, 2023

I'm also having the same issue with a very similar case. Thanks in advance.

For the case geometry 1 is polygon, geometry 2 is line. Looking at the code, looks like the issue is in isLineInPoly method. To calculate if the line is contained in the polygon, it only checks if the mid point of that line is inside the polygon. https://github.com/Turfjs/turf/blob/88dfdbb2617b70c833e30923590400dc0189e601/packages/turf-boolean-contains/index.ts#L196C27-L196C27
In the above case, the mid point of the red line is inside the polygon.

@samuelarbibe
Copy link

Hey! added a PR to fix this.
#2848

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

No branches or pull requests

4 participants