A complete refactor workflow is presented in vignette("refactor_nhdplus"). This article is used to describe the split-flowlines step in detail. The split flowlines functionality is intended to support either splitting flowlines that are too long at arbitrary locations or splitting flowlines at specific locations such as stream gages or particular model-output locations.

We'll use the sample "New Hope" watershed included in the package and prepare it for processing with nhdplusTools::prepare_nhdplus()

source(system.file("extdata", "new_hope_data.R", package = "hyRefactor"))

new_hope_flowline <- dplyr::right_join(dplyr::select(new_hope_flowline, COMID, REACHCODE, FromMeas, ToMeas), 
                                suppressWarnings(nhdplusTools::prepare_nhdplus(new_hope_flowline, 
                                                                 0, 0, 0, FALSE, warn = FALSE)), 
                                by = "COMID")

nhdplusTools::plot_nhdplus(outlets = new_hope_flowline$COMID, nhdplus_data = file.path(extdata, "new_hope.gpkg"), flowline_only = TRUE)
split <- hyRefactor::split_flowlines(suppressWarnings(sf::st_cast(sf::st_transform(new_hope_flowline, 5070), "LINESTRING")), 
                         max_length = 2000, events = new_hope_events)

split_outlets <- sf::st_sf(sf::st_drop_geometry(split), geom = sf::st_geometry(nhdplusTools::get_node(split, position = "end")))

plt <- function(x) sf::st_geometry(sf::st_transform(x, 3857))

nhdplusTools::plot_nhdplus(outlets = new_hope_flowline$COMID, nhdplus_data = file.path(extdata, "new_hope.gpkg"), flowline_only = TRUE)
plot(plt(split), add = TRUE)
plot(plt(split_outlets), add = TRUE, cex = 2)
plot(plt(new_hope_events), add = TRUE, cex = 1, col = "red")


dblodgett-usgs/hyRefactor documentation built on Aug. 25, 2023, 9:09 p.m.