簡體   English   中英

從R中的geojson末尾刪除括號[]

[英]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作者在這里......

我不認為這是任何庫/包( geojsoniorgdal等)的問題,但是方括號使得它不是根據規范的正確的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM