forked from degauss-org/census_block_group
-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_get_2020_block_groups.R
45 lines (36 loc) · 1.57 KB
/
01_get_2020_block_groups.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
library(tidyverse)
setwd('2020shp/')
get_block_group_shp <- function(fl_name, state_folder, state_fips) {
fl.name.zip <- paste0(fl_name, '.zip')
download.file(paste0('https://www2.census.gov/geo/tiger/TIGER2020PL/STATE/', state_folder, "/",
state_fips, "/", fl.name.zip),
destfile=fl.name.zip)
unzip(fl.name.zip)
unlink(fl.name.zip)
fl.name.shp <- paste0(fl_name, '.shp')
d.tmp <- sf::read_sf(fl.name.shp) %>% sf::st_transform(5072)
unlink(list.files(pattern = fl_name))
return(d.tmp)
}
states <-
tigris::states() %>%
sf::st_drop_geometry() %>%
arrange(STATEFP) %>%
select(STATEFP, NAME) %>%
mutate(state_folder = paste0(STATEFP, "_", toupper(NAME)),
state_folder = stringr::str_replace_all(state_folder, " ", "_"),
fl_name = paste0('tl_2020_', STATEFP, '_bg20')) %>%
filter(as.numeric(STATEFP) < 57)
block_group_shp <- pmap(list(states$fl_name, states$state_folder, states$STATEFP),
possibly(get_block_group_shp, NA_real_))
#block_group_shp[[6]] <- get_block_group_shp(states$fl_name[6], states$state_folder[6], states$STATEFP[6])
names(block_group_shp) <- states$NAME
block_group_shp_all <- block_group_shp[[1]]
for (i in 2:length(block_group_shp)) {
block_group_shp_all <- rbind(block_group_shp_all, block_group_shp[[i]])
}
blk_grps_sf_2020 <- block_group_shp_all %>%
dplyr::select(census_block_group_id_2020 = GEOID20,
geometry) %>%
mutate(census_block_group_id_2020 = as.character(census_block_group_id_2020))
saveRDS(blk_grps_sf_2020, "block_groups_2020_5072.rds")