-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPoT_data_processing.R
165 lines (131 loc) · 4.93 KB
/
SPoT_data_processing.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# packages ----------------------------------------------------------------
library(tidyverse)
library(sf)
library(here)
library(mapview)
library(readxl)
library(janitor)
library(httr)
library(esri2sf) # install using remotes::install_github("yonghah/esri2sf"); for more info see: https://github.com/yonghah/esri2sf
library(tigris)
library(tools)
## conflicts ----
library(conflicted)
conflicts_prefer(dplyr::filter)
# SPoT Catchments ---------------------------------------------------------
## read spot catchments ----
spot_catchments <- st_read(here('data_raw',
'SPoT_20190304_Catchments_WS.shp')) %>%
clean_names()
st_crs(spot_catchments)
glimpse(spot_catchments %>% st_drop_geometry())
# mapview(spot_catchments)
## write spot catchments ----
st_write(spot_catchments,
here('data_processed',
'spot_catchments_2019-03-04.gpkg'),
append = FALSE)
# SPoT Sites (2022) -------------------------------------------------------
## read SPoT Sites ----
# spot_sites_2022 <- read_excel(path = here('data_raw',
# 'SPoT 2022 sites.xlsx'),
# sheet = 'Site List',
# skip = 2) %>%
# clean_names() %>%
# rename(id = x1)
# glimpse(spot_sites_2022)
spot_sites <- read_excel(path = here('data_raw',
'SPoT 2023_sites_XY_Final.xlsx'),
sheet = 'Site List',
skip = 2,
na = c('', 'NA')) %>%
clean_names() %>%
rename(id = x1)
glimpse(spot_sites)
## clean SPoT Sites ----
### make sure all longitudes are negative ----
range(spot_sites$target_long)
sum(spot_sites$target_long > 0)
spot_sites <- spot_sites %>%
mutate(target_long = case_when(target_long > 0 ~ -1 * target_long,
TRUE ~ target_long))
range(spot_sites$target_long)
sum(spot_sites$target_long > 0)
### fix inconsistent capitalization
spot_sites <- spot_sites %>%
mutate(possible_sample_loc = tolower(possible_sample_loc))
### convert avg tox to numeric (if needed) ----
spot_sites <- spot_sites %>%
mutate(average_toxicity_2008_2022 = as.numeric(average_toxicity_2008_2022))
## convert SPoT Sites to sf ----
spot_sites_sf <- st_as_sf(spot_sites,
coords = c('target_long', 'target_lat'),
crs = 4269) %>%
st_transform(crs = 3310)
st_crs(spot_sites_sf)
### check SPoT Sites ----
plot(spot_catchments$geometry)
plot(spot_sites_sf$geometry, add = TRUE, col = 'red')
## write SPoT Sites ----
st_write(spot_sites_sf,
here('data_processed',
'spot_sites.gpkg'),
append = FALSE)
# CES Data ----------------------------------------------------------------
## shapefile ----
temp_dir <- tempdir()
url_ces4_shp <- 'https://oehha.ca.gov/media/downloads/calenviroscreen/document/calenviroscreen40shpf2021shp.zip'
## download zip file
GET(url = url_ces4_shp,
write_disk(file.path(temp_dir,
basename(url_ces4_shp)),
overwrite = TRUE))
unzip(zipfile = file.path(temp_dir,
basename(url_ces4_shp)),
exdir = file.path(temp_dir,
basename(url_ces4_shp) %>%
file_path_sans_ext()))
# exdir = tempdir())
# unlink('calenviroscreen40shpf2021shp.zip')
sf_ces4 <- st_read(file.path(temp_dir,
basename(url_ces4_shp) %>%
file_path_sans_ext())) %>%
arrange(Tract) %>%
clean_names()
# st_crs(sf_ces4)
names(sf_ces4)
## write CES data ----
st_write(sf_ces4,
here('data_processed',
'calenviroscreen_4-0.gpkg'),
append = FALSE)
# CA boundary -------------------------------------------------------------
## get CA boundary ----
ca_boundary <- states(year = 2020,
cb = TRUE) %>% # use cb = TRUE to get the cartographic boundary file
filter(STUSPS == 'CA') %>%
st_transform(3310)
## write CA boundary ----
st_write(ca_boundary,
here('data_processed',
'ca_boundary.gpkg'),
append = FALSE)
# tribal boundaries -------------------------------------------------------
## from Bureau of Indian Affairs - see: https://biamaps.doi.gov/bogs/datadownload.html
## get boundaries (all US) ----
tribal_bounds_bia <- esri2sf(
url = 'https://biamaps.doi.gov/server/rest/services/DivLTR/BIA_AIAN_National_LAR/MapServer/0',
crs = NULL) %>%
rename(geom = geoms) %>%
clean_names()
st_crs(tribal_bounds_bia)
tribal_bounds_bia <- tribal_bounds_bia %>%
st_transform(3310)
## filter for tribal areas in CA ----
tribal_bounds_bia <- tribal_bounds_bia %>%
st_filter(ca_boundary)
## write tribal boundaries ----
st_write(tribal_bounds_bia,
here('data_processed',
'ca_tribal_boundaries_bia.gpkg'),
append = FALSE)