| 1 |
<h2>DESCRIPTION</h2> |
| 2 |
|
| 3 |
<em>r.path</em> traces a path from starting points following input |
| 4 |
directions. Such a movement direction map can be generated with |
| 5 |
<em><a href="r.walk.html">r.walk</a></em>, |
| 6 |
<em><a href="r.cost.html">r.cost</a></em>, |
| 7 |
<em><a href="r.slope.aspect.html">r.slope.aspect</a></em>, |
| 8 |
<em><a href="r.watershed.html">r.watershed</a></em>, or |
| 9 |
<em><a href="r.fill.dir.html">r.fill.dir</a></em>, |
| 10 |
provided that the direction is in degrees, measured counterclockwise |
| 11 |
from east. |
| 12 |
|
| 13 |
<p> |
| 14 |
Alternatively, bitmask-encoded directions can be provided where each |
| 15 |
bit position corresponds to a specific neighbour. A path will continue |
| 16 |
to all neighbours with their bit set. This means a path can split and |
| 17 |
merge. Such bitmasked directions can be created with the <b>-b</b> |
| 18 |
flag of <em><a href="r.cost.html">r.cost</a></em> and |
| 19 |
<em><a href="r.walk.html">r.walk</a></em>. |
| 20 |
|
| 21 |
<div class="code"><pre> |
| 22 |
Direction encoding for neighbors of x |
| 23 |
|
| 24 |
135 90 45 7 8 1 |
| 25 |
180 x 360 6 x 2 |
| 26 |
225 270 315 5 4 3 |
| 27 |
|
| 28 |
degrees bit positions |
| 29 |
CCW from East |
| 30 |
</pre></div> |
| 31 |
|
| 32 |
A path stops when the direction is zero or negative, indicating a stop |
| 33 |
point or outlet. |
| 34 |
<p> |
| 35 |
The <b>output</b> raster map will show one or more least-cost paths |
| 36 |
between each user-provided location(s) and the target points (direction |
| 37 |
≤ 0). By default, the <b>output</b> will be an integer CELL map with |
| 38 |
the id of the start points along the least cost path, and null cells elsewhere. |
| 39 |
|
| 40 |
<p> |
| 41 |
With the <b>-c</b> (<em>copy</em>) flag, the values raster map cell values are |
| 42 |
copied verbatim along the path. With the <b>-a</b> (<em>accumulate</em>) |
| 43 |
flag, the accumulated cell value from the starting point up to the current |
| 44 |
cell is written on output. With either the <b>-c</b> or the <b>-a</b> flags, the |
| 45 |
<b>raster_path</b> map is created with the same cell type as |
| 46 |
the <b>values</b> raster map (integer, float or double). With |
| 47 |
the <b>-n</b> (<em>number</em>) flag, the cells are numbered |
| 48 |
consecutively from the starting point to the final point. |
| 49 |
The <b>-c</b>, <b>-a</b>, and <b>-n</b> flags are mutually |
| 50 |
incompatible. |
| 51 |
|
| 52 |
|
| 53 |
<p> |
| 54 |
The <b>start_coordinates</b> parameter consists of map E and N grid |
| 55 |
coordinates of a starting point. Each x,y pair is the easting and |
| 56 |
northing (respectively) of a starting point from which a path will be |
| 57 |
traced following <b>input</b> directions. The <b>start_points</b> |
| 58 |
parameter can take multiple vector maps containing additional starting |
| 59 |
points. |
| 60 |
|
| 61 |
<h2>NOTES</h2> |
| 62 |
|
| 63 |
The directions are recorded as degrees CCW from East, the Knight's move |
| 64 |
of r.cost and r.walk is considered: |
| 65 |
<div class="code"><pre> |
| 66 |
112.5 67.5 |
| 67 |
157.5 135 90 45 22.5 |
| 68 |
180 x 0 |
| 69 |
202.5 225 270 315 337.5 |
| 70 |
247.5 292.5 |
| 71 |
</pre></div> |
| 72 |
i.e. a cell with the value 135 means the next cell is to the North-West, |
| 73 |
and a cell with the value 157.5 means that the next cell is to the |
| 74 |
West-North-West. |
| 75 |
|
| 76 |
<h2>EXAMPLES</h2> |
| 77 |
|
| 78 |
<h3>Hydrological path</h3> |
| 79 |
|
| 80 |
We are using the full North Carolina sample dataset. |
| 81 |
First we create the two points from a text file using |
| 82 |
<em><a href="v.in.ascii.html">v.in.ascii</a></em> module |
| 83 |
(here the text file is CSV and we are using unix here-file syntax |
| 84 |
with EOF, in GUI just enter the values directly for the parameter input): |
| 85 |
|
| 86 |
<div class="code"><pre> |
| 87 |
v.in.ascii input=- output=start format=point separator=comma <<EOF |
| 88 |
638667.15686275,220610.29411765 |
| 89 |
638610.78431373,220223.03921569 |
| 90 |
EOF |
| 91 |
</pre></div> |
| 92 |
|
| 93 |
We need to supply a direction raster map to the <em>r.path</em> module. |
| 94 |
To get these directions, we use the |
| 95 |
<em><a href="r.watershed.html">r.watershed</a></em> module: |
| 96 |
|
| 97 |
<div class="code"><pre> |
| 98 |
r.watershed elevation=elev_lid792_1m accumulation=accum drainage=drain_dir |
| 99 |
</pre></div> |
| 100 |
|
| 101 |
The directions are categorical and we convert them to degrees using |
| 102 |
raster algebra: |
| 103 |
|
| 104 |
<div class="code"><pre> |
| 105 |
r.mapcalc "drain_deg = if(drain_dir != 0, 45. * abs(drain_dir), null())" |
| 106 |
</pre></div> |
| 107 |
|
| 108 |
Now we are ready to extract the drainage paths starting at the two points. |
| 109 |
|
| 110 |
<div class="code"><pre> |
| 111 |
r.path input=drain_deg raster_path=drain_path vector_path=drain_path start_points=start |
| 112 |
</pre></div> |
| 113 |
|
| 114 |
Before we visualize the result, we set a color table for the elevation |
| 115 |
we are using and create a shaded relief map: |
| 116 |
|
| 117 |
<div class="code"><pre> |
| 118 |
r.colors map=elev_lid792_1m color=elevation |
| 119 |
r.relief input=elev_lid792_1m output=relief |
| 120 |
</pre></div> |
| 121 |
|
| 122 |
We visualize the input and output data: |
| 123 |
|
| 124 |
<div class="code"><pre> |
| 125 |
d.shade shade=relief color=elev_lid792_1m |
| 126 |
d.vect map=drain_path color=0:0:61 width=4 legend_label="drainage paths" |
| 127 |
d.vect map=start color=none fill_color=224:0:0 icon=basic/circle size=15 legend_label=origins |
| 128 |
d.legend.vect -b |
| 129 |
</pre></div> |
| 130 |
|
| 131 |
<div align="center"> |
| 132 |
<a href="r_path_with_r_watershed_direction.png"><img src="r_path_with_r_watershed_direction.png" alt="drainage using r.watershed" width="300" height="280"></a> |
| 133 |
<br> |
| 134 |
<i>Figure: Drainage paths from two points where directions from |
| 135 |
r.watershed were used</i> |
| 136 |
</div> |
| 137 |
|
| 138 |
<h3>Least-cost path</h3> |
| 139 |
|
| 140 |
We compute bitmask encoded movement directions using <em>r.walk:</em> |
| 141 |
<div class="code"><pre> |
| 142 |
g.region swwake_30m -p |
| 143 |
|
| 144 |
# create friction map based on land cover |
| 145 |
r.recode input=landclass96 output=friction rules=- << EOF |
| 146 |
1:3:0.1:0.1 |
| 147 |
4:5:10.:10. |
| 148 |
6:6:1000.0:1000.0 |
| 149 |
7:7:0.3:0.3 |
| 150 |
EOF |
| 151 |
|
| 152 |
# without Knight's move |
| 153 |
r.walk -b elevation=elev_ned_30m friction=friction output=walkcost \ |
| 154 |
outdir=walkdir start_coordinates=635576,216485 |
| 155 |
|
| 156 |
r.path input=walkdir start_coordinates=640206,222795 \ |
| 157 |
raster_path=walkpath vector_path=walkpath |
| 158 |
|
| 159 |
# with Knight's move |
| 160 |
r.walk -b -k elevation=elev_ned_30m friction=friction output=walkcost_k \ |
| 161 |
outdir=walkdir_k start_coordinates=635576,216485 |
| 162 |
|
| 163 |
r.path input=walkdir_k start_coordinates=640206,222795 \ |
| 164 |
raster_path=walkpath_k vector_path=walkpath_k |
| 165 |
|
| 166 |
# without Knight's move and without bitmask encoding (single direction) |
| 167 |
r.walk elevation=elev_ned_30m friction=friction output=walkcost_s \ |
| 168 |
outdir=walkdir_s start_coordinates=635576,216485 |
| 169 |
|
| 170 |
r.path input=walkdir_s start_coordinates=640206,222795 \ |
| 171 |
raster_path=walkpath_s vector_path=walkpath_s |
| 172 |
</pre></div> |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
The extracted least-cost path splits and merges on the way from |
| 181 |
the start point to the stop point (start point for r.walk). Note the |
| 182 |
gaps in the raster path when using the Knight's move. |
| 183 |
<div class="code"><pre> |
| 184 |
</pre></div> |
| 185 |
|
| 186 |
<div align="center"> |
| 187 |
<a href="r_path_with_bitmask.png"> |
| 188 |
<img src="r_path_with_bitmask.png" alt="least cost path using bitmask" width="600" height="274"> |
| 189 |
</a> |
| 190 |
<br> |
| 191 |
<i>Figure: Comparison of shortest paths using single directions and |
| 192 |
multiple bitmask encoded directions without and with Knight's move</i> |
| 193 |
</div> |
| 194 |
|
| 195 |
<h2>SEE ALSO</h2> |
| 196 |
|
| 197 |
<em> |
| 198 |
<a href="g.region.html">g.region</a>, |
| 199 |
<a href="r.basins.fill.html">r.basins.fill</a>, |
| 200 |
<a href="r.cost.html">r.cost</a>, |
| 201 |
<a href="r.fill.dir.html">r.fill.dir</a>, |
| 202 |
<a href="r.mapcalc.html">r.mapcalc</a>, |
| 203 |
<a href="r.recode.html">r.recode</a>, |
| 204 |
<a href="r.terraflow.html">r.terraflow</a>, |
| 205 |
<a href="r.walk.html">r.walk</a>, |
| 206 |
<a href="r.watershed.html">r.watershed</a> |
| 207 |
</em> |
| 208 |
|
| 209 |
<h2>AUTHOR</h2> |
| 210 |
|
| 211 |
Markus Metz<br> |
| 212 |
Multiple path directions sponsored by <a href="https://www.mundialis.de">mundialis</a> |