-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patha_locs_poly_setup.R
370 lines (308 loc) · 11.4 KB
/
a_locs_poly_setup.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Source functions for this {targets} list
tar_source("a_locs_poly_setup/src/")
# Setting up the locations and polygon files for RS retrieval -------------
# this collates a few different polygon and point files into a single
# file of each type as needed for the RS workflow. CLP = Cache La Poudre,
# NW = Northern Water.
a_locs_poly_setup <- list(
# project config settings (you must use the `config::` style here)
tar_target(
name = a_config,
command = config::get(),
cue = tar_cue("always")
),
# check for proper directory structure ------------------------------------
tar_target(
name = a_check_dir_structure,
command = {
directories = c("a_locs_poly_setup/nhd/",
"a_locs_poly_setup/out/")
walk(directories, function(dir) {
if(!dir.exists(dir)){
dir.create(dir)
}
})
}
),
# get CLP polygons --------------------------------------------------------
# get the polygons for CLP watershed using HUC8
tar_target(
name = a_make_CLP_polygon,
command = {
a_check_dir_structure
get_polygons(HUC = "10190007",
minimum_sqkm = 0.01,
ftypes = c(390, 436))
},
packages = c("sf", "nhdplusTools", "tidyverse", "janitor")
),
# load the CLP polygons
tar_file_read(
name = a_CLP_polygons,
command = a_make_CLP_polygon,
read = read_sf(!!.x),
packages = "sf"
),
# get the NW-specific reservoirs --------------------------------------------
# track and load the csv with NW locs
tar_file_read(
name = a_NW_locs_file,
command = "data/spatialData/ReservoirLocations.csv",
read = read_csv(!!.x),
packages = "readr"
),
# using the locs file, get the upstream huc-4s to download NHDplusHR
# this returns a list to branch over.
tar_target(
name = a_get_NW_hucs,
command = {
a_check_dir_structure
get_hucs_from_points(point_csv = a_NW_locs_file,
CRS = "EPSG:4326")
},
packages = c("sf", "nhdplusTools", "tidyverse")
),
# now download the polygons associated with the huc4s from previous target
# we branch here, but return a repeated collated file name over the length of
# the list
tar_target(
name = a_get_NW_NHD,
command = get_polygons(HUC = a_get_NW_hucs,
minimum_sqkm = 0, # we aren't going to filter for size here
ftypes = c(390, 436)),
packages = c("sf", "nhdplusTools", "tidyverse", "janitor"),
pattern = map(a_get_NW_hucs)
),
# select the NW polygons by location from the collated polygon from previous target
tar_target(
name = a_get_NW_polygons,
command = select_polygons_by_points(shapefiles = a_get_NW_NHD,
points = a_NW_locs_file),
packages = c("sf", "tidyverse")
),
# track and load the polygons file for NW sites
tar_file_read(
name = a_NW_polygons,
command = a_get_NW_polygons,
read = read_sf(!!.x),
packages = "sf"
),
# combine NW/CLP polygons -------------------------------------------------
# here, we combine the NW and CLP polygons into a single file, condensing
# the metadata where needed
tar_target(
name = a_make_NW_CLP_polygons,
command = combine_and_simplify_sfs(sf_1 = a_CLP_polygons,
data_group_1 = "CLP",
sf_2 = a_NW_polygons,
data_group_2 = "NW",
filename = "CLP_NW_polygons",
simplify = TRUE),
packages = c("sf", "tidyverse")
),
# and then track and load the resulting polygon file
tar_file_read(
name = a_NW_CLP_polygons,
command = a_make_NW_CLP_polygons,
read = read_sf(!!.x),
packages = "sf"
),
# calculate centers of the polygons ---------------------------------------
# from the polygons, we're going to calculate the center point for each of them
tar_target(
name = a_make_NW_CLP_centers,
command = get_POI_centers(polygons = a_NW_CLP_polygons,
out_file = "NW_CLP_polygon_centers"),
packages = c("tidyverse", "sf", "polylabelr")
),
# and then track and load the centers file
tar_file_read(
name = a_NW_CLP_centers,
command = a_make_NW_CLP_centers,
read = read_sf(!!.x),
packages = "sf"
),
# load NW station locations --------------------------------------------------
# and now we'll read in the station location information for NW
tar_file_read(
name = a_NW_station_locs,
command = "data/spatialData/Northern Water Station Coordinates.xlsx",
read = read_excel(!!.x, sheet = "Lake_Res_edit"),
packages = "readxl"
),
# And make it a sf object, adding in the NHD info from the upstream polygons file
tar_target(
name = a_make_NW_station_points,
command = load_points_add_NHD_info(points = a_NW_station_locs,
polygons = a_NW_polygons,
data_grp = "NW",
loc_type = "station"),
packages = c("tidyverse", "sf")
),
# here we track and load that simple features file
tar_file_read(
name = a_NW_station_points,
command = a_make_NW_station_points,
read = read_sf(!!.x),
packages = "sf"
),
# load ROSS CLP station locations --------------------------------------------------
# let's also bring in the ROSS CLP subset of lakes, many of these are random points
# in the lake and not specific to a sampling location
tar_file_read(
name = a_ROSS_CLP_file,
command = 'data/CLP/upper_poudre_lakes_v2.csv',
read = read_csv(!!.x),
packages = 'readr'
),
# create a sf object of the ROSS CLP lakes
tar_target(
name = a_ROSS_CLP_points,
command = st_as_sf(a_ROSS_CLP_file,
crs = "EPSG:4326",
coords = c("Longitude", "Latitude")),
packages = "sf"
),
# get associated polygons for ROSS CLP -------------------------------------
# get polygons info from NW/CLP sf
tar_target(
name = a_ROSS_CLP_polygons,
command = a_NW_CLP_polygons[a_ROSS_CLP_points %>% st_transform(st_crs(a_NW_CLP_polygons)), ],
packages = 'sf'
),
# add ROSS_CLP label to data group ----------------------------------------
# since all the ROSS_CLP reservoirs are in NW_CLP centers and polygons files,
# we'll just add ROSS_CLP label to data group and make a new polygon target
tar_target(
name = a_NW_CLP_ROSS_centers,
command = {
NHD_perm_ids = unique(a_ROSS_CLP_polygons$permanent_identifier)
a_NW_CLP_centers %>%
mutate(data_group = if_else(permanent_identifier %in% NHD_perm_ids,
paste(data_group, "ROSS_CLP", sep = ", "),
data_group))
},
packages = c("tidyverse", "sf")
),
# do the same for NW_CLP polygons
tar_target(
name = a_NW_CLP_ROSS_polygons,
command = {
NHD_perm_ids = unique(a_ROSS_CLP_polygons$permanent_identifier)
a_NW_CLP_polygons %>%
mutate(data_group = if_else(permanent_identifier %in% NHD_perm_ids,
paste(data_group, "ROSS_CLP", sep = ", "),
data_group))
},
packages = c("tidyverse", "sf")
),
# pull out the ROSS_CLP centers -------------------------------------------
# and then we'll make the ROSS_CLP centers as a .csv
tar_target(
name = a_make_ROSS_CLP_centers,
command = {
NHD_perm_ids = unique(a_ROSS_CLP_polygons$permanent_identifier)
a_ROSS_CLP_centers <- a_NW_CLP_ROSS_centers %>%
filter(permanent_identifier %in% NHD_perm_ids)
points_to_csv(a_ROSS_CLP_centers, 'ROSS_CLP_centers')
},
packages = c("tidyverse", "sf")
),
# load and track that file
tar_file_read(
name = a_ROSS_CLP_centers,
command = a_make_ROSS_CLP_centers,
read = read_csv(!!.x),
packages = 'readr'
),
# prep for RS pull --------------------------------------------------------
# we want the centers and the station locations to be in a single data set for
# use in the Landsat pull, and want to retain the metadata (aka, data group
# in this case)
tar_target(
name = a_make_collated_points,
command = combine_and_simplify_sfs(sf_1 = a_NW_CLP_ROSS_centers, data_group_1 = NA_character_,
sf_2 = a_NW_station_points, data_group_2 = NA_character_,
filename = "CLP_NW_ROSS_points", simplify = FALSE),
packages = c("sf", "tidyverse")
),
# and track and load that simple feature
tar_file_read(
name = a_collated_points,
command = a_make_collated_points,
read = read_sf(!!.x),
packages = "sf"
),
# and create a .csv of the file for use in the RS pull workflow
tar_target(
name = a_collated_pts_to_csv,
command = points_to_csv(points = a_collated_points,
filename = "NW_CLP_all_points"),
packages = c("tidyverse", "sf")
),
# get EcoRegion L3 polygons ------------------------------------------------
# we're going to pull ER L3 lake centers to create a localized handoff
# coefficient. This is, in part, a side quest to see how the hand off
# coefficients change regionally (if at all)
tar_target(
name = a_ecoregion_aoi,
command = {
temp_file <- tempfile(fileext = 'zip')
download.file("https://gaftp.epa.gov/EPADataCommons/ORD/Ecoregions/us/us_eco_l3.zip",
destfile = temp_file)
temp_dir <- tempdir()
unzip(temp_file, exdir = temp_dir)
er_l3 <- read_sf(file.path(temp_dir, "us_eco_l3.shp"))
# filter for zone 21 only
er_l3 %>% filter(US_L3CODE == 21) %>% st_union()
},
packages = c("tidyverse", "sf")
),
tar_target(
name = a_aoi_hucs,
command = get_huc(AOI = a_ecoregion_aoi,
type = "huc04") %>%
st_drop_geometry() %>%
pull(huc4),
packages = c("nhdplusTools", "sf")
),
tar_target(
name = a_make_aoi_polygons,
command = get_polygons(HUC = a_aoi_hucs,
minimum_sqkm = 0.01,
ftypes = c(390, 436)),
packages = c("sf", "nhdplusTools", "tidyverse", "janitor"),
pattern = map(a_aoi_hucs)
),
tar_target(
name = a_aoi_polygons,
command = {
map(a_make_aoi_polygons,
read_sf) %>%
bind_rows() %>%
# there may be some tiny polygons that squeak through because there is
# no minimum set for 1019 from processing the NW and CLP reservoirs
filter(area_sq_km >= 0.01)
}
),
tar_target(
name = a_make_aoi_centers,
command = get_POI_centers(polygons = a_aoi_polygons,
out_file = "er3z21_centers"),
packages = c("tidyverse", "sf", "polylabelr")
),
# and then track and load the centers file
tar_file_read(
name = a_aoi_centers,
command = a_make_aoi_centers,
read = read_sf(!!.x),
packages = "sf"
),
# and output a .csv for the RS pull
tar_target(
name = a_aoi_centers_to_csv,
command = points_to_csv(points = a_aoi_centers,
filename = "er3z21_centers"),
packages = c("tidyverse", "sf")
)
)