Takes the full-resolution reconciled flowlines (as passed to
aggregate_to_distribution), splits any that exceed split_flines_meters,
assigns each segment its parent aggregated flowpath_id and divide_id
via the network lookup table in input_gpkg, remaps topology so the
resulting segments form a valid directed graph, and attributes each segment
with total and incremental contributing area derived from a
flow-accumulation raster (FAC).
If events is supplied (the full event set from
rf_generate_events_from_hydrolocations_fast), each event is assigned the
aggregated flowpath_id and divide_id of the reconciled flowline it
belongs to, and written as an events layer to input_gpkg.
Usage
add_flowlines(
flowlines,
input_gpkg,
split_flines_meters = 2000,
fac,
fallback_fac = NULL,
poi_events = NULL,
junction_events = NULL
)Arguments
- flowlines
An
sfobject of full-resolution reconciled flowlines (the data passed toaggregate_to_distribution). Must have integer columnsIDandtoID, and a character columnmember_flowpath_id(comma-separated NHD IDs per reconciled flowline).- input_gpkg
Path to a GeoPackage produced by
aggregate_to_distribution(must contain anetworktable). Theflowlines(and optionallyevents) layer will be written here.- split_flines_meters
Numeric. Maximum individual segment length in metres. Default
2000.- fac
Character path to a flow-accumulation GeoTIFF, or a terra::SpatRaster already in memory.
- fallback_fac
Optional character path (or
NULL) to a fallback FAC raster used whenfacreturns NA after the standard 2-cell buffer fill. Values filled from the fallback are taggedfac_sourcenaming that grid in the output. Whenfallback_facresolves to the same file asfac(path equality, i.e. a domain configured with the fallback grid as its primary), the fallback lookup is skipped automatically. DefaultNULL.- poi_events
Optional
sfobject of snapped POI events returned byrefactor_flowlines(i.e.out$events). Must contain areconciled_idcolumn (integer reconciled flowline ID) andpoi_id. These events carry geometries repositioned onto the reconciled network.- junction_events
Optional
sfobject of junction events fromrf_generate_events_from_hydrolocations_fast(i.e.filter(events, event_kind == "junction")). Must contain aflowpath_idcolumn with original NHD flowline IDs.
Value
Invisibly, the sf data frame of flowline segments. The layer
flowlines is written/overwritten in input_gpkg. If events is
provided, an events layer is also written/overwritten.
Details
Workflow:
Read the
networktable frominput_gpkgand expand its comma-separatedflowline_idcolumn to build a per-ID lookup: reconciled flowline ID -> aggregatedflowpath_id,flowpath_toid,divide_id.Split
flowlinessegments that exceedsplit_flines_metersusingsplit_flowlines.Assign
flowpath_id/divide_idto each segment via the lookup.Remap topology: segments within the same parent flowpath chain upstream-to-downstream; the terminal segment of each chain points to the first segment of the downstream chain (by
flowpath_toid).Extract the FAC value at the downstream endpoint of each segment to derive
tot_drainage_areasqkm(FAC * cell area in km2).Compute
areasqkm(incremental) astot_drainage_areasqkmof this segment minus the maximumtot_drainage_areasqkmof immediate upstream segments. Head segments with no upstream neighbour use their own total.If
poi_eventsand/orjunction_eventsare provided, each is attributed with the aggregatedflowpath_idanddivide_id, then the combined set is written as aneventslayer ininput_gpkg. POI events are mapped via theirreconciled_id(integer reconciled flowline ID fromrefactor_flowlines) directly through the network lookup. Junction events are mapped via their NHDflowpath_idcolumn throughflowlines$member_flowpath_idto the reconciled integer ID.Write flowline segments as a
flowlineslayer toinput_gpkgand return thesfobject invisibly.
Examples
if (FALSE) { # \dontrun{
reconciled <- sf::st_read("reconciled.gpkg", "reconciled")
add_flowlines(flowlines = reconciled,
input_gpkg = "out_vpu01.gpkg",
split_flines_meters = 2000,
fac = "fac.tif")
} # }