0

我正在尝试将人口普查区分配到学校出勤范围。

我有德克萨斯州学校的数据,我正在使用 NCES 学校出勤边界 (SAB) 数据集来查找位于这些 SAB 内的人口普查区域。

library(sf)
library(tigris)
library(dplyr) # for pipes
library(ggplot2)

# first: pull School Attendance Boundary Data from NCES: https://data-nces.opendata.arcgis.com/datasets/school-attendance-boundary-survey-2015-2016-1
# this query filters to TX
query <- "https://nces.ed.gov/opengis/rest/services/K12_School_Locations/SABS_1516/MapServer/0/query?where=stAbbrev%20%3D%20%27TX%27&outFields=*&outSR=4326&f=json"
tx_sabs <- st_read(query, stringsAsFactors = FALSE) # this takes a minute

# SABs are larger than census tracts 
ggplot(tx_sabs) + geom_sf()

# grab all tx census tracts 
tx_tracts <- tigris::tracts(state = "48", cb = T) %>%
  sf::st_as_sf() %>%
  select(STATEFP, COUNTYFP,
         tract = GEOID)

ggplot(tx_tracts) + geom_sf()

# set to same CRS
census_crs <- st_crs(tx_tracts)
# convert SAB crs to census CRS (4269)
tx_sabs <- tx_sabs %>%
  st_transform(crs = census_crs)

# merge all census tracts within SABs into SAB
tx_sab_tracts <- sf::st_join(tx_sabs, tx_tracts, join = st_intersects, left = TRUE)

# tx_sab_tracts %>% 
#   ggplot() + geom_sf(aes(geometry = geometry)) + 
#   theme(legend.title = element_blank())

因此,st_join这里将人口普查区编号分配给与之相交的每个 SAB。由于左连接,我无法将人口普查区形状与 SAB 形状进行比较,以查看重叠。所以我想我的问题是如何确保它按我想要的方式工作?我担心人口普查区被排除在 SAB 之外。谢谢!

(编辑提及 dplyr)

4

0 回答 0