library(hydroloom) library(dplyr) local <- (Sys.getenv("BUILD_VIGNETTES") == "TRUE") knitr::opts_chunk$set( collapse = TRUE, warning = FALSE, comment = "#>", fig.width=6, fig.height=6, fig.align="center", eval=local ) oldoption <- options(scipen = 9999)
The terms used below are derived from concepts of graph theory, the HY_Features conceptual data model, and the NHDPlus data model. Many of the concepts here are also presented in: Mainstems: A logical data model implementing mainstem and drainage basin feature types based on WaterML2 Part 3: HY Features concepts. This article formed an early draft of this paper: Generating a reference flow network with improved connectivity to support durable data integration and reproducibility in the coterminous US.
The NHDPlus data model, which the attributes described here are based on, includes many 'value added attributes' (VAA). This vignette discusses a core set of VAAs that hydroloom
can create from readily available hydrographic inputs. The vignette begins with a background needed to understand what these attributes are, and then demonstrates how to create them based on some sample input data. These attributes are documented in the NHDPlus manual, and every effort has been made to faithfully implement their meaning.
While the hydroloom
package contains other functions to generate network attributes, (e.g. add_pfafstetter()
for Pfafstetter codes and add_streamorder()
for stream orders) this vignette focuses on the network attributes from the NHDPlus data model that revolve around the topo_sort and levelpath. In the NHDPlus data model, topo_sort is referred to as hydrosequence but it is functionally equivalent to a topological sort and is referred to as topo_sort in hydroloom
.
x <- hy(sf::read_sf(system.file("extdata/new_hope.gpkg", package = "hydroloom"))) x <- add_toids(x) x <- sort_network(x) x$topo_sort <- seq(nrow(x), 1) oldpar <- par(mar = c(0, 0, 0, 0)) plot(sf::st_geometry(x), col = NA) plot(x["topo_sort"], add = TRUE, lwd = 2) par(oldpar)
The NHDPlus data model includes an attribute called hydrosequence that is functionally a topological sort of the flowline network. It provides an integer identifier guaranteed to decrease in the downstream direction. For flowlines that are not connected by a single direction navigation (e.g. parallel tributaries) the topo_sort has no significance. However, when two flowlines have a direct navigation, the downstream flowline will always have the smaller topo_sort. hydroloom
supports creation of topo_sort with the sort_network()
function.
It is hard to overstate the importance of topo_sort, as any function that requires understanding upstream-downstream relationships requires a sorted version of the flowline network. In the NHDPlus data model, a dendritic edge-list topology is stored in the form of a topo_sort and 'down topo_sort' attribute. The equivalent is available in hydroloom
, but does not use the to topo_sort convention, preferring the primary identifier (id) and an accompanying toid to store a dendritic edge list.
x <- hy(sf::read_sf(system.file("extdata/new_hope.gpkg", package = "hydroloom"))) x <- add_toids(x) lp <- data.frame(lp = sort(unique(x$levelpath))) lp$lpid <- seq_len(nrow(lp)) x <- dplyr::left_join(x, lp, by = c("levelpath" = "lp")) oldpar <- par(mar = c(0, 0, 0, 0)) plot(sf::st_geometry(x), col = NA) plot(x["lpid"], add = TRUE, lwd = 2) par(oldpar)
A level path is derived from "stream level" which assigns an integer value to mainstem rivers from outlet up the network (see NHDPlus documentation for more). Rivers terminating to the ocean are given level 1 and this level extends all the way to the headwaters. Rivers terminating in level 1 rivers are given level 2, and so on.
"Stream leveling" is the process of establishing uniquely identified "level paths" through a stream network. This is accomplished with a set of rules that determine which tributary should be considered dominant at every confluence to establish the "mainstem rivers" for each "drainage basin" in a network. hydroloom
supports creation of stream level with the add_streamlevel()
function, and the creation of level path with add_levelpaths()
. hydroloom
adopts the convention used in NHDPlus, to assign the levelpath as the topo_sort of the path's outlet.
See Mainstems: A logical data model implementing mainstem and drainage basin feature types based on WaterML2 Part 3: HY Features concepts for an in depth discussion of these concepts.
A number of additional attributes can be derived once levelpath
and topo_sort
are established. These include: (note that precise attribute names have not been used here)
Creating levelpath and topo_sort identifiers requires a set of base attributes that include:
fromnode
/ tonode
or id
/ toid
: from and to nodes can be used to generate an edge to edge flowline topology.
length
: a length is required for each flowline in the network to determine a flow distance, and if using the arbolate sum for stream leveling.
area
: the local drainage area of each flowline is useful in many contexts but is primarily used to calculate total drainage area.
weight_attribute
: a weight metric is required for stream leveling to determine the dominant upstream flowline. In the NHD, the arbolate sum is used. However, alternative metrics (e.g. total drainage area) can be used instead.
name_attribute
: Many times, it is preferable to follow a consistently named path rather than a strict physical weight when stream leveling. In these cases a name_attribute
can be provided.
divergence
: in order to create a [many:1] upstream to downstream topology, diverted paths must be labeled as such. This attribute is 0 for normal (already [many:1]) connections, 1 for the main path through a divergence, and 2 for any diverted path.
To illustrate the above concepts and attributes, we'll start with the "New Hope" demo data included in the hydroloom
package and add a toid attribute based on the edge-node topology included in the data.
# Import data x <- hy(sf::read_sf(system.file("extdata/new_hope.gpkg", package = "hydroloom"))) # Strip the data back to the required base attributes fpath <- hydroloom::add_toids( dplyr::select(x, id, fromnode, tonode, divergence, feature_type, da_sqkm, length_km, GNIS_ID) ) # Print head(fpath <- select(sf::st_cast(fpath, "LINESTRING"), -tonode, -fromnode, -divergence, -feature_type))
After removing attributes used to generate the toid
attribute, we have a id
and toid
relation representing the connectivity of the network as well as attributes required to generate a sorted network with sort_network()
head(fpath <- sort_network(fpath, split = TRUE))
The sort_network()
function sorts the flowlines such that headwaters come first and the terminal flowline last. Additionally, it produces a terminal_id
representing the outlet id
of the network. If multiple terminal networks had been provided, the terminal_id
would allow us to group the data by complete sub networks (a convenient parallelization scheme).
In contrast to NHDPlus, where the terminal path is identified by the topo_sort
id
of the outlet flowline (meaning the outlet of a level path is left to a user to generate), hydroloom
uses the more stable primary id
(COMID in NHDPlus) for identifying outlets to allow the topo_sort attribute to be generated and discarded as needed.
We can visualize this sorting by assigning a temporary "topo_sort" value to the sorted network, row wise. Here, we assign the first rows in the sorted set to large values and the last rows to small values in line with the topo_sort order convention in NHDPlus.
fpath['topo_sort'] <- seq(nrow(fpath), 1) plot(fpath['topo_sort'], key.pos = NULL)
To generate a levelpath attribute, a "physical" weight is needed to determine the upstream mainstem at confluences. For this example, we'll follow the NHD convention and calculate the arbolate sum explicitly. The add_levelpaths()
function will add arbolate sum internally if no weight is explicitly defined.
# Rename and compute weight fpath$arbolatesum <- accumulate_downstream( dplyr::select(fpath, id, toid, length_km), "length_km") plot(sf::st_geometry(fpath), lwd = fpath$arbolatesum / 10)
A name_attribute
identifier can also be provided to override the physical weight_attribute
when a "smaller" river has the same name. There is an optional override_factor
parameter that signifies if the physical weight is override_factor
times (e.g. 5) larger on an unnamed or differently named upstream path, the physical weight will be used in favor of the named id
.
# Get levelpaths fpath <- add_levelpaths(fpath, name_attribute = "GNIS_ID",weight_attribute = "arbolatesum", status = FALSE, override_factor = 5) # Print head(fpath) plot(fpath["topo_sort"], key.pos = NULL, reset = FALSE) plot(fpath["levelpath"], key.pos = NULL)
Finally, let's visualize these advanced VAAs!
In the below animation, each newly added level path is shown in blue, with the outlet flowline colored in red. Remembering that sort_network()
sorts flowlines such that headwaters come first and the terminal flowlines last we invert the network so that level paths fill in from outlet to head waters. For clarity, only levelpaths with more than 2 flowlines are shown.
# Invert plotting order fpath <- dplyr::arrange(fpath, topo_sort) # Level Paths with more then 2 flowlines lp <- dplyr::group_by(fpath, levelpath) %>% dplyr::filter(n() > 2) # Unique Level Path ID lp <- unique(lp$levelpath) # Terminal flowline 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]) outlet_plot <- dplyr::filter(lp_plot, id %in% terminal_id) plot(sf::st_geometry(fpath), lwd = 0.5, col = "grey") plot(sf::st_geometry(terminal_fpath), lwd = 3, col = "red", add = TRUE) plot(sf::st_geometry(dplyr::filter(fpath, levelpath %in% lp[1:i])), add = TRUE) plot(sf::st_geometry(lp_plot), col = "blue", add = TRUE) plot(sf::st_geometry(outlet_plot), col = "red", lwd = 1.5, add = TRUE) } }, gif_file, delay = 0.5) knitr::include_graphics(gif_file) })
This entire process of sorting the network and building topo_sort, levelpath, and derivative variables is wrapped in the nhdplusTools add_plus_network_attributes
function to provide performance and simplicity. It supports parallelization and will print status updates in the case when the input network is very large. add_plus_network_attributes
returns NHDPlus attribute names (truncated per shapefile rules as is done in the NHDPlus database).
The attributes described in this vignette assume that diversions in the network have zero flow such that diversions are treated as if they were headwaters. See the vignette("non-dendritic")
for information about attributes that include connected divergences.
options(oldoption) if(Sys.getenv("BUILD_VIGNETTES") != "TRUE") { unlink(work_dir, recursive = TRUE) }
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.