Truncate and simplify generates Error: invalid polygon
See original GitHub issueLatest turfjs version, location: /@turf/turf@6.5.0/turf.min.js
Code for reproducing:
const polygon = {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[12.56231543067888,55.71174271303183],[12.5622924993585,55.7117097183066],[12.56245909105533,55.71165693489258],[12.56248995363732,55.71168436284357],[12.56231543067888,55.71174271303183]]]}}
turf.truncate(polygon)
turf.simplify(turf.truncate(polygon, { precision: 4 }))
Error generated:
turf.min.js:43 Uncaught Error: invalid polygon
at en (turf.min.js:43)
at turf.min.js:43
at Array.forEach (<anonymous>)
at tn (turf.min.js:43)
at turf.min.js:88
at turf.min.js:88
at q (turf.min.js:1)
at Object.t.simplify (turf.min.js:88)
at <anonymous>:3:6
After debugging, it is the simplify function that removes duplicates, and thus ends up with a polygon that is not a polygon anymore.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
simplify fails to create a reasonable polygon in some cases
I tried simplifying directly with simplify-js and the issue doesn't occur with that. I'm not sure what kind of polygons triggers the issue....
Read more >Fixing non-noded intersection problem using PostGIS
I'm using a PL/R function and PostGIS to generate voronoi polygons around a set ...
Read more >Advanced geospatial analysis - Turf.js
Takes one or more features and calculates the centroid using the mean of all vertices. This lessens the effect of small islands and...
Read more >Check Geometry (Data Management)—ArcGIS Pro
Geometry that is validated or repaired using the OGC option will be valid for the Esri option. To learn more about both methods,...
Read more >Fix invalid polygon in Shapely - python - Stack Overflow
I found a solution that works for the specific case given: >>> pp2 = pp.buffer(0) >>> pp2.is_valid True >>> pp2.exterior.coords[:] [(0.0, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing as there has been no follow up. Hope this helped!
Hey @kevinsimper, no problem at all. Appreciate you providing more details always helpful to gain context.
Here, I can totally appreciate that it is a surprise that
truncate
produces a Polygon which is invalid for thesimplify
function. However, here as the developer you are in control of theprecision
parameter passed totruncate
. If we think about whattruncate
does, it cuts off the precision of the geometries coordinates to a given precision, in this case you have chosen 4. Here, a coordinate like:[12.56231543067888, 55.71174271303183]
becomes:
[12.5623, 55.7117]
so the actual behaviour is exactly correct. This is to say, truncating the coordinates you have given as example to a precision of
4
produces exactly the right output (there is no bug in the behaviour). Think about it as per JavaScript’stoFixed
number method, if you ran:It’s the same logic to
truncate
in this scenario.As a developer you can control the precision to ensure that truncate does not produce an invalid Polygon input for the
simplify
function. For example, you can loop incrementing the precision until it produces a valid Polygon for your requirements.In the interests of closing this, I’ve written a solution that should do the above: