leaflet icon indicating copy to clipboard operation
leaflet copied to clipboard

`addPolygons` fails when the polygon has 3 dimensions, even if the 3rd dimension is all zero.

Open warnes opened this issue 2 years ago • 0 comments

At the moment, addPolygons fails with an unhelpful message when the polygon has 3 dimensions, even if the 3rd dimension is all zero.

It's reasonable that addPolygons doesn't handle 3-d polygons, but it would be helpful if it detected this and generated a more helpful error message, perhaps something like:

Objects with Z or M dimensions are not supported. Retry after removing the M or Z dimension (e.g. using sf::st_zm).

Example:

A simple 2-D Polygon renders fine:

> pl_2d = list(
+   rbind(
+     c(0,0), 
+     c(1,0), 
+     c(1,1), 
+     c(0,1), 
+     c(0,0)
+     )
+   )

> mp_2d = sf::st_multipolygon(list(pl_2d))

> leaflet() %>% 
+   addTiles() %>%
+   addPolygons(data=mp_2d)

But adding a (constant) 3rd dimension yields an error with an unhelpful message:

> pl_3d <- list(
+   rbind(
+     c(0,0,0), 
+     c(1,0,0), 
+     c(1,1,0), .... [TRUNCATED] 

> mp_3d = sf::st_multipolygon(list(pl_3d))

> leaflet() %>% 
+   addTiles() %>%
+   addPolygons(data=mp_3d)
Error in if (length(nms) != n || any(nms == "")) stop("'options' must be a fully named list, or have no names (NULL)") : 
  missing value where TRUE/FALSE needed

Of course, Explicitly removing the third dimension (e.g. using sf::st_zm()) resolves the issue.

> leaflet() %>% 
+   addTiles() %>%
+   addPolygons(data=mp_3d %>% sf::st_zm())

warnes avatar Aug 23 '23 20:08 warnes