s2 icon indicating copy to clipboard operation
s2 copied to clipboard

sf issues associated with s2

Open edzer opened this issue 11 months ago • 6 comments

There's a collection: https://github.com/r-spatial/sf/issues/1771

edzer avatar Jan 14 '25 19:01 edzer

Thanks!

paleolimbot avatar Jan 14 '25 19:01 paleolimbot

Is there a fix that does not involve changing to calculating over a flat earth? I'm trying to obtain areas for large polygons (e.g. >1000 km) and changing to a flat earth changes the calculations. Thanks!

sschooler avatar Jan 23 '25 20:01 sschooler

@sschooler a fix to what?

edzer avatar Jan 23 '25 21:01 edzer

To the issue where s2 can't handle crossing vertices and throws the error "Loop X edge Y crosses loop A edge B", and st_make_valid doesn't work (e.g. Loop 0 is not valid: Edge 4441 crosses edge 4445, Loop 0 is not valid: Edge x has duplicate vertex with edge y).

What is recommended is using sf_use_s2(FALSE), but that changes geometry to planar and therefore leads to incorrect area calculations. I tried loading the file in with spherical geometry, turning spherical geometry off to do st_make_valid, then turning it back on to calculate area, but it still throws the error.

I think I might have a fix- it seems like if I use the s2 package with the s2_area function and set st_as_s2 manually using the rebuild = TRUE argument, it works. I found that solution in this thread st_difference broken with S2 on?.

sschooler avatar Jan 23 '25 21:01 sschooler

What is recommended is using sf_use_s2(FALSE), but that changes geometry to planar and therefore leads to incorrect area calculations.

But are the differences significant? Here is comparison using geographical coordinates and geodesic surface from lwgeom and the results are quite consistent.

kadyb avatar Jan 26 '25 17:01 kadyb

I saw that, but that example is on the scale of millions of square meters, and the areas I'm looking at are on the scale of billions of square meters. The distances become more pronounced the larger the area because the curvature of the earth matters more. E.g. in a calculation of area for one of my polygons, there is a difference of 30 million meters when computing with the lwgeom (sf_use_s2(FALSE)) versus computing with s2 (sf_use_s2(TRUE))

sschooler avatar Jan 27 '25 21:01 sschooler