-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
st_union reverses polygon orientation #2096
Comments
Thanks for the clear issue! I guess this is caused by the upstream GEOS library (@dr-jts?) - when using s2geometry things are fine: > p0 = st_sfc(pl)
> st_union(p0, p0) # no CRS, assume R2, use GEOS
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1
CRS: NA
POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))
> pg = st_sfc(pl, crs = 4326) # will use s2 by default
> st_union(pg, pg)
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1
Geodetic CRS: WGS 84
POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)) A way to "fix" this in current sf is pulling it through > sf::st_union(p0, p0) |> st_sfc(check_ring_dir=TRUE)
Geometry set for 1 feature
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1
CRS: NA
POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)) |
GEOS uses CW as its canonical orientation for polygons. This was chosen historically before the SFS settled on CCW as its canonical orientation. If CCW orientation is desired then client code needs to enforce that. And before the question is asked, it's a big job to change JTS/GEOS to adopt CCW orientation, and this is probably a breaking change. But note that JTS/GEOS does not require a particular orientation for input, so its flexible in that way. |
Thanks, Martin, that clarifies! We'll put that somewhere in the documentation here. |
Are you o.k., @finnlindgren if we leave this like it is and ask you to use |
I added the Related to this, will 1.0-10 go onto CRAN soon? The |
Hopefully yes, will leave this open until it is accepted. |
sf 1.0-10 is now on CRAN. |
Hi, I continue having the same issue when trying to combine all countries into one polygon, library(needs) continents <- ne_countries(returnclass = "sf")%>% st_write(continents, "output/coastline.geojson") when uploading it to https://observablehq.com/@bumbeishvili/rewind-geojson, it seems like the polygon is not simply wound the wrong way around but seems to be some kind of a mix-up, since the rewind tool (which in other cases works great) also is not able to produce a correct version of the polygon. as i keep running into this issue and haven't managed to figure out a solution for months now, any help is much appreciated. |
Describe the bug
When using
st_union
on two "upwards" facing polygons (i.e. in CCW order), the resulting polygon is changed so that it faces downwards (CW).From section 6.1.11.1 of version 1.2.1 for the SF specification from https://www.ogc.org/standards/sfa
To Reproduce
Additional context
I've been working on conversion methods between sf classes and fmesher mesh storage classes in https://github.com/inlabru-org/inlabru/ (including INLA and the coming fmesher package that will take over the geometry handling from the INLA package), and was encountering problems when testing polygons with holes as well as conversion to and from sp, and traced at least one of the issues to this
st_union
inconsistency. The fmesher code handles general 3D triangles as well as a boundary segment specification that in 2D can store the complement of regular polygons (i.e. "free holes"), by using the a CCW traversion convention just as the quoted sf standard above (and sometimes without mandating closed sequences; we make use of non-closed "proto boundary" segments as well). In cases where we need to apply st_* methods in geospatial contexts we rely on ordering being handled consistently, so that we don't have to do all the special handling that was needed for sp objects with theringDir
issues etc.I had previously discovered that the
sp
conversion method inst_as_sfc.SpatialPolygons
is broken in sf 1.0-9 for multi-polygons (in a set of disjoint polygons all but the first polygons were removed by the conversion, as the "make_valid" code removed them, presumably for believing them to be holes, and reversing the ordering of the first polygon. That bug appears to have been fixed in 1.0-10 (at least it doesn't remove polygons) so I haven't filed an Issue for that problem. The related issues/discussion I recall seeing about sp/sf conversion issues focused on geometries read from existing files, but since fmesher is a meshing library, we are the ones creating the geometries, so need to know that we can rely on well-defined behaviour.Session info
The text was updated successfully, but these errors were encountered: