Skip to content

Commit

Permalink
ensure vpuid is optional in hydrofabric layers
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejohnson51 committed Dec 3, 2024
1 parent 7dcccdb commit f42ad05
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion R/find_origin.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ find_origin <- function(

origin <-
origin |>
dplyr::select(id, toid, vpuid, topo, hydroseq) |>
dplyr::select(any_of(c('id', 'toid', 'vpuid', 'topo', 'hydroseq'))) |>
dplyr::distinct() |>
dplyr::collect() |>
dplyr::slice_min(hydroseq, with_ties = TRUE)
Expand Down
23 changes: 17 additions & 6 deletions R/query_subset.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ query_subset <- function(query) {
id = identifier,
type = class(identifier)
)

network <- query_source_layer(query$source, "network") |>
dplyr::filter(vpuid == !!origin$vpuid) |>

network <- query_source_layer(query$source, "network")

if(!is.null(suppressWarnings(origin$vpuid))){
network = dplyr::filter(network, vpuid == !!origin$vpuid)
}

network <- network |>
dplyr::select(dplyr::any_of(c("id", "toid", "divide_id", "poi_id"))) |>
dplyr::distinct() |>
dplyr::collect()

topology <- suppressWarnings(nhdplusTools::get_sorted(network, outlets = origin$toid))

topology$toid[nrow(topology)] <- NA

all_identifiers <-
Expand All @@ -29,7 +35,7 @@ query_subset <- function(query) {
all_identifiers <-
all_identifiers[!is.na(all_identifiers)]

query$vpuid <- origin$vpuid
query$vpuid <- suppressWarnings({ origin$vpuid })
query$requested <- all_identifiers

query_extract(query)
Expand All @@ -40,7 +46,9 @@ query_subset <- function(query) {
#' @returns A list of hydrofabric layers, or the path to the sink of the query
#' @note This should be called from query_subset().
#' @keywords internal
#'
query_extract <- function(query) {

layers <- query_get_layers(query)
result <- new.env(size = length(layers))
outfile <- query_get_sink(query)
Expand All @@ -55,8 +63,11 @@ query_extract <- function(query) {
next
}

layer_data <- dplyr::filter(layer_data, vpuid == !!query$vpuid)
variables <- c("COMID", "FEATUREID", "divide_id", "link", "to", "id", "toid", "ID", "poi_id")
if(!is.null(suppressWarnings(query$vpuid))){
layer_data <- dplyr::filter(layer_data, vpuid == !!query$vpuid)
}

variables <- c("COMID", "FEATUREID", "divide_id", "link", "to", "id", "toid", "ID", "poi_id")

if ("poi_id" %in% colnames(layer_data)) {
layer_data <- dplyr::mutate(layer_data, poi_id = as.character(poi_id))
Expand Down

0 comments on commit f42ad05

Please sign in to comment.