-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path1_fetch_spatial.R
More file actions
234 lines (202 loc) · 8.56 KB
/
1_fetch_spatial.R
File metadata and controls
234 lines (202 loc) · 8.56 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
source("1_fetch/src/Download_nhd.R")
source('1_fetch/src/download_states_shp.R')
source('1_fetch/src/fetch_nhdplus_data.R')
p1_sp_targets_list <- list(
# Lake locations (lat/lon) Fetch #
## Reading and cleaning list of saline lakes
tar_target(
p1_lakes_sf,
{read_csv('1_fetch/in/saline_lakes.csv', col_types = 'ccnn') %>%
st_as_sf(coords = c('Lon','Lat'), crs = p0_selected_crs) %>%
rename(point_geometry = geometry, lake = `Lake Ecosystem`, state = State) %>%
mutate(state_abbr = case_when(state == 'California' ~ 'CA',
state == 'Nevada' ~ 'NV',
state == 'Utah' ~ 'UT',
state == 'Oregon' ~ 'OR',
state == 'California/Oregon' ~ 'CA', # Put lake crossing OR CA border as CA for now.
TRUE ~ 'NA'),
lake = str_to_title(lake),
lake_w_state = paste(lake, state_abbr, sep = ','),
lake_name_shrt = trimws(str_replace(lake, pattern = 'Lake', replacement = "")))
}
),
## Lake sf dataset from sharepoint - project team provided
## This target is the same as p2_saline_lakes_sf but includes the manually drawn polygon Carson Sink.
## This target has been created to:
## 1/ have a second multipolygon dataset for lakes
## 2/ append Carson Sink to p2_saline_lakes_sf
## This should be manually downloaded to local 1_fetch/in/sharepoint folder
tar_target(
p1_saline_lakes_bnds_sf,
st_read('1_fetch/in/SalineLakeBnds/SalineLakeBnds.shp') %>%
st_transform(crs=st_crs(p1_lakes_sf)) %>%
## Formatting for easier rbind with p2_saline_lakes_sf
rename(GNIS_Name = Name) %>%
mutate(lake_w_state = paste0(GNIS_Name,',',State)) %>%
select(lake_w_state, GNIS_Name, geometry)
),
# States Shp Fetch - used for Lakes Querying #
## Download states shp
tar_target(
p1_download_states_shp,
download_states_shp(url = pO_states_dwnld_url,
out_path = '1_fetch/out/states_shp'),
format = 'file'
),
tar_target(
p1_states_sf,
st_read(file.path(p1_download_states_shp,'statesp010g.shp'), quiet = TRUE) %>%
filter(STATE_ABBR %in% c('CA',"NV",'UT','OR')) %>%
st_transform(crs = st_crs(p1_lakes_sf)) %>%
select(NAME,STATE_ABBR, geometry)
),
# nhdhr download and fetch #
## 1st fetch of huc08 of lakes to be ableo all relevant high res nhd data (water bodies, huc6, huc8, huc10 areas) for focal lakes
tar_target(
p1_huc08_full_basin_sf,
get_huc8(AOI = p1_lakes_sf$point_geometry)
),
## Split huc ids to 04 to pull nhdhr - download_nhdhr() requires that huc id param be huc4.
tar_target(
p1_huc04_for_download,
substr(p1_huc08_full_basin_sf$huc8, start = 1, stop = 4) %>%
unique() %>%
## adding 1601 which is a HU4 that contains watersheds relevant to Great Salt Lake
append('1601')
),
## Download high res nhd data to get lake water bodies
tar_target(
p1_download_nhdhr_lakes_path,
download_nhdhr_data(nhdhr_gdb_path = '1_fetch/out/nhdhr',
huc04_list = p1_huc04_for_download),
format = 'file'
),
## Fetch waterbodies, huc6, huc8, huc10 from hr and place in local gpkg
tar_target(p1_nhd_gpkg,
get_downloaded_nhd_data(gdb_path = p1_download_nhdhr_lakes_path,
out_gpkg_path = '1_fetch/out/nhd_WB_HUC6_HU8_HU10.gpkg',
layer = c('NHDWaterbody','WBDHU6', 'WBDHU8', 'WBDHU10')),
format = 'file'
),
# # OPTIONAL - if you already have the gpkg, and do not want to build,
# # place it in the correct folder and comment out target above :
# # In sharepoint, gpkg is located here: https://doimspp.sharepoint.com/sites/IIDDStaff/Shared%20Documents/Forms/AllItems.aspx?id=%2Fsites%2FIIDDStaff%2FShared%20Documents%2FFunction%20%2D%20Data%20Pipelines%2FData%5FScience%5FPractitioners%5FMtgs%2Ftasks%2Fsaline%5Flakes%2FData&viewid=acf8af64%2Daaf3%2D4f23%2D8b74%2D2859a89813c5
# tar_target(p1_nhd_gpkg,
# '1_fetch/out/nhd_WB_HUC6_HU8_HU10.gpkg'),
# Lakes Fetch #
## Read in all nhd hr waterbodies in our entire basin
## This is then processed to our focal lakes in 2_process.R
tar_target(p1_nhdhr_lakes,
sf::st_read(p1_nhd_gpkg,
layer = 'NHDWaterbody',
## filtering to larger lakes because nhdhr has a lot of very small lake polygons
query = 'SELECT * FROM NHDWaterbody WHERE Shape_Area > 7e-08',
quiet = TRUE)
),
# HUC area Processing #
## Note - using p2_saline_lakes_sf - created in 2_process.R - to scope HUCs
## Reading in huc6 entire basin
tar_target(
p1_basin_huc6_sf,
st_read(p1_nhd_gpkg, layer = 'WBDHU6', quiet = TRUE) %>%
st_transform(crs = st_crs(p2_saline_lakes_sf))
),
## Reading in all huc8 in entire basin
tar_target(
p1_basin_huc8_sf,
st_read(p1_nhd_gpkg, layer = 'WBDHU8', quiet = TRUE) %>%
st_transform(crs = st_crs(p2_saline_lakes_sf))
),
## Reading in all huc10 in entire basin
tar_target(
p1_basin_huc10_sf,
st_read(p1_nhd_gpkg, layer = 'WBDHU10', quiet = TRUE) %>%
st_transform(crs = st_crs(p2_saline_lakes_sf))
),
## Fetch watershed boundary areas filtered to our lakes - huc8 - HR
### note possible duplicate polygons since some individual saline lakes have same huc08
tar_target(
p1_lakes_huc6_sf,
p1_basin_huc6_sf %>%
## filter HUC6 to only the Huc6 surrounding saline lakes
st_join(p2_saline_lakes_sf, left = FALSE) %>%
filter(!is.na(GNIS_Name)) %>%
distinct()
),
## Fetch watershed boundary areas filtered to our lakes - huc8 - HR
### note possible duplicate polygons since some individual saline lakes have same huc08
tar_target(
p1_lakes_huc8_sf,
p1_basin_huc8_sf %>%
## filter HUC8 to only the HUC8 with within selected HUC6
st_join(x = ., y = p1_lakes_huc6_sf[,c('HUC6','lake_w_state')],
join = st_within, left = FALSE) %>%
filter(!is.na(HUC6)) %>%
distinct()
),
## Fetch watershed boundary areas - huc10
### note possible duplicate polygons since some individual saline lakes have same huc10
### This target is very slow to build!
tar_target(
p1_lakes_huc10_sf,
p1_basin_huc10_sf %>%
## Filtering HUC10 to within selected HUC8s
st_join(x = .,
y = p1_lakes_huc8_sf[,c('HUC6', 'HUC8', 'lake_w_state')],
join = st_within, left = FALSE) %>%
filter(!is.na(HUC8)) %>%
distinct()
),
# Flowlines Fetch #
## Grab vector of our huc08s in order to run branching for nhd flowlines fetch
tar_target(
p1_huc8_vec,
{unique(p1_lakes_huc8_sf$HUC8)}
),
# Fetch nhdplus flowlines for each selected huc8 region separately through dynamic branching - note difference between branches
tar_target(
p1_lake_flowlines_huc8_sf_lst,
{get_nhdplus(AOI = {p1_lakes_huc8_sf %>% filter(HUC8 %in% p1_huc8_vec)},
realization = 'flowline') %>%
## fixing col that are automatically transforming to char
mutate(across(c(surfarea, lakefract, rareahload), ~as.numeric(.x)),
HUC8 = p1_huc8_vec)
},
pattern = map(p1_huc8_vec),
iteration = 'list'
),
tar_target(
p1_lake_flowlines_huc8_sf,
bind_rows(p1_lake_flowlines_huc8_sf_lst)
),
# # NWIS site fetch from nhdplus
# # Fetch NWIS sites along tributaries and in our huc08 regions.
# # for comparison purposes
# ## Will require further filtering (e.g. ftype == ST, along flowlines only)
tar_target(
p1_nwis_sites_from_nhdplus_lst,
{tryCatch(expr = get_huc8(id = p1_huc8_vec) %>% get_nwis(AOI = .) %>%
## making as dataframe to load with tar_load()
as.data.frame() %>%
mutate(HUC8 = p1_huc8_vec),
error = function(e){
return(warning(e$message))
},
warning = function(w){
return(message(paste(w$message, 'huc8:', p1_huc8_vec)))
}
)},
pattern = map(p1_huc8_vec),
iteration = 'list'
),
tar_target(
p1_nwis_sites_from_nhdplus,
bind_rows(p1_nwis_sites_from_nhdplus_lst)
),
#
# ## Pulling site no from gauge sites to then query nwis and WQP with data retrieval
tar_target(
p1_site_ids_from_nhdplus,
{p1_nwis_sites_from_nhdplus %>% pull(site_no) %>% unique()}
)
)