无法将 geojson/json 文件读入 R 以在地图上绘图
Trouble reading in geojson/json file into R for plotting on map
我正在尝试读入 json 文件,该文件包含 R 中的折线,用于在传单或 ggmap 中绘图。该文件采用 geojson 格式。
文件位于:http://datasets.antwerpen.be/v4/gis/statistischesector.json
我试过:
library(rgdal)
library(jsonlite)
library(leaflet)
geojson <- readLines("statistischesector.json", warn = FALSE) %>%
paste(collapse = "\n") %>%
fromJSON(simplifyVector = FALSE)
这实际上读取了文件,但它的格式似乎有误,无法进一步处理。
或者:
readOGR(dsn="~/statistischesector.json", layer="OGRGeoJSON")
Returns:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open data source
欢迎任何帮助!
这是一种方法
library("jsonlite")
library("leaflet")
x <- jsonlite::fromJSON("http://datasets.antwerpen.be/v4/gis/statistischesector.json", FALSE)
geoms <- lapply(x$data, function(z) {
dat <- tryCatch(jsonlite::fromJSON(z$geometry, FALSE), error = function(e) e)
if (!inherits(dat, "error")) {
list(type = "FeatureCollection",
features = list(
list(type = "Feature", properties = list(), geometry = dat)
))
}
})
leaflet() %>%
addTiles() %>%
addGeoJSON(geojson = geoms[1]) %>%
setView(
lng = mean(vapply(geoms[1][[1]]$features[[1]]$geometry$coordinates[[1]], "[[", 1, 1)),
lat = mean(vapply(geoms[1][[1]]$features[[1]]$geometry$coordinates[[1]], "[[", 1, 2)),
zoom = 12)
leaflet::addGeoJSON
确实需要特定格式。例如,geojson 字符串在 http://geojsonlint.com/ 上很好,但需要对其进行调整才能与 leaflet
一起使用。另外,至少有一个字符串格式错误,所以我添加了一个 tryCatch
来跳过那些
所有多边形
gg <- list(type = "FeatureCollection",
features =
Filter(Negate(is.null), lapply(x$data, function(z) {
dat <- tryCatch(jsonlite::fromJSON(z$geometry, FALSE), error = function(e) e)
if (!inherits(dat, "error")) {
list(type = "Feature",
properties = list(),
geometry = dat)
} else {
NULL
}
}))
)
leaflet() %>%
addTiles() %>%
addGeoJSON(geojson = gg) %>%
setView(lng = 4.5, lat = 51.3, zoom = 10)
我正在尝试读入 json 文件,该文件包含 R 中的折线,用于在传单或 ggmap 中绘图。该文件采用 geojson 格式。
文件位于:http://datasets.antwerpen.be/v4/gis/statistischesector.json
我试过:
library(rgdal)
library(jsonlite)
library(leaflet)
geojson <- readLines("statistischesector.json", warn = FALSE) %>%
paste(collapse = "\n") %>%
fromJSON(simplifyVector = FALSE)
这实际上读取了文件,但它的格式似乎有误,无法进一步处理。
或者:
readOGR(dsn="~/statistischesector.json", layer="OGRGeoJSON")
Returns:
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open data source
欢迎任何帮助!
这是一种方法
library("jsonlite")
library("leaflet")
x <- jsonlite::fromJSON("http://datasets.antwerpen.be/v4/gis/statistischesector.json", FALSE)
geoms <- lapply(x$data, function(z) {
dat <- tryCatch(jsonlite::fromJSON(z$geometry, FALSE), error = function(e) e)
if (!inherits(dat, "error")) {
list(type = "FeatureCollection",
features = list(
list(type = "Feature", properties = list(), geometry = dat)
))
}
})
leaflet() %>%
addTiles() %>%
addGeoJSON(geojson = geoms[1]) %>%
setView(
lng = mean(vapply(geoms[1][[1]]$features[[1]]$geometry$coordinates[[1]], "[[", 1, 1)),
lat = mean(vapply(geoms[1][[1]]$features[[1]]$geometry$coordinates[[1]], "[[", 1, 2)),
zoom = 12)
leaflet::addGeoJSON
确实需要特定格式。例如,geojson 字符串在 http://geojsonlint.com/ 上很好,但需要对其进行调整才能与 leaflet
一起使用。另外,至少有一个字符串格式错误,所以我添加了一个 tryCatch
来跳过那些
所有多边形
gg <- list(type = "FeatureCollection",
features =
Filter(Negate(is.null), lapply(x$data, function(z) {
dat <- tryCatch(jsonlite::fromJSON(z$geometry, FALSE), error = function(e) e)
if (!inherits(dat, "error")) {
list(type = "Feature",
properties = list(),
geometry = dat)
} else {
NULL
}
}))
)
leaflet() %>%
addTiles() %>%
addGeoJSON(geojson = gg) %>%
setView(lng = 4.5, lat = 51.3, zoom = 10)