Closed RamiKrispin closed 3 years ago
> st_is_longlat(df)
[1] TRUE
> sf_use_s2()
[1] TRUE
> table(sf::st_is_valid(df))
FALSE TRUE
3 179
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> table(sf::st_is_valid(df))
TRUE
182
Please do read https://cran.r-project.org/web/packages/sf/news/news.html, noting for 1.0-0
use s2 spherical geometry as default when coordinates are ellipsoidal
This is more of a problem with the {rnaturalearth}
package, which is where the flawed dataset is coming from, than either {sf}
or {s2}
, which behave in line with their documentation.
Note how the issue seems to involve only countries that straddle the antimeridian; this will give you some idea about its root cause.
As a workaround I suggest using a different version of the world dataset; there are several readily available to choose from. My favorite lives in the {giscoR}
package, which interfaces to shapefiles provided by the good folks of the Eurostat.
library(rnaturalearth)
library(sf)
library(dplyr)
library(giscoR)
map <- ne_countries(returnclass = "sf") %>%
dplyr::select(name, iso2 = iso_a2, iso3 = iso_a3, geometry)
map$name[!st_is_valid(map)]
[1] "Antarctica" "Fiji" "Russia"
map2 <- giscoR::gisco_get_countries() %>%
dplyr::select(name = NAME_ENGL, iso2 = CNTR_ID, iso3 = ISO3_CODE)
map2$name[!st_is_valid(map2)]
character(0) # i.e. nothing is invalid = all countries are valid
Eurostat cheated:
> st_bbox(map2[map2$name == "Fiji",])
xmin ymin xmax ymax
-180.00000 -20.70876 179.99999 -12.46203
thank you all for the solution and explanation!
Describe the bug Change in the output of the
sf
object after moving to version1.0.4
from version0.9.5
. Some of the geometric object classified as an invalid objectsTo Reproduce
Checking the if all the geometry objects are valid:
Repeat the process with version
1.0.4
With version
1.0.4
the geometry objects of Russia and Fiji are invalidAdditional context This issue cause the following error: