Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve robustness of navigate_network_dfs #48

Merged
merged 1 commit into from
Oct 9, 2024
Merged

improve robustness of navigate_network_dfs #48

merged 1 commit into from
Oct 9, 2024

Conversation

dblodgett-usgs
Copy link
Collaborator

@dblodgett-usgs dblodgett-usgs commented Oct 9, 2024

library(hydroloom)

# https://github.com/internetofwater/ref_gages
gages <- sf::read_sf("https://www.hydroshare.org/resource/3295a17b4cc24d34bd6a5c5aaf753c50/data/contents/ref_gages.gpkg")

vaa <- nhdplusTools::get_vaa()

# remove coastlines
vaa <- vaa[vaa$fcode != 56600,]

# prep the graph data structure
vaa_fn <- hydroloom::to_flownetwork(vaa)

vaa_g <- hydroloom::make_index_ids(vaa_fn)
vaa_g_up <- hydroloom::make_fromids(vaa_g, return_list = TRUE, 
                                    upmain = dplyr::distinct(
                                      dplyr::select(vaa_fn, 
                                                    dplyr::all_of(c("id", "upmain")))))
provider_id <- "05060000"

gage <- gages[gages$provider_id == provider_id,]
start <- gage$nhdpv2_COMID

trim_to_len <- function(x, start, vaa, dist) {
  x <- unlist(x)
  
  cur <- vaa$pathlength[vaa$comid == start]
  
  out <- vaa[vaa$comid %in% x, c("comid", "pathlength")]
  
  out$comid[abs(out$pathlength - cur) <= dist]  
}

um <- hydroloom::navigate_network_dfs(vaa_g_up, start, direction = "upmain") |>
  trim_to_len(start, vaa, 150)

ut <- hydroloom::navigate_network_dfs(vaa_g_up, start, "up") |>
  trim_to_len(start, vaa, 150)

dm <- hydroloom::navigate_network_dfs(vaa_g, start, "downmain") |>
  trim_to_len(start, vaa, 150)

network_gages_opt <- gages[gages$nhdpv2_COMID %in% c(um, ut, dm),]

# can do much the same with the base network but it's a little slower if you are going to iterate
um <- hydroloom::navigate_hydro_network(vaa, gage$nhdpv2_COMID, mode = "UM", distance = 150)
ut <- hydroloom::navigate_hydro_network(vaa, gage$nhdpv2_COMID, mode = "UT", distance = 150)
dm <- hydroloom::navigate_hydro_network(vaa, gage$nhdpv2_COMID, mode = "DM", distance = 150)

network_gages <- gages[gages$nhdpv2_COMID %in% c(um, ut, dm),]

@dblodgett-usgs dblodgett-usgs merged commit 03092c4 into main Oct 9, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant