Open dc27 opened 2 years ago
Yes, this is a big issue, in particular given that some packages (tmap) require valid geometries, and that currently (sf 1.0.8) st_make_valid
makes things actually worse, see example below.
Note that the validity depends on whether one uses computations on the plane (sf_use_s2(FALSE)
) or on the sphere (sf_use_s2(TRUE)
), and validity on one does not guarantee validity in the other... so a pretty complicated task :-(
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.3, PROJ 8.2.0; sf_use_s2() is TRUE
library(rnaturalearth)
library(rnaturalearthdata)
world <- ne_countries(scale = "medium", returnclass = "sf")
## default
sf_use_s2(TRUE)
world$geounit[!st_is_valid(world)]
#> [1] "Antarctica" "Fiji" "India" "Russia" "Sudan"
#> [6] "South Sudan"
sum(!st_is_valid(world))
#> [1] 6
plot(world %>% st_make_valid() %>% st_geometry())
sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off
sum(!st_is_valid(world))
#> [1] 1
world$geounit[!st_is_valid(world)]
#> [1] "India"
Created on 2022-08-16 by the reprex package (v2.0.1)
Is there a reason that the medium-sized country geometries contain invalid geoms? This means some spatial manipulation operations, e.g. calculating a centroid require the geometries to be made valid beforehand.
Invalid geometries for Antarctica, Fiji, India(?), Russia, Sudan, and S.Sudan