From e93b89071a43c5ead912d092bc23a1e078b1be88 Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Mon, 19 Aug 2024 07:46:19 -0500 Subject: [PATCH 1/6] example requires nhdplusTools --- R/index_points_to_lines.R | 4 ++++ man/index_points_to_waterbodies.Rd | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/R/index_points_to_lines.R b/R/index_points_to_lines.R index 3e9ae45..564f3e4 100644 --- a/R/index_points_to_lines.R +++ b/R/index_points_to_lines.R @@ -392,6 +392,8 @@ index_points_to_lines.hy <- function(x, points, #' @export #' @examples #' +#' if(require(nhdplusTools)) { +#' #' source(system.file("extdata/sample_data.R", package = "nhdplusTools")) #' #' waterbodies <- sf::st_transform( @@ -404,6 +406,8 @@ index_points_to_lines.hy <- function(x, points, #' index_points_to_waterbodies(waterbodies, points, #' search_radius = units::set_units(500, "m")) #' +#' } +#' index_points_to_waterbodies <- function(waterbodies, points, flines = NULL, search_radius = NULL) { diff --git a/man/index_points_to_waterbodies.Rd b/man/index_points_to_waterbodies.Rd index d691792..a0ff1a8 100644 --- a/man/index_points_to_waterbodies.Rd +++ b/man/index_points_to_waterbodies.Rd @@ -35,6 +35,8 @@ COMID of dominant artificial path } \examples{ +if(require(nhdplusTools)) { + source(system.file("extdata/sample_data.R", package = "nhdplusTools")) waterbodies <- sf::st_transform( @@ -48,3 +50,5 @@ index_points_to_waterbodies(waterbodies, points, search_radius = units::set_units(500, "m")) } + +} From 88dbd90da3d245cd599cedc2ba3f675d503ade85 Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Mon, 19 Aug 2024 11:20:39 -0500 Subject: [PATCH 2/6] bug in logic for suggested packages --- R/add_levelpaths.R | 3 ++- vignettes/flow-table.Rmd | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/R/add_levelpaths.R b/R/add_levelpaths.R index c37dc58..e91f814 100644 --- a/R/add_levelpaths.R +++ b/R/add_levelpaths.R @@ -146,7 +146,8 @@ add_levelpaths.hy <- function(x, name_attribute, weight_attribute, group_split() cl <- "future" - if(inherits(future::plan(), "sequential")) cl = NULL + if(!requireNamespace("future", quietly = TRUE) || + inherits(future::plan(), "sequential")) cl = NULL # reweight sets up ranked upstream paths x <- pblapply(x, reweight, override_factor = override_factor, diff --git a/vignettes/flow-table.Rmd b/vignettes/flow-table.Rmd index de4f2c3..4380e03 100644 --- a/vignettes/flow-table.Rmd +++ b/vignettes/flow-table.Rmd @@ -11,9 +11,9 @@ vignette: > ```{r setup, include = FALSE} local <- (Sys.getenv("BUILD_VIGNETTES") == "TRUE") -if(!require(nhdplusTools)) local <- FALSE +if(!requireNamespace("nhdplusTools", quietly = TRUE)) local <- FALSE -if(!local) { +if(local) { nhdplusTools::nhdplusTools_data_dir(tempdir()) } From b376ef084785b7786da8be008e7e76e2d4bd8d63 Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Tue, 20 Aug 2024 15:44:51 -0500 Subject: [PATCH 3/6] update documentation of UT navigation as not following diversions --- R/navigation_network.R | 7 +++++++ man/navigate_hydro_network.Rd | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/R/navigation_network.R b/R/navigation_network.R index 3598124..6dd9499 100644 --- a/R/navigation_network.R +++ b/R/navigation_network.R @@ -41,6 +41,13 @@ get_start_row <- function(x, id) { #' @param distance numeric distance in km to limit navigation. The first #' catchment that exceeds the provided distance is included. #' @details if only `mode` is supplied, require network attributes are displayed. +#' +#' NOTE: for "Upstream with tributaries" navigation, if a tributary emanates from +#' a diversion and is the minor path downstream of that diversion, it is not +#' followed. This can have a very large impact when a diversion between two +#' large river systems. For non-dendritic upstream with tributaries +#' network navigation, use \link{navigate_network_dfs}. +#' #' @returns vector of identifiers found along navigation #' @name navigate_hydro_network #' @export diff --git a/man/navigate_hydro_network.Rd b/man/navigate_hydro_network.Rd index 242bd3a..95ab983 100644 --- a/man/navigate_hydro_network.Rd +++ b/man/navigate_hydro_network.Rd @@ -39,6 +39,12 @@ network attributes. } \details{ if only \code{mode} is supplied, require network attributes are displayed. + +NOTE: for "Upstream with tributaries" navigation, if a tributary emanates from +a diversion and is the minor path downstream of that diversion, it is not +followed. This can have a very large impact when a diversion between two +large river systems. For non-dendritic upstream with tributaries +network navigation, use \link{navigate_network_dfs}. } \examples{ From b93834748ec3c577366467916fb3e230e3623ad3 Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Wed, 21 Aug 2024 08:58:55 -0500 Subject: [PATCH 4/6] better handling for future not installed --- R/add_divergence.R | 2 +- R/add_levelpaths.R | 4 +--- R/make_attribute_topology.R | 2 +- R/navigate_connected_paths.R | 3 ++- R/utils.R | 5 +++++ vignettes/advanced_network.Rmd | 3 ++- 6 files changed, 12 insertions(+), 7 deletions(-) diff --git a/R/add_divergence.R b/R/add_divergence.R index de4f559..cbfd9fe 100644 --- a/R/add_divergence.R +++ b/R/add_divergence.R @@ -128,7 +128,7 @@ add_divergence.hy <- function(x, coastal_outlet_ids, inland_outlet_ids, try(navigate_network_dfs(x = net, starts = i, direction = "up", reset = FALSE)) - }, net = x, cl = "future") + }, net = x, cl = future_available()) paths_df <- data.frame(id = unlist(term), paths = I(unlist(paths, diff --git a/R/add_levelpaths.R b/R/add_levelpaths.R index e91f814..77e6d2f 100644 --- a/R/add_levelpaths.R +++ b/R/add_levelpaths.R @@ -145,9 +145,7 @@ add_levelpaths.hy <- function(x, name_attribute, weight_attribute, group_by(.data$toid) |> group_split() - cl <- "future" - if(!requireNamespace("future", quietly = TRUE) || - inherits(future::plan(), "sequential")) cl = NULL + cl <- future_available() # reweight sets up ranked upstream paths x <- pblapply(x, reweight, override_factor = override_factor, diff --git a/R/make_attribute_topology.R b/R/make_attribute_topology.R index 43fd890..2daae7b 100644 --- a/R/make_attribute_topology.R +++ b/R/make_attribute_topology.R @@ -77,7 +77,7 @@ make_attribute_topology.hy <- function(x, min_distance) { which(d == min(d, na.rm = TRUE)) } - }, nodes = nodes, cl = "future") + }, nodes = nodes, cl = future_available()) # Add resulting list as a list column nodes$torow <- closest diff --git a/R/navigate_connected_paths.R b/R/navigate_connected_paths.R index f53aa44..aa12fc0 100644 --- a/R/navigate_connected_paths.R +++ b/R/navigate_connected_paths.R @@ -78,7 +78,8 @@ navigate_connected_paths <- function(x, outlets, status = FALSE) { } pairs <- t(combn(length(id_match), 2)) - paths <- pbapply(pairs, 1, get_path, all_dn = all_dn, cl = "future") + + paths <- pbapply(pairs, 1, get_path, all_dn = all_dn, cl = future_available()) connected_paths <- paths[lengths(paths) > 0] diff --git a/R/utils.R b/R/utils.R index 6a98993..fae6c78 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,8 @@ +future_available <- function() { + if(!requireNamespace("future", quietly = TRUE) || + inherits(future::plan(), "sequential")) NULL else "future" +} + get_outlet_value <- function(x) { if(inherits(x$id, "character")) { "" diff --git a/vignettes/advanced_network.Rmd b/vignettes/advanced_network.Rmd index cd92636..195da7a 100644 --- a/vignettes/advanced_network.Rmd +++ b/vignettes/advanced_network.Rmd @@ -191,7 +191,7 @@ lp <- unique(lp$levelpath) terminal_fpath <- dplyr::filter(fpath, id %in% terminal_id) gif_file <- "levelpath.gif" - +try({ gifski::save_gif({ for(i in 1:length(lp)) { lp_plot <- dplyr::filter(fpath, levelpath == lp[i]) @@ -207,6 +207,7 @@ gifski::save_gif({ }, gif_file, delay = 0.5) knitr::include_graphics(gif_file) +}) ``` # Summary From d8ec44cac6e1f89c489e22e1e27096cd923d1ed6 Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Wed, 21 Aug 2024 11:07:15 -0500 Subject: [PATCH 5/6] more suggested package cleanup --- tests/testthat/test_add_pfafstetter.R | 2 +- tests/testthat/test_get_hydro_location.R | 2 +- tests/testthat/test_index.R | 15 ++++++++------- vignettes/flow-table.Rmd | 1 + 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test_add_pfafstetter.R b/tests/testthat/test_add_pfafstetter.R index f444f1a..5692338 100644 --- a/tests/testthat/test_add_pfafstetter.R +++ b/tests/testthat/test_add_pfafstetter.R @@ -1,6 +1,6 @@ test_that("get_pfaf", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") work_dir <- nhdplusTools::nhdplusTools_data_dir() diff --git a/tests/testthat/test_get_hydro_location.R b/tests/testthat/test_get_hydro_location.R index 7171093..6300de2 100644 --- a/tests/testthat/test_get_hydro_location.R +++ b/tests/testthat/test_get_hydro_location.R @@ -1,5 +1,5 @@ test_that("get location", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") source(system.file("extdata", "sample_flines.R", package = "nhdplusTools")) diff --git a/tests/testthat/test_index.R b/tests/testthat/test_index.R index 5b80776..c7951ef 100644 --- a/tests/testthat/test_index.R +++ b/tests/testthat/test_index.R @@ -1,5 +1,5 @@ test_that("index to waterbodies", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") source(system.file("extdata/sample_data.R", package = "nhdplusTools")) @@ -52,7 +52,7 @@ test_that("index to waterbodies", { sr <- units::set_units(0.1, "degrees") -if(require(nhdplusTools)) { +if(requireNamespace("nhdplusTools", quietly = TRUE)) { source(system.file("extdata", "sample_flines.R", package = "nhdplusTools")) sample_flines <- sf::st_cast(sample_flines, "LINESTRING", warn = FALSE) @@ -60,7 +60,8 @@ if(require(nhdplusTools)) { test_that("point indexing to nearest existing node works as expected", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") + if(!requireNamespace("lwgeom", quietly = TRUE)) skip("Missing lwgeom") flines_in <- sample_flines @@ -113,7 +114,7 @@ test_that("point indexing to nearest existing node works as expected", { test_that("point indexing works without measures", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") flines_in <- sample_flines @@ -133,8 +134,8 @@ test_that("point indexing works without measures", { test_that("point indexing to for multiple points works", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") - + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") + if(!requireNamespace("lwgeom", quietly = TRUE)) skip("Missing lwgeom") flines_in <- sample_flines flines_in <- sf::st_transform(flines_in, 4269) @@ -227,7 +228,7 @@ test_that("no duplicates when using precision", { test_that("disambiguate", { - if(!require(nhdplusTools)) skip("Missing nhdplusTools") + if(!requireNamespace("nhdplusTools", quietly = TRUE)) skip("Missing nhdplusTools") source(system.file("extdata", "sample_flines.R", package = "nhdplusTools")) diff --git a/vignettes/flow-table.Rmd b/vignettes/flow-table.Rmd index 4380e03..c5ec82e 100644 --- a/vignettes/flow-table.Rmd +++ b/vignettes/flow-table.Rmd @@ -12,6 +12,7 @@ vignette: > local <- (Sys.getenv("BUILD_VIGNETTES") == "TRUE") if(!requireNamespace("nhdplusTools", quietly = TRUE)) local <- FALSE +if(!requireNamespace("mapview", quietly = TRUE)) local <- FALSE if(local) { nhdplusTools::nhdplusTools_data_dir(tempdir()) From f54769716a5ba6050b7a49295971244fa3f3af08 Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Wed, 21 Aug 2024 11:32:42 -0500 Subject: [PATCH 6/6] UT navigation follow divergences now --- R/navigation_network.R | 24 ++++++++++++++++-------- man/navigate_hydro_network.Rd | 8 ++++---- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/R/navigation_network.R b/R/navigation_network.R index 6dd9499..eda2110 100644 --- a/R/navigation_network.R +++ b/R/navigation_network.R @@ -3,7 +3,7 @@ required_atts_navigate <- function(mode, distance) { DM = c(id, levelpath, dn_levelpath, topo_sort, dn_topo_sort), UT = c(id, levelpath, - topo_sort, dn_topo_sort), + topo_sort, dn_topo_sort, dn_minor_topo_sort), DD = c(id, levelpath, dn_levelpath, topo_sort, dn_topo_sort, dn_minor_topo_sort)) @@ -43,10 +43,10 @@ get_start_row <- function(x, id) { #' @details if only `mode` is supplied, require network attributes are displayed. #' #' NOTE: for "Upstream with tributaries" navigation, if a tributary emanates from -#' a diversion and is the minor path downstream of that diversion, it is not -#' followed. This can have a very large impact when a diversion between two -#' large river systems. For non-dendritic upstream with tributaries -#' network navigation, use \link{navigate_network_dfs}. +#' a diversion and is the minor path downstream of that diversion, it will be +#' included. This can have a very large impact when a diversion between two +#' large river systems. To strictly follow the dendritic network, set the +#' "dn_minor_topo_sort" attribute to all 0 in x. #' #' @returns vector of identifiers found along navigation #' @name navigate_hydro_network @@ -138,10 +138,18 @@ get_UT <- function(x, id, distance) { x <- filter(x, .data$id %in% all) - filter(x, .data$pathlength_km <= stop_pathlength_km)$id - } else { - all + all <- filter(x, .data$pathlength_km <= stop_pathlength_km)$id } + + incoming_div <- filter(x, !id %in% all & + dn_minor_topo_sort %in% x$topo_sort[x$id %in% all]) + + extra <- lapply(incoming_div$id, \(i) get_UT(x, i, distance)) + + all <- c(all, unique(unlist(extra))) + + return(all) + } private_get_UT <- function(x, id) { diff --git a/man/navigate_hydro_network.Rd b/man/navigate_hydro_network.Rd index 95ab983..ce3fc68 100644 --- a/man/navigate_hydro_network.Rd +++ b/man/navigate_hydro_network.Rd @@ -41,10 +41,10 @@ network attributes. if only \code{mode} is supplied, require network attributes are displayed. NOTE: for "Upstream with tributaries" navigation, if a tributary emanates from -a diversion and is the minor path downstream of that diversion, it is not -followed. This can have a very large impact when a diversion between two -large river systems. For non-dendritic upstream with tributaries -network navigation, use \link{navigate_network_dfs}. +a diversion and is the minor path downstream of that diversion, it will be +included. This can have a very large impact when a diversion between two +large river systems. To strictly follow the dendritic network, set the +"dn_minor_topo_sort" attribute to all 0 in x. } \examples{