我有一个 geojson 是FeatureCollection
包含 2 种地理数据类型:LineString
and a waypoint
- 查看原始文件here https://raw.githubusercontent.com/Robinlovelace/stplanr/master/inst/extdata/route_data.geojson- 这就是它的样子GitHub https://github.com/Robinlovelace/stplanr/blob/master/inst/extdata/route_data.geojson:
我只想加载LineString
,所以这就是我所做的:
library(RCurl)
obj <- getURL("https://raw.githubusercontent.com/Robinlovelace/stplanr/master/inst/extdata/route_data.geojson")
writeLines(obj, "/tmp/obj.geojson")
obj <- readLines("/tmp/obj.geojson")
just_lines <- obj[14:(length(obj) - 28)]
just_lines[1] <- paste0("{", just_lines[1])
just_lines[length(just_lines)] <- "}"
writeLines(just_lines, "/tmp/just_lines.geojson")
现在我们已经删除了文件开头和结尾的讨厌的行,这是一个格式良好的 GeoJSON 文件,我们可以加载和绘制,是的:
library(rgdal)
route <- readOGR("/tmp/just_lines.geojson", layer = "OGRGeoJSON")
plot(route)
但对于任何 R 用户来说,显而易见的是,这是一种非常笨拙且低效的方法,涉及太多代码行以及不必要的硬盘读写。一定还有别的办法!
我看过的选项
- 地理JSONIO https://github.com/ropensci/geojsonio
- jsonlite https://github.com/jeroenooms/jsonlite
-
leaflet https://github.com/rstudio/leaflet,可以显示
FeatureCollection
但似乎没有提取其部分。
Context
我正在创建一个可持续交通规划包,stplanr https://github.com/Robinlovelace/stplanr. A function https://github.com/Robinlovelace/stplanr/blob/master/R/gFlow.R要查找骑行路线(如下图所示),需要加载FeatureCollection geojson 数据 https://github.com/Robinlovelace/stplanr/raw/master/inst/extdata/route_data.geojson来自CycleStreets.net API http://www.cyclestreets.net/api/.