sf icon indicating copy to clipboard operation
sf copied to clipboard

st_make_valid aggravating invalidy of polygon?

Open MatthieuStigler opened this issue 3 years ago • 1 comments

This is mainly for discoverability, as issues with st_make_valid have been documented elsewhere, see #1771 and #1732 for example. It has been also reported on the package's page, see: https://github.com/ropensci/rnaturalearthdata/issues/4

In this case, st_make_valid will draw a long line linking two opposite sides of the same polygon, resulting into a very strange plot.

As an aside, the dataset is also a good example that validity is s2-dependent, with some 6 invalid geometries in s2, and 1 on r2.

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)

packageVersion("s2")
#> [1] '1.1.0'
packageVersion("sf")
#> [1] '1.0.9'

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

## default
sf_use_s2(TRUE)

## probs?
world_med$geounit[!st_is_valid(world_med)]
#> [1] "Antarctica"  "Fiji"        "India"       "Russia"      "Sudan"      
#> [6] "South Sudan"

## plot probs
world_probs <- world_med[!st_is_valid(world_med),]
plot(world_probs[c(2,4),] |>st_make_valid()  |> st_geometry(), border="blue",
     main= "st_make_valid (in blue) makes things worse")
plot(world_probs[c(2,4),] |> st_geometry(), add=TRUE)



## validity depends on s2
sum(!st_is_valid(world_med))
#> [1] 6
sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off
sum(!st_is_valid(world_med))
#> [1] 1

Created on 2022-08-16 by the reprex package (v2.0.1)

MatthieuStigler avatar Aug 16 '22 08:08 MatthieuStigler

We point out here that there are cases that can't be valid on both R2 and S2. Another one is Fiji, which is cut in two. The admin boundaries distributed by GISCO / Eurostat "solve" Fiji by shifting one of the antimeridian boundaries a tiny bit, say from -180 to -197.999, which leaves a narrow gap when viewed on S2. The When you mention "aggravating" you're still wearing your R2 glasses (R plot on longlat data) to judge s2-valid polygons: I don't think that is fair, or a good idea.

Most global vector datasets have been and are being processed with R2 software (GIS legacy), so it is worrying that world_med still has one invalid geometry.

edzer avatar Aug 16 '22 08:08 edzer