tmap icon indicating copy to clipboard operation
tmap copied to clipboard

qtm() fails in interactive mode with data from zonebuilder

Open Robinlovelace opened this issue 4 years ago • 6 comments

See reproducible example below. Can anyone else reproduce this error message? Seems that the geometries are valid, at least according to sf::st_is_valid():

# Error in build process:
# https://github.com/zonebuilders/zonebuilder/runs/3009599916#step:9:233

remotes::install_github("mtennekes/tmap")

library(zonebuilder)
library(tmap)
library(sf)
z = zb_zone(london_c(), london_a())
summary(sf::st_is_valid(z))
plot(z)
mapview::mapview(z) # works
qtm(z) # works
tmap_mode("view")
qtm(z)

It says:

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

Will retry after following the instructions...

Robinlovelace avatar Jul 07 '21 14:07 Robinlovelace

Interesting: manually fixing them with sf::st_make_valid() does not solve it but check.and.fix does.

Is that expected? Are there issues with the zonebuilder outputs in that case?

library(zonebuilder)
library(tmap)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
z = zb_zone(london_c(), london_a())
summary(sf::st_is_valid(z))
#>    Mode    TRUE 
#> logical      74
plot(z)
#> Error in plot.sf(x[, cname], main = cname, pal = pal, nbreaks = nbreaks, : plotting list-columns not supported
mapview::mapview(z) # works

qtm(z) # works
tmap_mode("view")
#> tmap mode set to interactive viewing
qtm(z)
#> Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot
qtm(sf::st_make_valid(z))
#> Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot
tmap_options(check.and.fix = TRUE)
qtm(z)
#> Warning: The shape z is invalid (after reprojection). See sf::st_is_valid

Created on 2021-07-07 by the reprex package (v2.0.0)

Robinlovelace avatar Jul 07 '21 14:07 Robinlovelace

My guess: the cause is the fact that the input is in a projected (27700) CRS. Maybe we should change the CRS of the object in the package data. Interesting error in tmap in any case...

Robinlovelace avatar Jul 07 '21 14:07 Robinlovelace

Hi I think it has to do with sf changes: https://github.com/r-spatial/sf/issues/1649#issuecomment-860231045

try

tmap_mode("view")
sf::sf_use_s2(FALSE)
qtm(z) #works!

temospena avatar Jul 07 '21 15:07 temospena

Thanks Rosa. Confirmed, it works!

library(zonebuilder)
library(tmap)
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
z = zb_zone(london_c(), london_a())
summary(sf::st_is_valid(z))
#>    Mode    TRUE 
#> logical      74
# plot(z)
# mapview::mapview(z) # works
# qtm(z) # works
tmap_mode("view")
#> tmap mode set to interactive viewing
sf::sf_use_s2(use_s2 = FALSE)
#> Spherical geometry (s2) switched off
qtm(z)

Created on 2021-07-07 by the reprex package (v2.0.0)

Any ideas of ways to reduce the changes of this issue affecting others?

Robinlovelace avatar Jul 07 '21 16:07 Robinlovelace

More browsing suggests that this issue may be a duplicate of https://github.com/mtennekes/tmap/issues/571. Should have checked... Is there a way to set it temporarily? I guess something like this could work in the package function:

current_s2 = sf::sf_use_s2()
if(current_s2) {
  message("Temporarily setting sf::sf_use_s2(FALSE)")
  sf::sf_use_s2(FALSE)
  # run the operation
  sf::sf_use_s2(TRUE)
}
#> Temporarily setting sf::sf_use_s2(FALSE)
#> Spherical geometry (s2) switched off
#> Spherical geometry (s2) switched on

Created on 2021-07-07 by the reprex package (v2.0.0)

Robinlovelace avatar Jul 07 '21 16:07 Robinlovelace

Not sure if this is still an issue, happy to close if sorted.

Robinlovelace avatar Nov 14 '21 10:11 Robinlovelace