DOI-USGS / hydroloom

hydrologic geospatial fabric creation tools. See official repository here: https://code.usgs.gov/water/hydroloom
https://doi-usgs.github.io/hydroloom/
Creative Commons Zero v1.0 Universal
25 stars 2 forks source link

add_topo_sort is not unique when the network has divergences #36

Closed dblodgett-usgs closed 3 months ago

dblodgett-usgs commented 3 months ago
  par(mar = c(0, 0, 0, 0))
data <- nhdplusTools::plot_nhdplus(list(8317415))
#> Zoom set to: 12


library(hydroloom)

network <- hydroloom::hy(data$flowline, clean = TRUE)

base_network <- dplyr::select(network, id, divergence, fromnode, tonode)

base_network <- hydroloom::add_toids(base_network, return_dendritic = FALSE)

# this is problematic

base_network <- hydroloom::add_topo_sort(base_network)

base_network$topo_sort[base_network$id == 8317409] < base_network$topo_sort[base_network$id == 8317403]
#> [1] TRUE TRUE

base_network$topo_sort[base_network$id == 8317403]
#> [1] 7 6

# the implementation should be

base_network <- hydroloom::sort_network(base_network)

base_network <- dplyr::left_join(base_network,
                                 data.frame(id = unique(base_network$id),
                                            hydroseq = seq(length(unique(base_network$id)), 1)),
                                 by = "id")

base_network$hydroseq[base_network$id == 8317409] < base_network$hydroseq[base_network$id == 8317403]
#> [1] TRUE TRUE

base_network$hydroseq[base_network$id == 8317403]
#> [1] 6 6

Created on 2024-07-30 with reprex v2.1.1