Skip to content

Commit

Permalink
bug in sort_network when duplicated rows due to attributes fixes #52
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Jan 25, 2025
1 parent 421f360 commit 5de8531
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: hydroloom
Title: Utilities to Weave Hydrologic Fabrics
Version: 1.1.0
Version: 1.1.1
Authors@R:
person(given = "David",
family = "Blodgett",
Expand All @@ -14,7 +14,7 @@ Suggests: testthat, nhdplusTools, future, lwgeom, future.apply, knitr, gifski, m
License: CC0
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Config/testthat/parallel: true
Config/testthat/edition: 3
VignetteBuilder: knitr
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
hydroloom 1.1.1
==========

- fix bug with sort_network when duplicate entries are in the extended attributes -- #52

hydroloom 1.1.0
==========

Expand Down
8 changes: 5 additions & 3 deletions R/make_index_ids.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ make_index_ids.hy <- function(x, long_form = FALSE) {
Run check_hy_graph to identify issues.")
}

vars <- c("id", "toid")
if("downmain" %in% names(x)) vars <- c(vars, "downmain")

x <- select(x, all_of(vars))

x <- distinct(x)

out_val <- get_outlet_value(x)
Expand All @@ -66,9 +71,6 @@ make_index_ids.hy <- function(x, long_form = FALSE) {
out_rename <- copy(out)
setnames(out_rename, old = "indid", new = "toindid")

vars <- c("id", "toid")
if("downmain" %in% names(x)) vars <- c(vars, "downmain")

out <- merge(merge(as.data.table(x)[, vars, with = FALSE],
out, by = "id", all.x = TRUE, sort = FALSE),
out_rename,
Expand Down
1 change: 1 addition & 0 deletions R/sort_network.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ sort_network.data.frame <- function(x, split = FALSE, outlets = NULL) {
#' @export
#'
sort_network.hy <- function(x, split = FALSE, outlets = NULL) {

hy_g <- get_hyg(x, add = TRUE, id = id)

x <- select(st_drop_geometry(x), id, toid, everything())
Expand Down
Binary file added tests/testthat/data/sort_network_dups.rds
Binary file not shown.
14 changes: 14 additions & 0 deletions tests/testthat/test_sort_network.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,17 @@ test_that("add_topo_sort deals with diversions", {
unique(base_network$topo_sort[base_network$id == 8317403]))

})

test_that("duplicated attributes", {
network <- readRDS("data/sort_network_dups.rds")

dedup <- dplyr::distinct(dplyr::select(network, id, toid))

sorted <- sort_network(network)

sorted_dedup <- sort_network(dedup)

sorted_dedup_2 <- dplyr::distinct(dplyr::select(sorted, id, toid))

expect_equal(sorted_dedup, sorted_dedup_2)
})

0 comments on commit 5de8531

Please sign in to comment.