我正在尝试将 geojson 作为空间对象(即 sp)从 USGS Web 服务(streamstats)导入到 R 中,但无法将其转换为 R 的正确格式。
library(jsonlite)
mydata <- fromJSON("https://streamstats.usgs.gov/streamstatsservices/watershed.geojson?rcode=NY&xlocation=-74.524&ylocation=43.939&crs=4326&includeparameters=false&includeflowtypes=false&includefeatures=true&simplify=true")
Run Code Online (Sandbox Code Playgroud)
这将返回 geojson 中的功能以及一堆我不需要的其他东西。我可以只选择我需要的数据框并将其写出:
tmp <- mydata$featurecollection[2,2]
write_json(tmp, 'test.json')
Run Code Online (Sandbox Code Playgroud)
[{"type": "FeatureCollection", ...一堆其他东西}]
如果我手动删除 json 文件两端的括号“[]”,则可以将其作为空间对象导入:
library(geojsonio)
test <- geojson_read('test.json', method='local', what='sp')
Run Code Online (Sandbox Code Playgroud)
否则我会收到此错误:
rgdal::ogrListLayers(input) 中的错误:无法打开数据源
有没有办法去掉两端的括号?也许我还缺少一个更简单的解决方案,我从中选择了所需的数据框。