Skip to content

Commit 1b42ab6

Browse files
committed
accept sf objects in add_hydroseq
1 parent 5417aa5 commit 1b42ab6

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

R/network_properties.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ accumulate_downstream <- function(x, id = "flowpath_id", toid = "flowpath_toid
9393
as.numeric(total[idx])
9494
}
9595

96-
9796
#' Compute and add the hydrosequence to a directed acyclic network.
9897
#'
9998
#' @param topology A data frame (or tibble) containing at least the identifier column
@@ -105,23 +104,25 @@ accumulate_downstream <- function(x, id = "flowpath_id", toid = "flowpath_toid
105104
#' Defaults to `"flowpath_toid"`.
106105
#' @param colname Character scalar. Column name to use in result.
107106
#' Defaults to `"hydroseq"`
108-
#'
107+
#'
109108
#' @returns The data frame `topology` with an additional column, named `colname`, representing the hydrosequence.
110109
#' @importFrom igraph dfs graph_from_data_frame
111110
#' @export
111+
112112
add_hydroseq <- function(topology, id = "flowpath_id", toid = "flowpath_toid", colname = "hydroseq") {
113113
# Create a _transposed_ network, where traversing the network
114114
# is equivalent to traversing the hydrological network upstream.
115115
#
116116
# This assumes the outlets of this network all connect to an
117117
# ephemeral "0" node (forming a rooted tree network).
118-
edgelist <- topology[, c(toid, id)]
118+
119+
edgelist <- as.data.frame(topology)[, c(toid, id)]
119120
names(edgelist) <- c("id", "toid")
120121
edgelist$id[is.na(edgelist$id)] <- "0"
121122

122123
# TODO: Check if multiple components exist. If they do, then
123124
# we need to add "0" edges for each component not rooted on "0".
124-
125+
125126
# Perform DFS from each terminal upstream to get a
126127
# distinct topological sort for the hydrosequence.
127128
sorted <- data.frame(
@@ -148,4 +149,6 @@ add_hydroseq <- function(topology, id = "flowpath_id", toid = "flowpath_toid", c
148149
# Arrange into input order
149150
topology[[colname]] <- result$hydroseq[match(topology[[id]], result[[id]])]
150151
topology
151-
}
152+
}
153+
154+

README.Rmd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ hfutils::as_ogr('/vsis3/lynker-spatial/hydrofabric/v2.2/conus/conus_nextgen.gpkg
8080
### Network Properties
8181

8282
```{r}
83+
## Accumulate Downstream
8384
system.time({
8485
da <- hfutils::as_ogr('/Users/mikejohnson/hydrofabric/v3.0/reference_fabric.gpkg',
8586
'flowpaths') |>
@@ -88,6 +89,17 @@ system.time({
8889
accumulate_downstream(attr = "areasqkm")})
8990
9091
head(da)
92+
93+
## Hydrosequence
94+
system.time({
95+
hs <- hfutils::as_ogr('/Users/mikejohnson/hydrofabric/v3.0/reference_fabric.gpkg',
96+
'flowpaths') |>
97+
filter(vpuid == "01") |>
98+
st_as_sf() |>
99+
add_hydroseq()
100+
})
101+
102+
head(hs$hydroseq)
91103
```
92104

93105
### Questions?

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,32 @@ hfutils::as_ogr('/vsis3/lynker-spatial/hydrofabric/v2.2/conus/conus_nextgen.gpkg
139139
### Network Properties
140140

141141
``` r
142+
## Accumulate Downstream
142143
system.time({
143144
da <- hfutils::as_ogr('/Users/mikejohnson/hydrofabric/v3.0/reference_fabric.gpkg',
144145
'flowpaths') |>
145146
filter(vpuid == "01") |>
146147
st_as_sf() |>
147148
accumulate_downstream(attr = "areasqkm")})
148149
#> user system elapsed
149-
#> 0.845 0.479 1.327
150+
#> 0.836 0.506 1.368
150151

151152
head(da)
152153
#> [1] 15.66720 11.12805 4.44600 6.52365 4.53420 335.51145
154+
155+
## Hydrosequence
156+
system.time({
157+
hs <- hfutils::as_ogr('/Users/mikejohnson/hydrofabric/v3.0/reference_fabric.gpkg',
158+
'flowpaths') |>
159+
filter(vpuid == "01") |>
160+
st_as_sf() |>
161+
add_hydroseq()
162+
})
163+
#> user system elapsed
164+
#> 1.004 0.462 1.468
165+
166+
head(hs$hydroseq)
167+
#> [1] 804 22722 22724 22723 22725 28137
153168
```
154169

155170
### Questions?

0 commit comments

Comments
 (0)