afrimapr / afrilearndata

small african spatial datasets for learning & teaching mapping in R
https://afrimapr.github.io/afrilearndata
15 stars 11 forks source link

Error: Shape contains invalid polygons tmap - not on all systems - africountries #6

Closed andysouth closed 3 years ago

andysouth commented 3 years ago

tmap::tm_shape(africountries)+tmap::tm_borders()

gives : Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

but only on RStudio cloud & seemingly with the book : https://github.com/afrimapr/afrimapr-book/issues/30

The error where it occurs seems to be caused by a problem with Sudan boundaries. sf::st_is_valid(africountries) [1] TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [14] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [27] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [40] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

isvalid <- sf::st_is_valid(africountries)
africountries$name[!isvalid]

[1] "Sudan"

Note also that even on systems where there is an error mapview(africountries) runs without error

andysouth commented 3 years ago

Reading from the shapefile also causes the error.

filename <- system.file("extdata","africountries.shp", package="afrilearndata", mustWork=TRUE)
africountries2 <- sf::read_sf(filename)
tm_shape(africountries2)+tmap::tm_borders()

Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

andysouth commented 3 years ago

seemingly occurs on sf version 1.0.0 and not on sf 0.9.8 packageVersion('sf')

andysouth commented 3 years ago
sudan <- africountries[africountries$name=='Sudan',]
mapview(sudan)

There are two dodgy looking parts of the boundary with South Sudan : image

andysouth commented 3 years ago

Those extra lower lines are not in the original rnaturalearth data that africountries was derived from.

library(rnaturalearth)
nessd <- ne_countries(country='sudan')
mapview(nessd)

image

andysouth commented 3 years ago

... and previous is the case when done on RStudio cloud too.

So it seems that the weird lines were introduced when africountries was created in https://github.com/afrimapr/afrilearndata/blob/master/data-raw/afrilearndata-data-creation.R

Perhaps a problem with sf then & changing CRS specifications.

Try rerunning creation of africountries.

andysouth commented 3 years ago

Seems to be OK now (e.g. after installing afrilearndata_0.0.0.9003 from github to RStudio cloud)

@AnneMTreasure can you check if this now works for you ?

AnneMTreasure commented 3 years ago

Hi @andysouth,

Thank you so much for fixing this!

I reinstalled afrilearndata using:

remotes::install_github("afrimapr/afrilearndata")

packageVersion('afrilearndata')
0.0.0.9003

This code, tmap::tm_shape(africountries)+tmap::tm_borders()

as well as reading from the shapefile:

filename <- system.file("extdata","africountries.shp", package="afrilearndata", mustWork=TRUE)
africountries2 <- sf::read_sf(filename)
tm_shape(africountries2)+tmap::tm_borders()

now works:

map2

and Sudan looks good:

sudan <- africountries[africountries$name=='Sudan',]
mapview(sudan)

map

This worked for packageVersion('sf') - 1.0.0 as well as 0.9.8

packageVersion('tmap')
3.3.1
packageVersion('afrilearndata')
0.0.0.9003

Great, thanks so much Andy!