From 84f325352f35ff4075791b74cbb6955ef7e19643 Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Fri, 27 Oct 2023 15:57:03 -0500 Subject: [PATCH] split up scripts into 4 to avoid repitition --- 01_download_data.R => 01_set_extent.R | 22 +------------ 02_download_data.R | 23 ++++++++++++++ 02_process_data.R | 44 -------------------------- 03_process_data.R | 21 ++++++++++++ 03_make_figures.R => 04_make_figures.R | 0 5 files changed, 45 insertions(+), 65 deletions(-) rename 01_download_data.R => 01_set_extent.R (55%) create mode 100644 02_download_data.R delete mode 100644 02_process_data.R create mode 100644 03_process_data.R rename 03_make_figures.R => 04_make_figures.R (100%) diff --git a/01_download_data.R b/01_set_extent.R similarity index 55% rename from 01_download_data.R rename to 01_set_extent.R index bdd4de3..f2dac1c 100644 --- a/01_download_data.R +++ b/01_set_extent.R @@ -28,24 +28,4 @@ extent_bbox <- st_bbox(st_transform(x = extent_poly, crs)) extent <- list(longitude_max = max(extent$longitude), longitude_min = min(extent$longitude), latitude_max = max(extent$latitude), - latitude_min = min(extent$latitude)) - -# get watershed areas -extent_huc <- get_huc(AOI = extent_poly, buffer = 0, type = "huc04") - -# download data -download_nhdplushr(nhdplusTools_data_dir(), extent_huc$huc4, download_files = TRUE) - -# get vaa -get_vaa() - -# combine data into geopackage -layers <- c("NHDArea", - "NHDFlowline", - "NHDWaterbody", - "NHDPlusLandSea") - -data <- get_nhdplushr(hr_dir = nhdplusTools_data_dir(), - out_gpkg = paste0(nhdplusTools_data_dir(),"/data.gpkg"), - layers = layers, - overwrite = TRUE) + latitude_min = min(extent$latitude)) \ No newline at end of file diff --git a/02_download_data.R b/02_download_data.R new file mode 100644 index 0000000..0eeb0e0 --- /dev/null +++ b/02_download_data.R @@ -0,0 +1,23 @@ +# load libraries +library(nhdplusTools) +library(sf) + +# get watershed areas +extent_huc <- get_huc(AOI = extent_poly, buffer = 0, type = "huc04") + +# download data +download_nhdplushr(nhdplusTools_data_dir(), extent_huc$huc4, download_files = TRUE) + +# get vaa +get_vaa() + +# combine data into geopackage +layers <- c("NHDArea", + "NHDFlowline", + "NHDWaterbody", + "NHDPlusLandSea") + +data <- get_nhdplushr(hr_dir = nhdplusTools_data_dir(), + out_gpkg = paste0(nhdplusTools_data_dir(),"/data.gpkg"), + layers = layers, + overwrite = TRUE) diff --git a/02_process_data.R b/02_process_data.R deleted file mode 100644 index 3418dab..0000000 --- a/02_process_data.R +++ /dev/null @@ -1,44 +0,0 @@ -# load libraries -library(nhdplusTools) -library(sf) - -# reload extent data so you don't need to run the download data script again -data_dir <- "data" -nhdplusTools_data_dir(dir = paste0(data_dir, "/hydrologic")) - -extent <- read.csv(file = "extent.csv") -crs <- 4269 - -extent_poly <- st_polygon( - x = list( - cbind( - extent$longitude[c(1,2,2,1,1)], - extent$latitude[c(1,1,2,2,1)]) - ) -) - -extent_poly <- st_sfc(extent_poly, crs=4326) -extent_bbox <- st_bbox(st_transform(x = extent_poly, crs)) -extent <- list(longitude_max = max(extent$longitude), - longitude_min = min(extent$longitude), - latitude_max = max(extent$latitude), - latitude_min = min(extent$latitude)) - -# load and crop hydrologic data -data <- list(NULL) -sf_use_s2(FALSE) -layers <- c("NHDArea", - "NHDFlowline", - "NHDWaterbody", - "NHDPlusLandSea") -for (layer in layers){ - data[[layer]] <- st_crop(st_read(paste0(nhdplusTools_data_dir(),"/data.gpkg"), layer = layer), y = extent_bbox) -} - -# load political boundaries -political <- st_crop( - st_transform( - st_read( - paste0(data_dir, "/political_boundaries/bound_p/boundaries_p_2021_v3.shp")), - crs=crs), - y = extent_bbox) \ No newline at end of file diff --git a/03_process_data.R b/03_process_data.R new file mode 100644 index 0000000..0e8626e --- /dev/null +++ b/03_process_data.R @@ -0,0 +1,21 @@ +# load libraries +library(nhdplusTools) +library(sf) +# load and crop hydrologic data +data <- list(NULL) +sf_use_s2(FALSE) +layers <- c("NHDArea", + "NHDFlowline", + "NHDWaterbody", + "NHDPlusLandSea") +for (layer in layers){ + data[[layer]] <- st_crop(st_read(paste0(nhdplusTools_data_dir(),"/data.gpkg"), layer = layer), y = extent_bbox) +} + +# load political boundaries +political <- st_crop( + st_transform( + st_read( + paste0(data_dir, "/political_boundaries/bound_p/boundaries_p_2021_v3.shp")), + crs=crs), + y = extent_bbox) \ No newline at end of file diff --git a/03_make_figures.R b/04_make_figures.R similarity index 100% rename from 03_make_figures.R rename to 04_make_figures.R