【问题标题】:simplify my us state shapefile with rmapshaper::ms_simplify gives error使用 rmapshaper::ms_simplify 简化我的美国状态 shapefile 给出错误
【发布时间】:2020-09-13 23:34:42
【问题描述】:

我正在使用传单包在我闪亮的应用程序上制作 chloropleth us state 地图。我发现渲染地图非常慢。谷歌搜索后,看起来 shapefile 可能过于复杂和简化,这可能会使其速度更快。根据post,简化 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

我不知道这意味着什么以及该怎么做。有谁知道为什么会发生这种情况以及如何使其工作?非常感谢!

【问题讨论】:

  • 嗨!你可以尝试运行tigris::states() 设置class = "sf" 然后rmapshaper::ms_simplify() 吗?
  • 它有效!请把它作为答案,我会接受的。顺便说一句,“sp”和“sf”有什么区别?为什么它适用于 class='sf'?

标签: r gis shapefile r-leaflet tigris


【解决方案1】:

以下应该有效:

# 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 年 5 月 27 日创建

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

无论如何,如果您想了解sfsp 之间的区别,请查看Geoocomputation with R 的第1 章(也许还有关于重投影的第6 章,例如st_transform)。不知道为什么sp会失败,也许你可以问package mantainer

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2020-08-03
    • 2020-08-23
    • 2020-07-31
    • 2021-12-05
    • 2020-03-16
    • 2012-01-05
    • 2020-01-16
    相关资源
    最近更新 更多