Skip to content

fix(boolean-crosses): a LineString that only touches a Polygon boundary does not cross it - #3091

Open
spokodev wants to merge 4 commits into
Turfjs:masterfrom
spokodev:w33/turf-boolean-crosses-touch
Open

spokodev wants to merge 4 commits into
Turfjs:masterfrom
spokodev:w33/turf-boolean-crosses-touch

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

booleanCrosses returned true for a LineString that only touches a Polygon
boundary without passing through the interior.

doLineStringAndPolygonCross treated any intersection between the line and the
polygon boundary as a crossing. Per the OGC definition a cross requires the line
to have parts both inside and outside the polygon. A line that merely touches the
boundary at a point (endpoint on a vertex or edge) has all of its interior on one
side, so it does not cross.

Concrete cases on the polygon [[0,0],[0,10],[10,10],[10,0],[0,0]]:

  • Line [[0,5],[5,5]] lies inside the polygon and touches the left edge. It is
    booleanWithin === true, yet the old code also returned booleanCrosses === true,
    which is contradictory.
  • Line [[-5,-5],[0,0],[-5,5]] lies outside and touches a corner. The old code
    returned true.

Both match the JSTS Geometry.crosses reference, which returns false for them.

The fix splits the line on the polygon boundary and checks the midpoint of each
resulting sub-segment. A cross is reported only when at least one sub-segment lies
strictly inside and another lies strictly outside. Genuine crossings still return
true.

Added a false fixture for the inside-touching case.

spokodev added 4 commits July 2, 2026 13:02
…ine-split

The new @turf/line-split dependency was added to package.json but not to the
tsconfig references, so monorepolint's standardTsconfig rule failed with
"tsconfig.json: Expect file contents to match". Written by mrl check --fix.

Verified locally: mrl check exits 100 before and 0 after; prettier and eslint
clean; tsc --build succeeds; the package suite is 23/23 including the new
LineInsidePolygonTouchesBoundary fixture.
@spokodev

Copy link
Copy Markdown
Contributor Author

CI went red on this branch when I merged master on 27 August; before that it was green.

The cause was mine rather than the change. Upstream adopted generated tsconfig project references in the meantime (#3145, merged 7 August), and I had added @turf/line-split to this package's dependencies without the matching entry in its tsconfig.json references — so monorepolint's standardTsconfig rule reported tsconfig.json: Expect file contents to match.

19022a05 adds it, written by mrl check --fix rather than by hand. Locally: mrl check exits 100 before and 0 after, prettier and eslint are clean, tsc --build succeeds, and the package suite is 23/23 including the new LineInsidePolygonTouchesBoundary fixture. CI is green on 22, 24 and 26 for this head. The branch is four commits behind master, so say the word if you would rather I merge again first.

This branch has not been deployed

No deployments
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