使用 rmapshaper::ms_simplify 简化我的美国状态 shapefile 给出错误

simplify my us state shapefile with rmapshaper::ms_simplify gives error

我正在使用 leaflet 包在我闪亮的应用程序上制作 chloropleth us state 地图。我发现渲染地图非常慢。谷歌搜索后,似乎 shapefile 可能过于复杂和简化可能会使它更快。根据这个 ,简化 shapefile 可能是答案。

读取形状文件正常。我能够渲染我的传单地图。

states_shape <- tigris::states(cb = TRUE, resolution='500k')
leaflet(states_shape) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 0.5) %>%
  setView(-98.5795, 39.8282, zoom=3)

我尝试使用 rmapshaper::ms_simplify

来简化我的 shapefile
states_shape_simple <- rmapshaper::ms_simplify(states_shape, keep = 0.05, keep_shapes = TRUE)

我遇到如下错误:

Error in FUN(X[[i]], ...) : isTRUE(gpclibPermitStatus()) is not TRUE

我不知道那是什么意思,也不知道该怎么做。有谁知道为什么会这样以及如何让它发挥作用?非常感谢!

以下应该有效:

# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)

# data
states_shape <- tigris::states(cb = TRUE, resolution='500k', class = "sf")

# simplify
states_shape_simple <- rmapshaper::ms_simplify(states_shape, keep = 0.05, keep_shapes = TRUE)
states_shape_simple <- st_transform(states_shape_simple, 4326)

# plot
leaflet(states_shape_simple) %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(fillColor = "white",
              color = "black",
              weight = 0.5) %>%
  setView(-98.5795, 39.8282, zoom = 3)

reprex package (v0.3.0)

于 2020-05-27 创建

我添加了 states_shape_simple <- st_transform(states_shape_simple, 4326),因为我收到 leaflet 的警告消息,说对象 states_shape_simple 的数据无效。我不知道你是否面临同样的警告信息。

无论如何,如果您想了解 sfsp 之间的差异,请查看 Geoocomputation with R (and maybe Chapter 6 about reprojections, such as st_transform). I don't know why it fails with sp, maybe you can ask to the package mantainer 的第 1 章。