turf
turf copied to clipboard
lineIntersects() won't return the intersection of a line and a polygon
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?
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
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.
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.
This should also be resolved by https://github.com/Turfjs/turf/pull/2033 when it gets merged and released (hopefully for v7)