2017-09-22 65 views

回答

0
library(rjson) 
lst=fromJSON(file = 'http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json') 

index = which(sapply(lapply(lst$features,"[[",'properties'),'[[','NAME')=='Kansas') 

subdata = lst$features[[index]] 
1

你有什麼有geoJson,可直接通過library(sf)閱讀,給你一個sf對象,這也是data.frame。然後你可以使用普通的data.frame子集化操作

library(sf) 

sf <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json") 

sf[sf$NAME == "Kansas", ] 

# Simple feature collection with 1 feature and 5 fields 
# geometry type: MULTIPOLYGON 
# dimension:  XY 
# bbox:   xmin: -102.0517 ymin: 36.99308 xmax: -94.58993 ymax: 40.00316 
# epsg (SRID): 4326 
# proj4string: +proj=longlat +datum=WGS84 +no_defs 
#   GEO_ID STATE NAME LSAD CENSUSAREA      geometry 
# 30 0400000US20 20 Kansas  81758.72 MULTIPOLYGON(((-99.541116 3... 

而且看到你想要的個別縣,你需要使用縣數據集

sf_counties <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json") 
sf_counties[sf_counties$STATE == 20, ] 
+0

因此,它概述了堪薩斯州的整個狀態,但我如何顯示其中的不同縣? – Albert

+0

@ColinAlbert你需要包含[個別縣]的數據集(http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json) – SymbolixAU

1

要留一個JSON的工作流程,可以嘗試jqr

library(jqr) 
url <- 'http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json' 
download.file(url, (f <- tempfile(fileext = ".json"))) 
res <- paste0(readLines(f), collapse = " ") 
out <- jq(res, '.features[] | select(.properties.NAME == "Kansas")') 

可以很容易地映射像

library(leaflet) 
leaflet() %>% 
    addTiles() %>% 
    addGeoJSON(out) %>% 
    setView(-98, 38, 6)