diff --git a/workspace/00_get_data.Rmd b/workspace/00_get_data.Rmd index 241ef2b9ac46cc3bb8585746f688aed60fbac8e1..91bfcd70011d9af2fb72eff91e33f127d093503d 100644 --- a/workspace/00_get_data.Rmd +++ b/workspace/00_get_data.Rmd @@ -213,9 +213,7 @@ if(!file.exists(nhdplus_dir)) { , " -o", data_dir), ignore.stdout = T)} ) - nhdplus_path(nhdplus_gdb) - - suppressWarnings(staged_nhd <- stage_national_data()) + suppressWarnings(staged_nhd <- stage_national_data(nhdplus_data = nhdplus_gdb)) x <- tryCatch( download_nhdplusv2(islands_dir, url = paste0("https://dmap-data-commons-ow.s3.amazonaws.com/NHDPlusV21/", @@ -227,9 +225,7 @@ if(!file.exists(nhdplus_dir)) { , " -o", islands_dir), ignore.stdout = T)} ) - nhdplus_path(islands_gdb) - - suppressWarnings(staged_nhdplus_islands <- stage_national_data()) + suppressWarnings(staged_nhdplus_islands <- stage_national_data(nhdplus_data = islands_gdb)) rm(gLz, xWalk) diff --git a/workspace/R/config.R b/workspace/R/config.R index 4d06f0ce56da7096418a4a79e1b858c7f42a900a..014d0127349ca49d6710ad3404b3cdf041420252 100644 --- a/workspace/R/config.R +++ b/workspace/R/config.R @@ -54,8 +54,7 @@ options(scipen = 9999) crs <- 5070 data_paths <- jsonlite::read_json(file.path("cache", "data_paths.json")) -nhdplus_path(data_paths$nhdplus_gdb) -suppressWarnings(staged_nhd <- stage_national_data()) +suppressWarnings(staged_nhd <- stage_national_data(nhdplus_data = data_paths$nhdplus_gdb)) # Defined and used broadly nhd_flowline <- "reference_flowline" diff --git a/workspace/R/utils.R b/workspace/R/utils.R index e7727d973be4676a3fa4ade12c5d182f739e5be5..cf022aa6366d61897b2864726a5a639f2b45d10a 100644 --- a/workspace/R/utils.R +++ b/workspace/R/utils.R @@ -595,3 +595,78 @@ generate_lookup_table = function(nl, } +# deprecated function from nhdplusTools -- should remove from package but here for compatibility +stage_national_data <- function(include = c("attribute", + "flowline", + "catchment"), + output_path = NULL, + nhdplus_data = NULL, + simplified = TRUE) { + + if (is.null(output_path)) { + output_path <- dirname(nhdplus_data) + warning(paste("No output path provided, using:", output_path)) + } + + if (is.null(nhdplus_data)) { + stop("must provide nhdplus_data") + } + + allow_include <- c("attribute", "flowline", "catchment") + + if (!all(include %in% allow_include)) { + stop(paste0("Got invalid include entries. Expect one or more of: ", + paste(allow_include, collapse = ", "), ".")) + } + + outlist <- list() + + if (any(c("attribute", "flowline") %in% include)) { + + out_path_attributes <- file.path(output_path, + "nhdplus_flowline_attributes.rds") + out_path_flines <- file.path(output_path, "nhdplus_flowline.rds") + + if (!(file.exists(out_path_flines) | file.exists(out_path_attributes))) { + fline <- sf::st_zm(sf::read_sf(nhdplus_data, + get_flowline_layer_name(nhdplus_data))) + } + + if ("attribute" %in% include) { + if (file.exists(out_path_attributes)) { + warning("attributes file exists") + } else { + saveRDS(sf::st_set_geometry(fline, NULL), out_path_attributes) + } + outlist["attributes"] <- out_path_attributes + } + + if ("flowline" %in% include) { + if (file.exists(out_path_flines)) { + warning("flowline file exists") + } else { + saveRDS(fline, out_path_flines) + } + outlist["flowline"] <- out_path_flines + } + } + + if (exists("fline")) rm(fline) + + if ("catchment" %in% include) { + out_path_catchments <- file.path(output_path, "nhdplus_catchment.rds") + if (file.exists(out_path_catchments)) { + warning("catchment already exists.") + } else { + + layer_name <- get_catchment_layer_name(simplified, nhdplus_data) + + saveRDS(sf::st_zm(sf::read_sf(nhdplus_data, layer_name)), + out_path_catchments) + } + outlist["catchment"] <- out_path_catchments + } + assign("national_data", outlist, envir = nhdplusTools_env) + + return(outlist) +} \ No newline at end of file