forked from OSGeo/grass-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGRASS_movement.qmd
More file actions
572 lines (378 loc) · 17.4 KB
/
GRASS_movement.qmd
File metadata and controls
572 lines (378 loc) · 17.4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
---
title: "Modeling Movement in GRASS"
author: "Michael Barton & Anna Petrasova"
date: 2025-02-10
date-modified: today
lightbox: true
engine: knitter
image: img_movement/thumbnail.webp
format:
html:
embed-resources: true
toc: true
code-tools: true
code-copy: true
code-fold: false
page-layout: article
categories: [intermediate, advanced, GUI, raster, cost surface, least cost path]
description: Generating a cumulative cost surface and least cost path with *r.walk* and *r.path* to model movement by walking across a landscape.
execute:
eval: false
copyright:
holder: Michael Barton & Anna Petrasova
year: 2025
funding: "Creation of this tutorial was supported in part by US National Science Foundation grant FAIN 2303651."
---
GRASS has sophisticated tools to model movement across terrain, including [r.cost](https://grass.osgeo.org/grass-stable/manuals/r.cost.html), [r.walk](https://grass.osgeo.org/grass-stable/manuals/r.walk.html), [r.drain](https://grass.osgeo.org/grass-stable/manuals/r.drain.html), and [r.path](https://grass.osgeo.org/grass-stable/manuals/r.path.html). In this tutorial, we will use ***r.walk*** and ***r.path*** to determine the best walking path to reach a destination, like a hospital.
::: {.callout-note title="Dataset"}
This tutorial uses one of the standard GRASS sample data sets: flagstaff_arizona_usa. We will refer to place names in that data set, but it can be completed with any of the [standard sample data sets](https://grass.osgeo.org/download/data/) for any region--for example, the [North Carolina data set](https://grass.osgeo.org/sampledata/north_carolina/nc_basic_spm_grass7.zip). We will use the Flagstaff *elevation* DEM, the *hospitals* vector points file (any other vector point file will serve), and the *landuse* raster map.
This tutorial is designed so that you can complete it using the **GRASS GUI**, GRASS commands from the **console or terminal**, or using GRASS commands in a **Jupyter Notebook** environment.
:::
::: {.callout-note title="Don't know how to get started?"}
If you are not sure how to get started with GRASS using its graphical user interface or using Python, checkout the tutorials [Get started with GRASS GUI](../get_started/fast_track.qmd) and [Get started with GRASS & Python in Jupyter Notebooks](../get_started/fast_track_grass_and_python.qmd).
:::
## What is a cost surface?
::::: grid
::: g-col-6
- A **cost surface** is a raster in which each cell represents the “cost” or difficulty to move across landscape.
- A **cumulative cost surface** shows the total accumulated cost of moving from a starting point to a location. Cumulative cost surfaces are also used to find a **least cost path** between a location and the starting point.
- GRASS [r.walk](https://grass.osgeo.org/grass-stable/manuals/r.walk.html) tool generates a cumulative cost surface using Naismith's rule for walking times where each cell has the value in **seconds** of the time it takes to walk from the start point to that cell.
:::
::: g-col-6
{fig-align="right" width="100%"}
:::
:::::
# Modeling movement with a cumulative cost surface
## Overview
::::: grid
::: g-col-6
1. Start with a **DEM of elevation** for topography to determine movement costs.
2. Create a **friction map** with a value of 0 (or other values for additional movement costs).
3. Select **start point(s)**.
4. Create a **cost surface**.
5. A **least cost** path can then be calculated between any point on the cost surface and the start point.
:::
::: g-col-6

:::
:::::
## Friction map
- The GRASS [map calculator](https://grass.osgeo.org/grass-stable/manuals/r.mapcalc.html) lets you create a map, alter a map, or combine maps using **map algebra**. The map calculator can be used from the GUI or as a command.
- Before you make a friction map, make sure the [**computational region**](https://grass.osgeo.org/grass-stable/manuals/g.region.html) matches the elevation map.
- Create the *friction0* map with 0 in every cell.
:::::: {.panel-tabset group="language"}
#### GUI
::::: grid
::: g-col-6
1. Add the *elevation* raster to the Layer Manager.
2. Right click *elevation* in Layer Manager.
3. Select **Set computational region from selected map**.
4. Open the **Raster Map Calculator** from the top toolbar ("abacus" icon).
5. Fill in the output map name as *friction0* and enter 0 in the expression field.
6. Press Run.
:::
::: g-col-6

:::
:::::
#### Command line
1. Set the region to match the elevation map.
2. Create a friction map with a value of 0 in all cells that matches the extent and resolution of the elevation map.
```{bash}
g.region raster=elevation
r.mapcalc "friction0 = 0"
```
#### Python
1. Set the region to match the elevation map.
2. Create a friction map with a value of 0 in all cells that matches the extent and resolution of the elevation map.
```{python}
gs.run_command("g.region", raster="elevation")
gs.mapcalc("friction0 = 0")
```
::::::
## Choose a start point
Now that we have a DEM map for terrain and a friction map, all we need for a cumulative cost surface is a start point: the point from which to calculate movement costs.
- A start point can be a vector point, a raster cell, or even a pair of coordinates.
- A cost surface in *r.walk* can have multiple start points.
- We’ll make a start point from the Flagstaff Medical Center, found in the *hospitals* vector points file.
:::::: {.panel-tabset group="language"}
#### GUI
::::: grid
::: g-col-6
Use the **Attribute Table Manager** for *hospitals*:
1. Display *hospitals* map by adding it to the Layer Manager from Data Catalog.
2. Right click and open the Attribute Table Manager.
3. Select "Flagstaff Medical Center" record.
4. Right click and select **Extract selected features** from the context menu.
5. Name the new map *FMC*.
:::
::: g-col-6

{width="60%"}
:::
:::::
#### Command line
Use the v.extract tool to create a vector point map named *FMC* from the *hospitals* map.
```{bash}
v.extract input=hospitals type=point where="FACILITY_N = 'FLAGSTAFF MEDICAL CENTER'" output=FMC
```
#### Python
Use the v.extract tool to create a vector point map named *FMC* from the *hospitals* map.
```{python}
gs.run_command("v.extract",
input="hospitals",
type="point",
where="FACILITY_N = 'FLAGSTAFF MEDICAL CENTER'",
output="FMC")
```
::::::
- Use the *d.vect* tool by double clicking FMC in the layer manager to display the point with a color and size to see it better

## Generate the cumulative cost surface {#generate-cumulative-cost-surface}
::::::::::::: {.panel-tabset group="language"}
#### GUI
Use the *r.walk* tool from the **Raster/Terrain Analysis** menu.
::::: grid
::: g-col-6
1. Enter the **elevation map** ( *elevation* ), **friction map** ( *friction0* ), and **name of the cost surface** to create (FMC_cost_seconds).
{fig-align="left" width="100%"}
:::
::: g-col-6
2. Enter the name of a **directions map** ( *FMC_directions* ) to use for creating a least cost path.
{fig-align="left" width="100%"}
:::
:::::
::::: grid
::: g-col-6
3. Enter the **start point** ( *FMC* ).
{fig-align="left" width="100%"}
:::
::: g-col-6
4. Optional: control the **spatial extent** of cost surface.

:::
:::::
::::: grid
::: g-col-6
5. Optional: adjust **Movement parameter** settings.
{fig-align="left" width="100%"}
:::
::: g-col-6
6. Recommended: select **knight's move** for calculating cost and direction.
{fig-align="left" width="100%"}
:::
:::::
::: {.callout-note title="Tip"}
Click the "copy" button to copy the GRASS command. You can save it in a text file for later reuse or to document your work.
:::
#### Command line
- Use the r.walk command to generate the cumulative cost surface.
```{bash}
r.walk elevation=elevation friction=friction0 output=FMC_cost_seconds outdir=FMC_directions start_points=FMC -k
```
#### Python
- Use the r.walk command to generate the cumulative cost surface.
```{python}
gs.run_command("r.walk",
elevation="elevation",
friction="friction0",
output="FMC_cost_seconds",
outdir="FMC_directions",
start_points="FMC",
flags="k")
```
:::::::::::::
## Cumulative cost surface map
Each raster cell value in the cost surface is the time in seconds to walk from FMC to that cell over the terrain of the Flagstaff DEM.

::: {.callout-note title="Tip"}
Tip: You can display the cumulative cost surface over a shaded relief map in the **layer manager** using the [d.shade](https://grass.osgeo.org/grass-stable/manuals/d.shade.html) tool and a relief map of *elevation* made with [r.relief](https://grass.osgeo.org/grass-stable/manuals/d.shade.html).
:::
## Movement across a cumulative cost surface {#movement-across-cost-surface}
You can transform the walking time in seconds to hours by dividing the map by 3600 using the map calculator.
:::::: {.panel-tabset group="language"}
#### GUI
::::: grid
::: g-col-6
1. Open the map calculator.
2. Enter the name of the new map of walking time in hours: *FMC_cost_hours*.
3. Enter FMC_cost_seconds / 3600 into the expressions field.
4. Press Run.
:::
::: g-col-6
{fig-align="left" width="100%"}
:::
:::::
#### Command line
```{bash}
r.mapcalc "FMC_cost_hours = FMC_cost_seconds / 3600"
```
#### Python
```{python}
gs.mapcalc("FMC_cost_hours = FMC_cost_seconds / 3600")
```
::::::
------------------------------------------------------------------------
::::: grid
::: g-col-6
- We can query or filter this hourly cumulative cost surface to areas of equivalent walking time.
- For example, in the **layer manager** we can used the *d.rast* tool to show the area within a 2 hour walk of FMC and then set the opacity of the cost surface to 50% to see the underlying terrain.
:::
::: g-col-6

:::
:::::

# Least cost paths {#least-cost-path}
## Overview
- We can also plot a **least cost path** (LCP), which is the least costly (shortest time) route between any point on the cumulative cost surface and FMC.
- Imagine a stranded hiker NE of Flagstaff who has to walk to FMC.
- What path would take the least time?

## Generating a least cost path
To create a LCP in GRASS, we will use [r.path](https://grass.osgeo.org/grass-stable/manuals/r.path.html) (also under the Raster/Terrain analysis menu).
:::::::: {.panel-tabset group="language"}
#### GUI
::::: grid
::: g-col-6
1. Input the **directions map** (*FMC_directions*) we also made when we created the cumulative cost surface.

:::
::: g-col-6
2. Input the **coordinates of the hiker** as the starting point for the LCP. (We could also use a pre-defined vector point).

:::
:::::
:::: grid
::: g-col-6
3. Specify the **name of the output vector path map** (*LCP_cumulative*).

:::
::::
#### Command line
```{bash}
r.path input=FMC_directions format=auto vector_path=LCP_cumulative start_coordinates=477476,13914951
```
#### Python
```{python}
gs.run_command("r.path",
input="FMC_directions",
format="auto",
vector_path="LCP_cumulative",
start_coordinates=[477476, 13914951])
```
::::::::
## Least cost path generated
Here is the LCP result.

# Adding a friction map to movement costs
## Overview
- A **friction map** can be used to incorporate other factors than just terrain into creating a cumulative cost surface and modeling movement.
- The value of each cell in a friction map raster is the amount of walking time, in seconds/meter, *in addition to* the time needed because of terrain.
- We can **reclassify** the *landuse* map to create a friction map of the amount of extra time it would take to walk through different kinds of land cover.

## Reclassifying *landuse* to create a friction map
A standard walking speed across flat terrain is about 5 km/hr = 0.72 sec/m.
We might then estimate that it would take an additional
- 3 sec/m to walk through dense pinyon-juniper woodland
- 1 sec/m to cross conifer forest
- 2 sec/m to wander through urban Flagstaff
- 5 sec/m to clamber over lava fields
- 10 sec/m to try and cross water
We can create this friction map by reclassifying the *landuse* map using [*r.reclass*](https://grass.osgeo.org/grass-stable/manuals/r.reclass.html) to assign new friction values to the existing land cover categories.
:::::: {.panel-tabset group="language"}
## GUI
The [r.reclass](https://grass.osgeo.org/grass-stable/manuals/r.reclass.html) tool is found under the Raster/Change category menu.
::::: grid
::: g-col-6
1. Enter *landuse* for the raster to be reclassified.
2. Enter *friction_reclassified* for the output reclassified map.
3. Enter the reclass rules directly in the text box or from a saved text file. Use and \* symbol to represent everything not covered by the specific reclass rules.
> 11 90 95 = 10 water
> 21 thru 24 = 2 urban
> 31 = 5 lava
> 41 thru 43 = 1 conifer forest
> 52 = 3 pinyon juniper woodland
> \* = 0 no friction
:::
::: g-col-6

:::
:::::
## Command line
```{bash}
r.reclass input=landuse output=friction_landcover rules=- << EOF
11 90 95 = 10 water
21 thru 24 = 2 urban
31 = 5 lava
41 thru 43 = 1 conifer forest
52 = 3 pinyon juniper woodland
* = 0 no friction
EOF
```
## Python
```{python}
rules = """\
11 90 95 = 10 water
21 thru 24 = 2 urban
31 = 5 lava
41 thru 43 = 1 conifer forest
52 = 3 pinyon juniper woodland
* = 0 no friction
"""
gs.write_command("r.reclass",
input="landuse",
output="friction_landcover",
rules="-",
stdin=rules)
```
::::::
This creates a new friction map named *friction_landcover*.

## Modifying a cumulative cost surface with a friction map
We can now create a new cumulative cost surface using this new friction map and convert it from seconds to hours, as we did before.
::: {.panel-tabset group="language"}
#### GUI
1. Follow the procedures in the [Generate the cumulative cost surface
section](#generate-cumulative-cost-surface) and substitute the new *friction_landcover* map for the *friction0* map used previously.
2. Convert the cumulative cost surface to hours instead of seconds following the procedures in the [Movement across a cumulative cost surface section]({#movement-across-cost-surface}).
#### Command line
```{bash}
r.walk elevation=elevation friction=friction_landcover output=FMC_vegcost_seconds outdir=FMC_vegcost_directions start_points=FMC -k
r.mapcalc "FMC_vegcost_hours = FMC_vegcost_seconds / 3600"
```
#### Python
```{python}
gs.run_command("r.walk",
elevation="elevation",
friction="friction_landcover",
output="FMC_vegcost_seconds",
outdir="FMC_vegcost_directions",
start_points="FMC",
flags="k")
gs.mapcalc("FMC_vegcost_hours = FMC_vegcost_seconds / 3600")
```
:::

## Least cost paths and friction
We can create a new LCP from the stranded hiker to FMC over terrain where land cover also affects the cost of movement.
::: {.panel-tabset group="language"}
#### GUI
1. Follow the procedures in the [Least cost path section](#least-cost-path), substituting the new direction map made along with the cumulative cost surface with land cover friction map.
2. Give this new LCP the name *LCP_vegcost* to distinguish from the previous LCP.
#### Command line
```{bash}
r.path input=FMC_vegcost_directions format=auto vector_path=LCP_vegcost start_coordinates=477476,13914951
```
#### Python
```{python}
gs.run_command("r.path",
input="FMC_vegcost_directions",
format="auto",
vector_path="LCP_vegcost",
start_coordinates=[477476, 13914951])
```
:::
- It takes more time to reach FMC if the hiker has to navigate dense vegetation as well as terrain.
- In the map below, terrain colored by land cover, the original LCP with terrain only is shown by the blue line. The LCP with a land cover friction map is shown by the heavier yellow line.
