ropensci / rnaturalearthdata

to hold low and medium resolution data used by rnaturalearth
https://docs.ropensci.org/rnaturalearthdata
13 stars 3 forks source link

Invalid Geometries? #4

Open dc27 opened 2 years ago

dc27 commented 2 years ago

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.

library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(rgeos)

world <- ne_countries(scale = "medium", returnclass = "sf")

invalid_countries <- world |>
  filter(!st_is_valid(geometry)) |>
  select(name, geometry)

# error: features have invalid spherical geometry
invalid_geoms <- st_centroid(invalid_countries, geometry)

Invalid geometries for Antarctica, Fiji, India(?), Russia, Sudan, and S.Sudan

MatthieuStigler commented 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)