mapview fails silently for sf object mixing polygon/multipolygon, but works for sp with same mix
The issue title basically covers what's happening: when I plot an sf object with mixed polygon/multipolygon types, mapview fails silently (generates a completely blank map, no error/warning). I can convert the same underlying object to sp class with as(obj, "Spatial") and mapview renders correctly here.
Here's a reprex using mapview 2.9, sf 0.9-5, and sp 1.4-2; note GDAL version >= 3.1.0 | setting mapviewOptions(fgb = TRUE) for my installation.
# read in the first two counties - type is MULTIPOLYGON in raw form
nc_sample <- sf::st_read(system.file("shape/nc.shp", package = "sf"))[1:2, ]
# Works as expected
mapview(sf::st_cast(nc_sample, "MULTIPOLYGON"))
# Works as expected, with correct warning about repeating attributes
mapview(sf::st_cast(nc_sample, "POLYGON"))
# This generates a blank map
mapview(rbind(sf::st_cast(nc_sample, "POLYGON"),
sf::st_cast(nc_sample, "MULTIPOLYGON")))
# This generates the correct polygons, layered identically on top of each other, as expected
mapview(as(rbind(sf::st_cast(nc_sample, "POLYGON"),
sf::st_cast(nc_sample, "MULTIPOLYGON")),
"Spatial"))
I looked through mapview internals enough to realize I'm in over my head -- can't be more helpful on diagnosing cause unfortunately. As always, thank you for the excellent package.
Thanks! Very helpful & interesting. Seems to be related to fgb rendering. It works for me if I set mapviewOptions(fgb = FALSE). Not sure why fgb cannot handle mixed geometries... Will dig further when I find more time.
The above commit should make things work again. The issue was that we only check the object for the st_dimension() of an object and only st_cast() if we find more than one unique dim. We now also check whether the object inherits sfc_GEOMETRY and st_cast() if so. This does not necessarily solve all issue I assume, but it should address this one...