mapview icon indicating copy to clipboard operation
mapview copied to clipboard

mapview fails silently for sf object mixing polygon/multipolygon, but works for sp with same mix

Open sheffe opened this issue 5 years ago • 2 comments

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.

sheffe avatar Oct 05 '20 16:10 sheffe

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.

tim-salabim avatar Oct 07 '20 17:10 tim-salabim

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...

tim-salabim avatar Oct 11 '20 09:10 tim-salabim