sf
sf copied to clipboard
st_make_valid failing to make geometries valid
I'm analysing species distributions within Australia. Some of my geometries are invalid, and are not able to be made valid using either st_make_valid
or st_buffer
.
I subsetted the troublesome invalid geometry from the large dataset which can be found here.
> invalid.geom <- st_read(dsn = "~/invalid.geom.gpkg")
Reading layer `invalid.geom' from data source
`~\invalid.geom.gpkg'
using driver `GPKG'
Simple feature collection with 3 features and 0 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: 42.66 ymin: -69.82 xmax: 173.4 ymax: -24
Geodetic CRS: GDA94
> invalid.geom <- st_make_valid(invalid.geom)
> st_is_valid(invalid.geom, reason = TRUE)
[1] "Loop 35: Edge 387 crosses edge 1104" "Loop 0 edge 0 crosses loop 266 edge 4" "Loop 0 edge 3590 crosses loop 371 edge 0"
> invalid.geom.b <- st_buffer(invalid.geom, 0.0)
Error in s2_geography_from_wkb(x, oriented = oriented, check = check) :
Evaluation error: Found 3 features with invalid spherical geometry.
[1] Loop 0 is not valid: Edge 2153 crosses edge 2162
[2] Loop 0 edge 0 crosses loop 266 edge 4
[3] Loop 0 edge 3590 crosses loop 371 edge 0.
Would you be able to help me dig deeper on this, please?
Yes, this is a nice odd case for s2
. If you switch s2
off, by sf_use_s2(FALSE)
then things seem to work. The geometries are then valid on the plane, not on the sphere, but this may be good enough, depending on your purpose. Do you need to compute buffers?
Works for me, thank you for your help @edzer. Not at this point, if I do I'll switch to an alternative software or be aware of the projection errors. Just buffer analysis to be aware of when computing planar geometries?
Just buffer analysis to be aware of when computing planar geometries?
No, any geometric operation or measure when large distances, poles or antimeridian are involved.
The dataset contained duplicated rows, one row contained geometry that were slightly larger than the second and roughly in the same location. When removed the problem does not persist. I would think this doesn't explain why this was happening so I won't close this but from my end this issue has been resolved - I am now using s2 with no hiccups.
how can i validate the geometries of a folder with formta .SAFE?, because I'm trying process copernicus tiles but the problem is this Error in UseMethod("st_is_valid") : no applicable method for 'st_is_valid' applied to an object of class "logical"
@Pablocr7 This issue is closed. Your problem is with sen2r, and your script- which you have not shown - creates a logical vector, not an sf object. Are you doing anything like st_make_valid(st_is_valid())
? Please stop adding to irrelevant issues, and post a plain text minimal reproducible example on the R-sig-geo mailing list: https://stat.ethz.ch/mailman/listinfo/R-SIG-Geo aftet subscribing.
This is still an issue. See https://github.com/r-tmap/tmap/issues/606.
With invalid_geometry.rds.zip in your working directory, st_make_valid
does nothing:
invalid_geometry = readRDS("invalid_geometry.rds.zip")
st_is_valid(st_make_valid(invalid_geometry))
[1] FALSE FALSE FALSE FALSE FALSE