-
Notifications
You must be signed in to change notification settings - Fork 8
/
global.R
86 lines (65 loc) · 2.96 KB
/
global.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
## 01. Load libraries, set options, source functions and resources -------------
require(tidyverse)
require(tidycensus)
require(sf)
require(tigris)
require(mapview)
options(
scipen = 999,
digits = 4,
tigris_class = "sf",
tigris_use_cache = T
)
source("4_scripts/1_data-processing/01_load_spatial_resources.R")
source("3_functions/get_counties_from_cbsa.R")
source("3_functions/get_states_from_stcnty_fips.R")
## 02. Determine and input target region's FIPS/CBSA codes and places ----------
# TODO: Enter a region to look up in the line below to search for the correct CBSA FIPS
REGION_OF_INTEREST <- "portland"
# Use code below to determine the FIPS code for the region of interest ("CBSA" = region).
# Look at the results and decide what the region's FIPS code is
county2msa %>%
filter(str_detect(cbsaname15, regex(REGION_OF_INTEREST, ignore_case = TRUE)))
# TODO: Enter the correct CBSA FIPS code found from lookup above to determine
# the counties that are part of that CBSA
COUNTIES_TO_DOWNLOAD <- get_counties_from_cbsa(38900)
## 03. Automatically generate other variables we will need based ---------------
## Determine states from counties
STATES_TO_DOWNLOAD <- get_states_from_stcnty_fips(COUNTIES_TO_DOWNLOAD)
TRACTS.SF <- map_dfr(STATES_TO_DOWNLOAD, .f = function(STATE){
tigris::tracts(STATE, cb = TRUE, year = 2020) %>%
filter(substr(GEOID, 1, 5) %in% COUNTIES_TO_DOWNLOAD)})
TRACTS_HIRES.SF <- map_dfr(STATES_TO_DOWNLOAD, .f = function(STATE){
tigris::tracts(STATE, cb = FALSE, year = 2020) %>%
filter(substr(GEOID, 1, 5) %in% COUNTIES_TO_DOWNLOAD)})
# TARGET_EPSG <- crsuggest::suggest_top_crs(TRACTS.SF, units = "us-ft")
TARGET_EPSG <- 2913
WEB_EPSG <- 4326
## Main places in CBSA
PLACES_IN_CBSA <- tigris::places(STATES_TO_DOWNLOAD) %>%
st_filter(., filter(tigris::counties(state = STATES_TO_DOWNLOAD, cb = T), GEOID %in% COUNTIES_TO_DOWNLOAD)) %>%
st_transform(TARGET_EPSG) %>%
arrange(desc(ALAND)) %>%
head()
## Grab GEOID/FIPS code of the primary place of interest, which is USUALLY
## the largest by land area.
PRIMARY_PLACE <- PLACES_IN_CBSA[1,]
TRACTS.SF <- st_transform(TRACTS.SF, TARGET_EPSG)
TRACTS_HIRES.SF <- st_transform(TRACTS_HIRES.SF, TARGET_EPSG)
## Identify a list of tracts that are within the primary place of interest
## Adjust negative distance buffer to fit desired tolerance
BUFFER_DISTANCE <- -1320
TRACTS_IN_PLACE <- TRACTS.SF %>%
st_filter(., st_buffer(PRIMARY_PLACE, dist = BUFFER_DISTANCE)) %>%
pull(GEOID)
## Quick view of the tracts in place
TRACTS.SF %>%
mutate(in_primary_place = GEOID %in% TRACTS_IN_PLACE) %>%
mapview(zcol = 'in_primary_place')
tracts_to_remove <- c('41005022208', '41051010200')
TRACTS_IN_PLACE <- setdiff(TRACTS_IN_PLACE, tracts_to_remove)
## Create new variable on whether that tract is in the primary place of interest
TRACTS.SF <- TRACTS.SF %>%
mutate(in_primary_place = GEOID %in% TRACTS_IN_PLACE)
TRACTS_HIRES.SF <- TRACTS_HIRES.SF %>%
mutate(in_primary_place = GEOID %in% TRACTS_IN_PLACE)