如何使用SF和dplyr软件包制作多面体

时间:2019-06-12 11:34:21

标签: r dplyr sf r-leaflet

如何使用dplyr将sf地图对象折叠为多面几何体?

当我使用group_by进行汇总时,默认的最终几何图形似乎是MULTIPOINT sfg类。我想从下面的虚拟数据中绘制3个不同的多边形(每个“区域”值1个),并带有包装传单。

非常感谢任何指导。

library(tidyverse)
library(sf)
library(leaflet)

wannabe_polygons <- st_as_sf(tibble(
  area =c(rep("southern suburbs", 8),
      rep("northern suburbs", 6),
      rep("somerset west", 5)
      ),
  lng = c(18.35, 18.37, 18.34, 18.45,
      18.49, 18.43, 18.42, 18.32,
      18.49, 18.48, 18.44, 18.7,
      18.74, 18.72, 18.82, 18.89,
      18.92, 18.87, 18.7
  ),
  lat = c(-34.1, -34.06, -33.99, -33.97,
      -34.09, -34.14, -34.19, -34.15,
      -33.88, -33.83, -33.7, -33.8,
      -33.9, -34.07, -33.9, -33.9,
      -34.11, -34.16, -34.05
  )

),
coords = c("lng", "lat"))

wannabe_polygons <- wannabe_polygons %>% 
  group_by(area) %>% 
  summarise()

# str(wannabe_polygons)

leaflet() %>% 
  addTiles(group = "OSM") %>% 
  addPolygons(data = wannabe_polygons)

1 个答案:

答案 0 :(得分:0)

不确定为什么要转换为MULTIPOLYGON,但操作方法如下:


wannabe_polygons %>%
  group_by(area) %>%
  summarise(geometry = st_combine(geometry)) %>%
  st_cast("POLYGON") %>%
  summarise(geometry = st_combine(geometry)) -> new_polygons

leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>% 
  addPolygons(data = new_polygons)

如果您使用MULTIPOLYGONPOLYGON不会对最终的传单地图产生任何影响,因此您可以在st_cast("POLYGON")之后停下来。