【发布时间】:2019-04-28 18:02:37
【问题描述】:
我正在尝试读取 geojson 数据以与 leaflet 包一起使用。我尝试过多种方式读取数据,但我无法将其绘制出来。数据为here。
我尝试使用this StackOverflow post 中的代码。第一个街区给了我一张空白地图。第二个给了我一张没有添加新线的地图。
我还尝试了here 的代码。我是新手,所以任何帮助将不胜感激。
【问题讨论】:
我正在尝试读取 geojson 数据以与 leaflet 包一起使用。我尝试过多种方式读取数据,但我无法将其绘制出来。数据为here。
我尝试使用this StackOverflow post 中的代码。第一个街区给了我一张空白地图。第二个给了我一张没有添加新线的地图。
我还尝试了here 的代码。我是新手,所以任何帮助将不胜感激。
【问题讨论】:
这是你想要的吗?
library(leaflet)
library(jsonlite)
download.file("https://opendata.arcgis.com/datasets/772f3621fa354ec9abf3ba33f3ace59e_0.geojson", "RPD_Sections.geojson")
x = fromJSON("RPD_Sections.geojson", FALSE)
leaflet() %>% addTiles() %>% addGeoJSON(x) %>% setView(
lng = mean(vapply(x$features[[1]]$geometry$coordinates[[1]], "[[", 1, 1)),
lat = mean(vapply(x$features[[1]]$geometry$coordinates[[1]], "[[", 1, 2)),
zoom = 12)
【讨论】:
更简洁一些,但给出相同的结果
library(sf)
library(leaflet)
sf <- sf::st_read("https://opendata.arcgis.com/datasets/772f3621fa354ec9abf3ba33f3ace59e_0.geojson")
leaflet() %>%
addTiles() %>%
addPolygons(data = sf)
这也假设您的 GeoJSON 仅包含多边形
【讨论】: