Skip to content

Commit 74576ab

Browse files
committedJan 15, 2022
get minimal network function
1 parent dfc8855 commit 74576ab

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed
 

‎NAMESPACE

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export(collapse_flowlines)
99
export(download_elev)
1010
export(download_fdr_fac)
1111
export(flowpaths_to_linestrings)
12+
export(get_minimal_network)
1213
export(map_outlet_ids)
1314
export(reconcile_catchment_divides)
1415
export(reconcile_collapsed_flowlines)
@@ -43,6 +44,7 @@ importFrom(httr,write_disk)
4344
importFrom(magrittr,"%>%")
4445
importFrom(methods,is)
4546
importFrom(methods,slot)
47+
importFrom(nhdplusTools,add_plus_network_attributes)
4648
importFrom(nhdplusTools,get_node)
4749
importFrom(nhdplusTools,get_vaa)
4850
importFrom(nhdplusTools,prepare_nhdplus)

‎R/aggregate_network.R

+85
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,88 @@ get_catchment_sets <- function(flowpath, outlets) {
496496
list(fline_sets, cat_sets)
497497
}
498498

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+

‎man/get_minimal_network.Rd

+49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test_aggregate_network.R

+27
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,30 @@ test_that("example runs", {
2828

2929
expect_error(aggregate_network(fline, outlets), "Terminal paths must have an NA or 0 toID")
3030
})
31+
32+
test_that("minimal network", {
33+
source(system.file("extdata", "walker_data.R", package = "nhdplusTools"))
34+
fline <- walker_flowline
35+
36+
outlets <- data.frame(ID = c(5329357, 5329317, 5329365, 5329435, 5329817),
37+
type = c("outlet", "outlet", "outlet", "outlet", "outlet"))
38+
39+
#' Add toCOMID
40+
fline[["toCOMID"]] <- nhdplusTools::get_tocomid(fline)
41+
42+
fline <- dplyr::select(fline, ID = COMID, toID = toCOMID,
43+
levelpathid = LevelPathI, hydroseq = Hydroseq,
44+
areasqkm = AreaSqKM, lengthkm = LENGTHKM)
45+
46+
min_net <- get_minimal_network(fline, outlets)
47+
48+
expect_equal(nrow(min_net), 8)
49+
50+
expect_s3_class(min_net, "sf")
51+
52+
min_net <- get_minimal_network(sf::st_drop_geometry(fline), outlets)
53+
54+
expect_s3_class(min_net, c("tbl_df","tbl","data.frame"), exact = TRUE)
55+
56+
expect_true(all(outlets$ID %in% min_net$ID))
57+
})

0 commit comments

Comments
 (0)