Skip to content

Commit 718eb80

Browse files
committed
Add small HOW-TOs
1 parent b6bd9c7 commit 718eb80

36 files changed

+1549
-0
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
---
2+
title: "How to convert flow direction between different encodings in GRASS"
3+
author: "Huidae Cho"
4+
date: 2022-03-25
5+
date-modified: today
6+
format:
7+
html:
8+
toc: true
9+
toc-depth: 2
10+
code-tools: true
11+
code-copy: true
12+
code-fold: false
13+
html-math-method: katex
14+
theme:
15+
- cosmo
16+
categories: [bash, beginner]
17+
linkcolor: green
18+
urlcolor: green
19+
citecolor: green
20+
highlight-style: github
21+
# engine: knitr
22+
execute:
23+
eval: false
24+
---
25+
# Introduction
26+
27+
This document shows how to convert flow direction encodings between several formats in GRASS. Each section displays:
28+
29+
- **Figure Left:** Input encoding
30+
- **Equation:** Conversion formula
31+
- **Figure Right:** Output encoding
32+
- **GRASS Command**
33+
34+
---
35+
36+
## 1. Degree to 45degree
37+
38+
::: columns
39+
:::: {.column width="20%"}
40+
![](images/degree.svg){width=80%}
41+
::::
42+
43+
:::: {.column width="60%"}
44+
$$D = \frac{d}{45}$$
45+
::::
46+
47+
:::: {.column width="20%"}
48+
![](images/45degree.svg){width=80%}
49+
::::
50+
:::
51+
```bash
52+
r.mapcalc "45degree = int(degree / 45)"
53+
```
54+
55+
## 2. 45degree to Degree
56+
57+
::: columns
58+
:::: {.column width="20%"}
59+
![](images/45degree.svg){width=80%}
60+
::::
61+
62+
:::: {.column width="60%"}
63+
$$d = 45 \times |D|$$
64+
::::
65+
66+
:::: {.column width="20%"}
67+
![](images/degree.svg){width=80%}
68+
::::
69+
:::
70+
```bash
71+
r.mapcalc "degree = abs(45degree) * 45"
72+
```
73+
74+
## 3. Degree to Power2
75+
76+
::: columns
77+
:::: {.column width="20%"}
78+
![](images/degree.svg){width=80%}
79+
::::
80+
81+
:::: {.column width="60%"}
82+
$$p = 2^{8 - \frac{d}{45}}$$
83+
::::
84+
85+
:::: {.column width="20%"}
86+
![](images/power2.svg){width=80%}
87+
::::
88+
:::
89+
```bash
90+
r.mapcalc "power2 = 2^(8 - degree / 45)"
91+
```
92+
---
93+
94+
## 4. Power2 to Degree
95+
96+
::: columns
97+
:::: {.column width="20%"}
98+
![](images/power2.svg){width=80%}
99+
::::
100+
101+
:::: {.column width="60%"}
102+
$$d = 45 \times (8 - \log_2 p)$$
103+
::::
104+
105+
:::: {.column width="20%"}
106+
![](images/degree.svg){width=80%}
107+
::::
108+
:::
109+
```bash
110+
r.mapcalc "degree = 45 * int(8 - log(power2, 2))"
111+
```
112+
113+
## 5. Degree to TauDEM
114+
115+
::: columns
116+
:::: {.column width="20%"}
117+
![](images/degree.svg){width=80%}
118+
::::
119+
120+
:::: {.column width="60%"}
121+
$$t = 1 + \left( \frac{d}{45} \mod 8 \right)$$
122+
::::
123+
124+
:::: {.column width="20%"}
125+
![](images/taudem.svg){width=80%}
126+
::::
127+
:::
128+
```bash
129+
r.mapcalc "taudem = 1 + (degree / 45) % 8"
130+
```
131+
132+
## 6. TauDEM to Degree
133+
134+
::: columns
135+
:::: {.column width="20%"}
136+
![](images/taudem.svg){width=80%}
137+
::::
138+
139+
:::: {.column width="60%"}
140+
$$d = 45 \times \left\{ \begin{array}{ll} t-1 & \text{if } t-1 > 0 \\ 8 & \text{otherwise} \end{array} \right.$$
141+
::::
142+
143+
:::: {.column width="20%"}
144+
![](images/degree.svg){width=80%}
145+
::::
146+
:::
147+
```bash
148+
r.mapcalc "degree = 45 * if(taudem - 1, taudem - 1, 8)"
149+
```
150+
151+
## 7. 45degree to Power2
152+
153+
::: columns
154+
:::: {.column width="20%"}
155+
![](images/45degree.svg){width=80%}
156+
::::
157+
158+
:::: {.column width="60%"}
159+
$$p = 2^{8 - |D|}$$
160+
::::
161+
162+
:::: {.column width="20%"}
163+
![](images/power2.svg){width=80%}
164+
::::
165+
:::
166+
```bash
167+
r.mapcalc "power2 = 2^(8 - abs(45degree))"
168+
```
169+
170+
## 8. Power2 to 45degree
171+
172+
::: columns
173+
:::: {.column width="20%"}
174+
![](images/power2.svg){width=80%}
175+
::::
176+
177+
:::: {.column width="60%"}
178+
$$D = 8 - \log_2 p$$
179+
::::
180+
181+
:::: {.column width="20%"}
182+
![](images/45degree.svg){width=80%}
183+
::::
184+
:::
185+
```bash
186+
r.mapcalc "45degree = int(8 - log(power2, 2))"
187+
```
188+
189+
## 9. 45degree to TauDEM
190+
191+
::: columns
192+
:::: {.column width="20%"}
193+
![](images/45degree.svg){width=80%}
194+
::::
195+
196+
:::: {.column width="60%"}
197+
$$t = 1 + (|D| \mod 8)$$
198+
::::
199+
200+
:::: {.column width="20%"}
201+
![](images/taudem.svg){width=80%}
202+
::::
203+
:::
204+
```bash
205+
r.mapcalc "taudem = 1 + abs(45degree) % 8"
206+
```
207+
208+
## 10. TauDEM to 45degree
209+
210+
::: columns
211+
:::: {.column width="20%"}
212+
![](images/taudem.svg){width=80%}
213+
::::
214+
215+
:::: {.column width="60%"}
216+
$$D = \left\{\begin{array}{ll} t-1 & \text{if } t-1 > 0 \\ 8 & \text{otherwise} \end{array} \right.$$
217+
::::
218+
219+
:::: {.column width="20%"}
220+
![](images/45degree.svg){width=80%}
221+
::::
222+
:::
223+
```bash
224+
r.mapcalc "45degree = if(taudem - 1, taudem - 1, 8)"
225+
```
226+
227+
## 11. Power2 to TauDEM
228+
229+
::: columns
230+
:::: {.column width="20%"}
231+
![](images/power2.svg){width=80%}
232+
::::
233+
234+
:::: {.column width="60%"}
235+
$$t = 1 + \left[ (8 - \log_2 p) \mod 8 \right]$$
236+
::::
237+
238+
:::: {.column width="20%"}
239+
![](images/taudem.svg){width=80%}
240+
::::
241+
:::
242+
```bash
243+
r.mapcalc "taudem = 1 + (8 - log(power2, 2)) % 8"
244+
```
245+
246+
## 12. TauDEM to Power2
247+
248+
::: columns
249+
:::: {.column width="20%"}
250+
![](images/taudem.svg){width=80%}
251+
::::
252+
253+
:::: {.column width="60%"}
254+
$$p = 2^{(9 - t) \mod 8}$$
255+
::::
256+
257+
:::: {.column width="20%"}
258+
![](images/power2.svg){width=80%}
259+
::::
260+
:::
261+
```bash
262+
r.mapcalc "power2 = 2^((9 - taudem) % 8)"
263+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: "How to create an empty vector map in GRASS"
3+
author: "Huidae Cho"
4+
date: 2022-03-25
5+
date-modified: today
6+
format:
7+
html:
8+
toc: true
9+
toc-depth: 2
10+
code-tools: true
11+
code-copy: true
12+
code-fold: false
13+
html-math-method: katex
14+
theme:
15+
- cosmo
16+
categories: [beginner]
17+
linkcolor: green
18+
urlcolor: green
19+
citecolor: green
20+
highlight-style: github
21+
# engine: knitr
22+
execute:
23+
eval: false
24+
---
25+
26+
27+
[`v.edit`](https://grass.osgeo.org/grass-stable/manuals/v.edit.html) can create an empty vector map, but it does not create an attribute table.
28+
29+
```bash
30+
# Create an empty vector map
31+
v.edit map=new_map tool=create
32+
# Add a new table
33+
v.db.addtable map=new_map columns="value double"
34+
```
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
title: "How to delineate stream networks in GRASS"
3+
author: "Huidae Cho"
4+
date: 2022-03-25
5+
date-modified: today
6+
format:
7+
html:
8+
toc: true
9+
toc-depth: 2
10+
code-tools: true
11+
code-copy: true
12+
code-fold: false
13+
html-math-method: katex
14+
theme:
15+
- cosmo
16+
categories: [bash, beginner]
17+
linkcolor: green
18+
urlcolor: green
19+
citecolor: green
20+
highlight-style: github
21+
# engine: knitr
22+
execute:
23+
eval: false
24+
---
25+
# 1. Identifying stream networks without calculating hydrologic parameters
26+
27+
Extract streams using the A* algorithm with a threshold value of 50,000 cells for stream generation:
28+
29+
```bash
30+
# accumulation=flow_accum is optional
31+
r.stream.extract elevation=elevation threshold=50,000 stream_vector=streams accumulation=flow_accum
32+
```
33+
34+
Figure 1 shows a stream network vector map created using [r.stream.extract](https://grass.osgeo.org/grass-stable/manuals/r.stream.extract.html). The stream vector properly represents flow directions.
35+
36+
![Figure 1: Stream networks generated using r.stream.extract. The red and blue lines represent the stream vector and flow directions, respectively. The underlying raster map is flow accumulation.](images/r.stream.extract.webp)
37+
38+
---
39+
40+
# 2. Delineating watersheds and identifying stream networks from the same source of elevation data
41+
42+
Calculate flow direction and accumulation, and delineate basins in the raster format:
43+
44+
```bash
45+
r.watershed -a elevation=elevation threshold=50,000 accumulation=flow_accum basin=basins
46+
```
47+
48+
Extract streams from the flow accumulation raster map from [r.watershed](https://grass.osgeo.org/grass-stable/manuals/r.watershed.html) so that the stream network output matches the watershed output from [r.watershed](https://grass.osgeo.org/grass-stable/manuals/r.watershed.html):
49+
50+
```bash
51+
r.stream.extract elevation=elevation accumulation=flow_accum threshold=50,000 stream_vector=streams
52+
```
53+
54+
---
55+
56+
# 3. Delineating stream networks from a flow direction map
57+
58+
[r.accumulate](https://grass.osgeo.org/grass-stable/manuals/addons/r.accumulate.html) takes a flow direction map and delineates stream networks using a threshold:
59+
60+
```bash
61+
r.accumulate direction=drain_directions threshold=50000 stream=streams
62+
```
63+
64+
---
65+
66+
# 4. How not to delineate stream networks
67+
68+
[r.watershed](https://grass.osgeo.org/grass-stable/manuals/r.watershed.html) generates a stream raster map and you may be tempted to simply convert this stream raster map to vector to identify stream networks:
69+
70+
```bash
71+
r.watershed -a elevation=elevation threshold=50,000 stream=streams
72+
r.thin input=streams output=streams_thinned
73+
r.to.vect input=streams_thinned output=streams type=line
74+
```
75+
76+
However, there are two problems with this method. First, the output stream vector map does not guarantee the correct directionality of stream paths. Second, if there are raster cell clumps in the stream raster map, stream loops may be generated or even incorrect stream paths can be obtained. An example is shown in Figure 2. Note that the stream vector does not fully agree with flow directions. Compare this output to Figure 1.
77+
78+
![Figure 2: Stream networks generated using r.watershed, r.thin, and r.to.vect. The red and blue lines represent the stream vector and flow directions, respectively. The underlying raster map is flow accumulation.](images/r.watershed.webp)

0 commit comments

Comments
 (0)