@@ -496,3 +496,88 @@ get_catchment_sets <- function(flowpath, outlets) {
496
496
list (fline_sets , cat_sets )
497
497
}
498
498
499
+ # ' Get Minimal Network
500
+ # ' @description Given a set of outlets, will generate a minimal network by
501
+ # ' calling \code{\link{aggregate_network}} and adding nhdplus attributes to the result.
502
+ # '
503
+ # ' If geometry is included with the network, it will be merged and returned.
504
+ # '
505
+ # ' @inheritParams aggregate_network
506
+ # ' @return a data.frame (potentially including an sfc list column) with
507
+ # ' attributes generated by \code{\link[nhdplusTools]{add_plus_network_attributes}}
508
+ # ' and a list column "set" containing members of each output flowpath.
509
+ # ' @importFrom nhdplusTools add_plus_network_attributes
510
+ # ' @export
511
+ # ' @examples
512
+ # ' source(system.file("extdata", "walker_data.R", package = "nhdplusTools"))
513
+ # ' fline <- walker_flowline
514
+ # '
515
+ # ' outlets <- data.frame(ID = c(5329357, 5329317, 5329365, 5329435, 5329817),
516
+ # ' type = c("outlet", "outlet", "outlet", "outlet", "outlet"))
517
+ # '
518
+ # ' #' Add toCOMID
519
+ # ' fline[["toCOMID"]] <- nhdplusTools::get_tocomid(fline)
520
+ # '
521
+ # ' # get attributes set
522
+ # ' fline <- dplyr::select(fline, ID = COMID, toID = toCOMID,
523
+ # ' levelpathid = LevelPathI, hydroseq = Hydroseq,
524
+ # ' areasqkm = AreaSqKM, lengthkm = LENGTHKM)
525
+ # '
526
+ # ' min_net <- get_minimal_network(fline, outlets)
527
+ # '
528
+ # ' plot(sf::st_geometry(fline), col = "blue")
529
+ # ' plot(sf::st_geometry(min_net), lwd = 2, add = TRUE)
530
+ # ' plot(sf::st_geometry(nhdplusTools::get_node(min_net)), add = TRUE)
531
+ # '
532
+ get_minimal_network <- function (flowpath , outlets ) {
533
+
534
+ outlets <- add_terminals(flowpath , outlets )
535
+
536
+ minimal <- hyRefactor :: aggregate_network(
537
+ flowpath , dplyr :: filter(outlets , .data $ ID %in% flowpath $ ID ),
538
+ da_thresh = NA , only_larger = TRUE )
539
+
540
+ min_net <- tidyr :: unnest_longer(drop_geometry(minimal $ fline_sets ),
541
+ col = .data $ set ) %> %
542
+ left_join(select(flowpath , .data $ ID , .data $ lengthkm ,
543
+ .data $ areasqkm , .data $ levelpathid ),
544
+ by = c(" set" = " ID" )) %> %
545
+ group_by(ID ) %> %
546
+ summarise(toID = .data $ toID [1 ],
547
+ lengthkm = sum(.data $ lengthkm ),
548
+ areasqkm = sum(.data $ areasqkm ),
549
+ outlet_levelpath = min(.data $ levelpathid )) %> %
550
+ mutate(toID = ifelse(is.na(.data $ toID ), 0 , .data $ toID )) %> %
551
+ rename(comid = .data $ ID ,
552
+ tocomid = .data $ toID ,
553
+ nameID = .data $ outlet_levelpath )%> %
554
+ add_plus_network_attributes() %> %
555
+ rename(ID = .data $ comid , toID = .data $ tocomid ,
556
+ outlet_nhdpv2_levelpath = .data $ nameID ,
557
+ arbolate_sum = .data $ weight ) %> %
558
+ left_join(select(minimal $ fline_sets , .data $ ID , .data $ set ),
559
+ by = " ID" )
560
+
561
+ if (inherits(minimal $ fline_sets , " sf" )) {
562
+ sf :: st_sf(min_net )
563
+ } else {
564
+ min_net
565
+ }
566
+ }
567
+
568
+ add_terminals <- function (flowpath , outlets ) {
569
+ flowpath_sort <- left_join(
570
+ data.frame (ID = flowpath $ ID ),
571
+ nhdplusTools :: get_sorted(flowpath [, c(" ID" , " toID" ), drop = TRUE ],
572
+ split = TRUE ), by = " ID" )
573
+
574
+ # Grab terminal paths that matter and combine with outlets..
575
+ terminal_paths <- unique(flowpath_sort $ terminalID [flowpath_sort $ ID %in% outlets $ ID ])
576
+ flowpath <- flowpath [flowpath_sort $ terminalID %in% terminal_paths , ]
577
+ terminal_paths <- flowpath $ ID [flowpath_sort $ ID %in% terminal_paths ]
578
+
579
+ rbind(outlets ,
580
+ data.frame (ID = terminal_paths ,
581
+ type = " terminal" ))
582
+ }
583
+
0 commit comments