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

lineIntersects() won't return the intersection of a line and a polygon #2266

Open
Kirkman opened this issue Mar 3, 2022 · 4 comments
Open

Comments

@Kirkman
Copy link

Kirkman commented Mar 3, 2022

I'm having trouble with running lineIntersects() on a line and a polygon.

I'm using TurfJS 6, via https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js. I'm mapping the results in Leaflet 1.7.1.

A working example of my issue (including sample GeoJSON data) can be found here:
https://codepen.io/Kirkman/pen/JjOxjwP

Basically, I have two neighboring polygons. I use Turf to create a LineString running from the centroid of one to the centroid of the other. What I want to do now is use lineIntersects() to find the point where this LineString intersects the boundary of one of the polygons.

My code looks roughly like this:

// Calculate the distance beteween centroid of highlighted polygon and the centroid of the neighboring polygon.
const highlight_centroid = turf.centerOfMass(highlight_feature);
const centroid = turf.centerOfMass(neighbor_feature);
const line = turf.lineString([highlight_centroid, centroid]);

// Find the intersection of this line with the boundary of neighbor_feature

// First, check if it intersects. Turf says TRUE, the line DOES intersect neighbor_feature
console.log(turf.booleanIntersects(line,neighbor_feature));

// But lineIntersect() returns an empty feature collection.
// There should be a point coordinate
const intersection = turf.lineIntersect(line,neighbor_feature);
console.log(intersection);

Turf's booleanIntersects() method returns TRUE -- because the line and polygon do intersect.

But when I try to use lineIntersect() to obtain the intersection point, Turf just returns an empty feature collection. And it does the same if I try other similar methods like lineOverlap() or lineSplit().

Am I doing something wrong, or is there an issue with these line-related methods?

@cuddlecake
Copy link

booleanIntersects will find out if there is shared geometry between two geometries.
So if you have a Polygon and a LineString, booleanIntersects will also return true if the LineString is completely contained by the Polygon, meaning that the LineString does not intersect with any of the edges of the Polygon.

lineIntersect will only find intersections between lines (edges of the Polygon and the LineString).

This may be a reason why no Points are returned.

booleanCrosses might be useful to you in this case (not exactly sure what it does), or a combination of booleanIntersect and booleanWithin

@Kirkman
Copy link
Author

Kirkman commented Mar 4, 2022

Yes, finding an intersection between a lineString and the edge of a polygon is exactly what I'm trying to do with lineIntersect(). (please see my codepen example) It should work.

@cuddlecake
Copy link

In your example, you define line as:

const line = turf.lineString([highlight_centroid, centroid]);

where highlight_centroid and centroid are Features themselves (with a Point geometry), but turf.lineString expects an array of coordinates.

const line = turf.lineString([highlight_centroid.geometry.coordinates, centroid.geometry.coordinates]);

should do the trick.

@rowanwins
Copy link
Member

This should also be resolved by #2033 when it gets merged and released (hopefully for v7)

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

3 participants