[英]Removing brackets [] from ends of geojson in R
我正在尝试将一个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")
这将返回geojson中的功能以及一些我不需要的其他内容。 我可以只选择我需要的数据框并将其写出来:
tmp <- mydata$featurecollection[2,2]
write_json(tmp, 'test.json')
[{“type”:“FeatureCollection”,...一堆其他东西}]
如果我手动删除json文件两端的括号“[]”,我可以将其作为空间对象导入:
library(geojsonio)
test <- geojson_read('test.json', method='local', what='sp')
否则我收到此错误:
rgdal :: ogrListLayers(输入)出错:无法打开数据源
有没有办法删除每端的括号? 也许甚至还有一个更简单的解决方案,我在选择所需数据框的地方缺少这些解决方案。
这个答案假设保存文件是必要的步骤(例如,用另一个Rscript读取它)。 通过这种方式,您只进行一次慢速外部呼叫,然后您可以通过仅请求本地文件进行测试,这样可以更快。 外部请求也可能无法始终可用。
作为一种解决方法,您可以删除write_json命令行并添加以下命令行:
stringJson <- toJSON(tmp)
write(substr(stringJson, 2, nchar(stringJson) - 1), file = 'test.json')
另一种方法是使用rjson库来编写json(但要小心,因为两个库都具有相同名称的函数):
library(jsonlite)
library(rjson)
mydata <- jsonlite::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")
tmp <- mydata$featurecollection[2,2]
write(rjson::toJSON(tmp), file = 'test.json')
希望能帮助到你 :)
geojsonio
维护者......
所以rgdal::readOGR
发生了变化,其中任何比有效文件路径长的geojson字符串都被截断,因此无法读取。 请参阅https://github.com/ropensci/geojsonio/issues/130
所以我们为geojsonio::geojson_sp
做了一个解决方法,但没有针对geojsonio::geojson_read
用jqr
来解决这个jqr
library(jqr)
library(geojsonio)
library(sf)
myurl <- "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"
jq(url(myurl), ".featurecollection[1]") %>% st_read(quiet = TRUE, stringsAsFactors = FALSE)
jq
允许我们在不必先写入磁盘的情况下使用JSON
另一位geojsonio
作者在这里......
我不认为这是任何库/包( geojsonio
, rgdal
等)的问题,但是方括号使得它不是根据规范的正确的geojson对象(方括号表示数组)。
该url返回一个json对象,该对象包含一个数组(混淆地称为featurecollection
),然后包含两个对象,每个对象包含一个name
和一个特征,每个特征都是一个合适的geojson
FeatureCollection
。 那些FeatureCollections
是我们想要提取的 - 一个Point
(可能是分水岭的质心?),一个是定义分水岭边界的Polygon
。
library(jsonlite)
library(geojsonio)
library(sp)
# Don't simplify the results so that it's easier to pull out the full geojson objects
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",
simplifyVector = FALSE, simplifyDataFrame = FALSE)
# Extract each geojson object and 'auto_unbox' to remove square brackets from
# arrays of length 1:
point_geojsonsting <- toJSON(mydata$featurecollection[[1]]$feature,
auto_unbox = TRUE)
poly_geojsonsting <- toJSON(mydata$featurecollection[[2]]$feature,
auto_unbox = TRUE)
# Read directly to sp without writing to disk first:
point_sp <- geojson_sp(point_geojsonsting)
poly_sp <- geojson_sp(poly_geojsonsting)
plot(poly_sp)
plot(point_sp, add = TRUE, col = "red", pch = 21, cex = 5)
由reprex包 (v0.2.0)于2018-05-09创建。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.