[英]Subsetting geojson data with R
I have a geojson file of state boundaries that I obtained from here . 我有一个状态边界的geojson文件,我从这里获得 。 In particular I'm using the 20m US States Data
特别是我正在使用2000万美国国家数据
I'm trying to subset the data so that I can use leaflet to map only certain states. 我正在尝试对数据进行子集化,以便我可以使用传单仅映射某些状态。 I can subset a single state using:
我可以使用以下方法对单个状态进
states <- geojsonio::geojson_read("gz_2010_us_040_00_20m.json", what = "sp")
az <- subset(states, NAME == "Arizona")
This method does not seem to work for selecting multiple states though: 这种方法似乎不适用于选择多个状态:
swStates <- subset(states, NAME == c("Arizona", "Nevada", "New Mexico"))
Selecting multiple states usually results in only one or two states being selected, or none at all. 选择多个状态通常会导致仅选择一个或两个状态,或者根本不选择状态。 Is there a different method I can use to subset this data?
我可以使用不同的方法来对这些数据进行子集化吗?
You need to use %in%
rather than ==
您需要使用
%in%
而不是==
cities <- geojsonio::us_cities
TwoCities <- subset(cities, name %in% c("Seattle WA", "San Francisco CA"))
Not to take away from the accepted answer, just throwing out an option if you want to get nerdy with JSON. 不要忘记接受的答案,如果你想让JSON讨厌,就扔掉一个选项。
I have a pkg in dev that tries to help filter raw GeoJSON itself using jqr
package (so no need to convert to a sp
object and use subset
) 我有一个pkg in dev试图帮助使用
jqr
包过滤原始GeoJSON本身(所以不需要转换为sp
对象并使用subset
)
devtools::install_github("ropenscilabs/geofilter")
library(geofilter)
library(leaflet)
url <- "http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_20m.json"
curl::curl_download(url, destfile = (f <- tempfile(fileext = ".json")))
ariz <- sifter(readLines(f), NAME == Arizona)
leaflet() %>%
addTiles() %>%
addGeoJSON(ariz) %>%
setView(-112, 34, zoom = 7)
can't currently do many matches at the time (eg, NAME %in% c(Arizona, Nevada, New Mexico)
) 目前不能做很多比赛(例如,
NAME %in% c(Arizona, Nevada, New Mexico)
)
Also not taking away from the accepted answer, but here is an alternative approach. 也没有从接受的答案中删除,但这是另一种方法。
I'm using library(geojsonsf)
to read the GeoJSON directly into an sf
object. 我正在使用
library(geojsonsf)
将GeoJSON直接读入sf
对象。 From there you can subset it as per any data.frame
(and do other spatial operations on it using library(sf)
) 从那里你可以根据任何
data.frame
对它进行子集data.frame
(并使用library(sf)
对其进行其他空间操作)
url <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_20m.json"
## use library(geojsonsf) to convert into an sf object
sf <- geojsonsf::geojson_sf(url)
library(dplyr)
library(sf)
sf %>%
filter(NAME %in% c("Arizona", "Nevada", "New Mexico"))
# Simple feature collection with 3 features and 5 fields
# geometry type: POLYGON
# dimension: XY
# bbox: xmin: -120.0057 ymin: 31.3323 xmax: -103.002 ymax: 42.00025
# epsg (SRID): 4326
# proj4string: +proj=longlat +datum=WGS84 +no_defs
# CENSUSAREA GEO_ID LSAD NAME STATE geometry
# 1 113594.1 0400000US04 Arizona 04 POLYGON ((-112.5386 37.0006...
# 2 121298.1 0400000US35 New Mexico 35 POLYGON ((-105.998 32.00233...
# 3 109781.2 0400000US32 Nevada 32 POLYGON ((-114.0466 40.1169...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.