question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@turf/intersect throws error "Each LinearRing of a Polygon must have 4 or more Positions"

See original GitHub issue

Please provide the following when reporting an issue:

  • The version of Turf you are using, and any other relevant versions.
  • GeoJSON data as a gist file or geojson.io (filename extension must be .geojson).
  • Snippet of source code or for complex examples use jsfiddle.

Hi,

I created a stackblitz with a “minimal” reproduction scenario. I think what is happening is that both polygons intersect only on a lineString which then internally fails the conversion to a polygon which is understandable. However, I would expect that either null is returned or even the resulting lineString. Just letting the polygon helper function throw an error is not a very good DX and hard to debug.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
sroetteringcommented, Jun 20, 2022

@stebogit Thank you for the hint. We did not know that turf can only handle 6 decimal places “safely”. Truncating seems to be the solution for my problem. I also tried it with a simplified example of two polygons intersecting on a lineString and that does not throw an error. Thanks for the help guys!

0reactions
stebogitcommented, Jun 16, 2022

@sroettering it looks like the issue is related to the precision of the coordinates. Try truncating the coordinates in your polygons:

const intersection = turf.intersect(turf.truncate(polyA, 9), turf.truncate(polyB, 9));
// null

Not sure if there’s a formal statement in the docs (maybe there should be one), however Turf - as per GeoJSON specs - generally assumes Features don’t have more than 6 decimals, which is like 10cm spatial resolution. In fact I’ve seen other instances where too many decimals could generate unexpected results like here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

turf intersect: Each LinearRing of a Polygon must have 4 or ...
The error that is thrown when you don't have any internal rings is "Each LinearRing of a Polygon must have 4 or more...
Read more >
D3.js with Turf.js error: "Uncaught (in promise) Error: Each ...
Uncaught (in promise) Error: Each LinearRing of a Polygon must have 4 or more Positions. I've tried D3's d3.geoContain() but it threw false ......
Read more >
Each LinearRing of a Polygon must have 4 or more Positions
turf intersect : Each LinearRing of a Polygon must have 4 or more Positions.
Read more >
Each Linearring Of A Polygon Must Have 4 Or More Positions.
Turf's logic then tries to create a polygon with an incorrect coordinate array which throws the linear ring error.Here's a test case for...
Read more >
Union GeoJSON polygons - openlayers - GIS Stack Exchange
If I try with just: turf.polygon(geojsonObject) I get the error: Each LinearRing of a Polygon must have 4 or more Positions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found