StatisticsNZ / simplevis

Simple visualisation with ggplot2 and leaflet wrappers
https://StatisticsNZ.github.io/simplevis
Other
86 stars 11 forks source link

Mix of geometry types #456

Open cphaarmeyer opened 1 year ago

cphaarmeyer commented 1 year ago

A lot of the time I have datasets with a mix of polygons and multipolygons. It would be helpful (and simpler) if leaf_sf() could just deal with this case.

library(simplevis)
library(sf)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE

data <- st_union(example_polygon, by_feature = TRUE)
data
#> Simple feature collection with 963 features and 2 fields
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: 1090000 ymin: 4748153 xmax: 2089541 ymax: 6194182
#> Projected CRS: NZGD2000 / New Zealand Transverse Mercator 2000
#> First 10 features:
#>    grid_id density                       geometry
#> 1       12   0.000 POLYGON ((1199950 4815303, ...
#> 2       14   0.000 POLYGON ((1249363 4828620, ...
#> 3       28 110.454 POLYGON ((1259922 4857799, ...
#> 4       88  58.405 POLYGON ((1219940 4927049, ...
#> 5       89  62.744 POLYGON ((1239931 4927049, ...
#> 6       90  89.362 POLYGON ((1259922 4927049, ...
#> 7       91   6.112 POLYGON ((1279913 4927049, ...
#> 8      135   0.000 POLYGON ((1149972 4978987, ...
#> 9      151   0.000 MULTIPOLYGON (((1140918 498...
#> 10     167   0.000 POLYGON ((1149972 5013612, ...

# error
leaf_sf(data)
#> Error in if (geometry_type %in% c("POINT", "MULTIPOINT")) {: the condition has length > 1

# this works
invisible(leaf_sf(st_cast(data, "MULTIPOLYGON")))

Created on 2023-01-27 with reprex v2.0.2

I could make a PR for this if you'd like me to.