-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
120 lines (90 loc) · 3.06 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/",
eval = FALSE
)
```
# streamnet
[![CRAN status](https://www.r-pkg.org/badges/version/streamnet)](https://cran.r-project.org/package=streamnet)
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Travis-CI Build Status](https://travis-ci.org/jsta/streamnet.svg?branch=master)](https://travis-ci.org/jsta/streamnet)
[![DOI](https://zenodo.org/badge/104792308.svg)](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)
```