Skip to content

Commit

Permalink
remove duplicate outlets
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Aug 22, 2023
1 parent 9ae1de5 commit a4c3fa1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 11 additions & 1 deletion R/aggregate_network.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ make_outlets_valid <- function(outlets, flowpath,
}
}

otl <- distinct(otl)

# Need to check that a "next down tributary" in the outlet set has a break along the
# main stem that each outlet contributes to.
otl <- left_join(otl, select(drop_geometry(flowpath),
Expand Down Expand Up @@ -338,6 +340,14 @@ make_outlets_valid <- function(outlets, flowpath,
otl <- get_outlets(outlets, lps)
}
}

# deduplicate outlets.
outlets <- distinct(outlets) %>%
group_by(.data$ID) %>%
filter(!(n() > 1 & # this removes outlets that duplicate terminals.
# they can be added in the above outlets check.
.data$type == "outlet")) %>%
ungroup()

return(outlets)
}
Expand Down Expand Up @@ -485,7 +495,7 @@ get_catchment_sets <- function(flowpath, outlets) {
cut(path,
breaks = c(0, breaks$id),
labels = c(breaks$set)))

cat_sets$set[as.integer(names(paths))] <-
lapply(names(paths), function(x) {
my_combine(cat_sets$set[as.integer(x)][[1]],
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_aggregate_catchments.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ aggregate_lookup_cat <- dplyr::select(sf::st_drop_geometry(aggregated$cat_sets),
expect_true(all(walker_fline_rec$ID %in% unlist(aggregate_lookup_cat$set)),
"all input ids should be in catchment output")

expect_equal(aggregated_cat$toID, get_id(c(NA, "5329843", "5329339.1", "5329303")), info = "Expect these toIDs")
expect_true(all(aggregated_cat$toID %in% get_id(c(NA, "5329843", "5329339.1", "5329303"))), info = "Expect these toIDs")

expect_true(all(aggregated_cat$toID[!is.na(aggregated_cat$toID)] %in% aggregated_cat$ID),
"All not NA toIDs should be in IDs")

### Make sure we can run split_catchment_divide on aggregate output.
crs <- st_crs(walker_fdr)
aggregated_cat <- st_transform(aggregated_cat, crs)
aggregated_fline <- st_transform(aggregated_fline, crs)
crs <- sf::st_crs(walker_fdr)
aggregated_cat <- sf::st_transform(aggregated_cat, crs)
aggregated_fline <- sf::st_transform(aggregated_fline, crs)

aggregated_cat <- aggregated_cat[match(aggregated_fline$ID, aggregated_cat$ID), ]

Expand Down

0 comments on commit a4c3fa1

Please sign in to comment.