https://github.com/jsta/streamnet
Morphology analysis of stream networks 🍃
https://github.com/jsta/streamnet
geomorphology limnology
Last synced: 10 months ago
JSON representation
Morphology analysis of stream networks 🍃
- Host: GitHub
- URL: https://github.com/jsta/streamnet
- Owner: jsta
- Created: 2017-09-25T19:21:43.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-07-09T19:35:17.000Z (almost 4 years ago)
- Last Synced: 2025-08-01T00:25:36.214Z (11 months ago)
- Topics: geomorphology, limnology
- Language: R
- Homepage: https://jsta.github.io/streamnet/
- Size: 2.34 MB
- Stars: 6
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/",
eval = FALSE
)
```
# streamnet
[](https://cran.r-project.org/package=streamnet)
[](https://www.tidyverse.org/lifecycle/#experimental)
[](https://travis-ci.org/jsta/streamnet)
[](https://zenodo.org/badge/latestdoi/104792308)
Morphology analysis of stream networks
## Installation
You can install streamnet from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("jsta/streamnet")
```
In addition, many functions require a system installation of [GRASS 7](https://grass.osgeo.org/) along with the [v.stream.order](https://grass.osgeo.org/grass74/manuals/addons/v.stream.order.html) extension.
There is a helper function to install `v.stream.order` at `streamnet:::install_grass_extensions`.
## Usage
### Calculate morphology metrics
```{r grass_flag, echo=FALSE}
Sys.setenv(GRASS_VERBOSE = 0)
```
```{r setup, message=FALSE, eval=TRUE, warning=FALSE}
library(sf)
library(nhdR)
library(streamnet)
library(ggplot2)
library(raster)
```
```{r calc_metrics, message=FALSE, warning=FALSE, eval=FALSE}
data(nhd_sub_lines)
data(nhd_sub_lakes)
outlet_reach <- terminal_reaches(network = nhd_sub_lines,
approve_all_dl = TRUE, quiet = TRUE)
outlet_point <- st_cast(st_line_sample(outlet_reach, sample = 1), "POINT")
ggplot() +
geom_sf(data = nhd_sub_lines) +
geom_sf(data = outlet_point, aes(color = "red")) +
scale_color_manual(labels = "outlet", values = "red") +
labs(colour = "") + theme_minimal()
calc_metrics(nhd_sub_lines, nhd_sub_lakes)
```
### Simplify stream networks
```{r simplify_networks, eval=TRUE}
data(nhd_sub_lines)
# Combine(dissolve) adjacent reaches with no junctions
nhd_sub_simple <- simplify_network(nhd_sub_lines)
avg_link_length(nhd_sub_simple)
avg_link_length(nhd_sub_lines)
```
### Round-trip igraph and sf lines
```{r igraph_v_sf, eval=TRUE}
tree <- create_reversed_tree(15)
class(tree)
plot(tree)
tree_sf <- igraph2sf(tree)
plot(tree_sf)
```
### Create synthetic stream networks
```{r gen_dla, eval=FALSE}
# Diffusion limited aggregation
dt <- sim_dla()
viz_dla(dt, which.max(dt))
```
```{r show_dla, echo=FALSE, eval=TRUE}
knitr::include_graphics("man/figures/show_dla.png")
```
```{r viz_bin_raster, eval=TRUE}
# Generate from a binary raster
foo <- matrix(0, ncol = 9, nrow = 9)
foo[1:4,3] <- 1
foo[5,4] <- 1
foo[6:9,5] <- 1
foo <- raster(foo, xmn = 1, xmx = 9, ymn = 1, ymx = 9)
origin <- which.min(apply(
which(as.matrix(flip(foo, "y")) == 1, arr.ind = TRUE), 1, sum))
res <- raster2network(foo, origin)
par(mfrow = c(1, 2))
plot(foo)
plot(foo); plot(res, add = TRUE)
```