[英]Trouble reading geojson data into R
I am trying to read in geojson data to use with the leaflet
package. 我正在尝试读取geojson数据以与leaflet
包一起使用。 I have tried reading in the data multiple ways, but I cannot get it to plot. 我尝试以多种方式读取数据,但无法绘制它。 Data is here . 数据在这里 。
I tried using the code in this StackOverflow post . 我尝试在此StackOverflow帖子中使用代码。 The first block gave me a blank map. 第一块给了我一张空白的地图。 The second gave me a map with no new lines added. 第二个给了我一张没有添加新行的地图。
I also tried the code from here . 我也从这里尝试过代码。 I am new to this, so any help would be appreciated. 我对此并不陌生,因此不胜感激。
Is this what you want? 这是你想要的吗?
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)
A bit more concise, but gives the same result 更加简洁,但结果相同
library(sf)
library(leaflet)
sf <- sf::st_read("https://opendata.arcgis.com/datasets/772f3621fa354ec9abf3ba33f3ace59e_0.geojson")
leaflet() %>%
addTiles() %>%
addPolygons(data = sf)
This also makes the assumption your GeoJSON only consists of polygons 这也使您的GeoJSON仅由多边形组成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.