Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

Commit 2beef10

Browse files
author
Lauren Koenig Snyder
committed
commit state boundaries to 1_fetch/in and pass to map_sites function
1 parent c296e2e commit 2beef10

5 files changed

Lines changed: 39 additions & 12 deletions

File tree

1_fetch.R

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ source("1_fetch/src/fetch_nhdv2_attributes_from_sb.R")
99

1010
p1_targets_list <- list(
1111

12+
# Track and read in the state boundaries sf object. This file was
13+
# created by running `states <- get_state_shp()` followed by
14+
# `saveRDS(states, "1_fetch/in/state_boundaries.rds)`. get_state_shp
15+
# is from 3_visualize/src/map_sites.R. It depends on the USAboundaries
16+
# package, which is currently archived on CRAN.
17+
tar_target(
18+
p1_state_boundaries_rds,
19+
"1_fetch/in/state_boundaries.rds",
20+
format = "file"
21+
),
22+
tar_target(
23+
p1_state_boundaries_sf,
24+
readRDS(p1_state_boundaries_rds)
25+
),
26+
1227
# download WQP data product from science base for discrete samples
1328
tar_target(
1429
p1_wqp_data_file,

1_fetch/in/state_boundaries.rds

207 KB
Binary file not shown.

3_visualize.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ p3_targets_list <- list(
7777
p3_site_map_png,
7878
map_sites(flowlines = p1_nhd_reaches_sf,
7979
matched_sites = p2a_site_splits,
80+
states_shp = p1_state_boundaries_sf,
8081
out_file = "3_visualize/out/do_site_map.png")
8182
),
8283

3_visualize/src/map_sites.R

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#' @param matched_sites sf object containing site locations and the flowline
1010
#' reach identifier ("COMID") that the site has been matched to. Must contain
1111
#' columns "COMID" and "geometry".
12+
#' @param states_shp sf object containing the U.S. state boundaries to
13+
#' include in the watershed plot.
1214
#' @param out_file character string indicating the name of the saved file,
1315
#' including file path and extension.
1416
#' @param huc6_select vector of character string(s) indicating the HUC6 basins that
@@ -37,6 +39,7 @@
3739
#'
3840
map_sites <- function(flowlines,
3941
matched_sites,
42+
states_shp,
4043
out_file,
4144
huc6_select = "020402",
4245
basin_bbox = c(xmin = -76.39556, ymin = 39.5, xmax = -74.37121, ymax = 40.89106),
@@ -109,7 +112,7 @@ map_sites <- function(flowlines,
109112
ggspatial::annotation_scale(bar_cols = c("gray70","white"))
110113

111114
# create inset map
112-
inset_map <- map_drb_watershed(matched_sites)
115+
inset_map <- map_drb_watershed(matched_sites, states_shp = states_shp)
113116

114117
# grab legend
115118
legend <- cowplot::get_legend(sites_map)
@@ -145,10 +148,6 @@ map_sites <- function(flowlines,
145148
#' @param huc8 vector of character string(s) indicating the HUC8 basins that
146149
#' make up the watershed of interest. By default, the HUC8 basins that make up
147150
#' the Delaware River Basin will be used.
148-
#' @param states vector of character string(s) indicating which states should
149-
#' be included in the inset map, using two-letter postal code abbreviations for
150-
#' each state. By default, the states surrounding the Delaware River Basin will
151-
#' be downloaded, including "NY", "PA", "NJ", "DE", and "MD".
152151
#' @param epsg_out integer indicating the coordinate reference system that
153152
#' should be used when creating the inset map. Defaults to EPSG 3857, pseudo-
154153
#' mercator: https://epsg.io/3857
@@ -157,17 +156,14 @@ map_sites <- function(flowlines,
157156
#' Returns an inset map as a ggplot object.
158157
#'
159158
map_drb_watershed <- function(sites,
159+
states_shp,
160160
huc8 = c("02040101","02040102","02040103",
161161
"02040104","02040105","02040106",
162162
"02040201","02040202","02040203",
163163
"02040204","02040205","02040206",
164164
"02040207"),
165-
states = c("NY","PA","NJ","DE","MD"),
166165
epsg_out = 3857){
167166

168-
# Download shapefiles for states that encompass the watershed of interest
169-
states_shp <- USAboundaries::us_states(resolution = "high", states = states)
170-
171167
# Download HUC8 boundaries associated with watershed boundary
172168
boundary <- nhdplusTools::get_huc8(id = huc8) %>%
173169
sf::st_union()
@@ -191,6 +187,21 @@ map_drb_watershed <- function(sites,
191187
}
192188

193189

190+
#' @title Download state shapefiles
191+
#'
192+
#' @param states vector of character string(s) indicating which states should
193+
#' be included in the inset map, using two-letter postal code abbreviations for
194+
#' each state. By default, the states surrounding the Delaware River Basin will
195+
#' be downloaded, including "NY", "PA", "NJ", "DE", and "MD".
196+
#'
197+
get_state_shp <- function(states = c("NY","PA","NJ","DE","MD")){
198+
199+
# Download shapefiles for states that encompass the watershed of interest
200+
states_shp <- USAboundaries::us_states(resolution = "high", states = states)
201+
202+
}
203+
204+
194205
#' @title Create leaflet map of site locations
195206

196207
#' @description

_targets.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ library(targets)
33
options(tidyverse.quiet = TRUE)
44
tar_option_set(packages = c("tidyverse", "lubridate", "rmarkdown", "knitr",
55
"dataRetrieval", "nhdplusTools", "sbtools",
6-
"leaflet", "sf", "USAboundaries", "cowplot",
7-
"ggspatial", "patchwork", "streamMetabolizer",
8-
"reticulate", "yaml"))
6+
"leaflet", "sf", "cowplot", "ggspatial",
7+
"patchwork", "streamMetabolizer", "reticulate",
8+
"yaml"))
99

1010
source("1_fetch.R")
1111
source("2_process.R")

0 commit comments

Comments
 (0)