Turfjs / Turfjs/turf

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

Open
#2,266 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

@turf/line-intersect bug
Dominant language
TypeScript
Stars
10.5k
Forks
1k
Avg merge
1h 11m
Merged PRs (30d)
4

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied CodePen with TurfJS 6 and Leaflet 1.7.1, comparing booleanIntersects() with lineIntersect() for the centroid-to-centroid LineString and polygon. Trace the line-related methods involved and use the sample GeoJSON to verify the expected boundary point; done means the reported intersection behavior is corrected or clearly characterized with coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
computer-graphics, web-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.