| 1 |
<h2>DESCRIPTION</h2> |
| 2 |
|
| 3 |
The GRASS program <em>r.patch</em> allows the user to build a new |
| 4 |
raster map the size and resolution of the current region by assigning |
| 5 |
known data values from input raster maps to the cells in this region. |
| 6 |
<br> |
| 7 |
In case of overlapping input raster maps this is done by filling in |
| 8 |
"no data" cells, those that do not yet contain data, contain NULL data, |
| 9 |
or, optionally contain 0 data, with the data from the first input map. |
| 10 |
Once this is done the remaining holes are filled in by the next input map, |
| 11 |
and so on. |
| 12 |
<br> |
| 13 |
In case of adjacent input raster maps the output map contains the map |
| 14 |
mosaic. |
| 15 |
|
| 16 |
<p> |
| 17 |
Hence this command is useful for |
| 18 |
<ul> |
| 19 |
<li> making a composite raster map layer from two or more adjacent map layers,</li> |
| 20 |
<li> for filling in "holes" in a raster map layer's data (e.g., in digital |
| 21 |
elevation data), or</li> |
| 22 |
<li> for updating an older map layer with more recent data.</li> |
| 23 |
</ul> |
| 24 |
|
| 25 |
The current geographic region definition and mask settings are respected. |
| 26 |
|
| 27 |
<center> |
| 28 |
<img src="r_patch.png"> |
| 29 |
<p><em>Figure: Result of patching of two raster maps containing NULLs |
| 30 |
using the default settings.</em></p> |
| 31 |
</center> |
| 32 |
|
| 33 |
<h3>Stacking order</h3> |
| 34 |
|
| 35 |
<p>The first <em>name</em> listed in the string |
| 36 |
<b>input=</b><em>name</em>,<em>name</em>,<em>name</em>,... is the name |
| 37 |
of the first map whose data values will be used to fill in |
| 38 |
cells in the current region. |
| 39 |
Then, the second through the last input |
| 40 |
maps (..., <em>name</em>, <em>name</em>, ...) will be used, |
| 41 |
in order, to supply data values for the remaining |
| 42 |
"no data" cells (or cells with value 0 with <b>-z</b> flag). |
| 43 |
|
| 44 |
<p> |
| 45 |
In other words, the first raster map is used first and if it had some |
| 46 |
"no data" cells, then second raster map is used for these cells, then |
| 47 |
the third and so on. |
| 48 |
So the formal command line syntax can be also written as |
| 49 |
<b>input=</b><em>primary</em>,<em>secondary</em>,<em>tertiary</em>,... |
| 50 |
For two raster maps, the first one can be viewed as the primary one |
| 51 |
or the default one and the second one as the secondary one or a fallback. |
| 52 |
|
| 53 |
<center> |
| 54 |
<img src="r_patch_zeros_as_nulls.png"> |
| 55 |
<p><em>Figure: Result of patching of two raster maps using the |
| 56 |
<b>-z</b> flag to treat zeros as NULLs. Note the value 1 being preserved |
| 57 |
from the first raster while the value 6 is taken from the second raster |
| 58 |
instead of the value 0 from the first raster because zeros are replaced |
| 59 |
with the <b>-z</b> flag active.</em></p> |
| 60 |
</center> |
| 61 |
|
| 62 |
<h3>Relation to SQL COALESCE() function</h3> |
| 63 |
|
| 64 |
The module is corresponds to the SQL COALESCE() function. |
| 65 |
This function takes two or more arguments and returns |
| 66 |
a copy of its first non-NULL argument. If all arguments are NULL, |
| 67 |
the function returns NULL. |
| 68 |
|
| 69 |
<p> |
| 70 |
The <em>r.patch</em> module iterates over all cells and for each cell |
| 71 |
of the output raster map uses the first corresponding non-NULL cell |
| 72 |
in the series of the input raster maps. |
| 73 |
|
| 74 |
<h3>Example of filling areas</h3> |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Below, the raster map layer on the far left is <b>patched</b> |
| 79 |
with the middle (<em>patching</em>) raster map layer, |
| 80 |
to produce the <em>composite</em> raster map layer on the right. |
| 81 |
The example assumes zero values to be treated as NULLs (<b>-z</b> flag). |
| 82 |
|
| 83 |
<div class="code"><pre> |
| 84 |
1 1 1 0 2 2 0 0 0 0 1 1 0 0 0 0 1 1 1 1 2 2 0 0 |
| 85 |
1 1 0 2 2 2 0 0 0 0 1 1 0 0 0 0 1 1 1 2 2 2 0 0 |
| 86 |
3 3 3 3 2 2 0 0 0 0 0 0 0 0 0 0 3 3 3 3 2 2 0 0 |
| 87 |
3 3 3 3 0 0 0 0 4 4 4 4 4 4 4 4 3 3 3 3 4 4 4 4 |
| 88 |
3 3 3 0 0 0 0 0 4 4 4 4 4 4 4 4 3 3 3 4 4 4 4 4 |
| 89 |
0 0 0 0 0 0 0 0 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 |
| 90 |
</pre></div> |
| 91 |
|
| 92 |
Switching the <em>patched</em> and the <em>patching</em> raster map layers |
| 93 |
produces the following results: |
| 94 |
|
| 95 |
<div class="code"><pre> |
| 96 |
0 0 1 1 0 0 0 0 1 1 1 0 2 2 0 0 1 1 1 1 2 2 0 0 |
| 97 |
0 0 1 1 0 0 0 0 1 1 0 2 2 2 0 0 1 1 1 1 2 2 0 0 |
| 98 |
0 0 0 0 0 0 0 0 3 3 3 3 2 2 0 0 3 3 3 3 2 2 0 0 |
| 99 |
4 4 4 4 4 4 4 4 3 3 3 3 0 0 0 0 4 4 4 4 4 4 4 4 |
| 100 |
4 4 4 4 4 4 4 4 3 3 3 0 0 0 0 0 4 4 4 4 4 4 4 4 |
| 101 |
4 4 4 4 4 4 4 4 0 0 0 0 0 0 0 0 4 4 4 4 4 4 4 4 |
| 102 |
</pre></div> |
| 103 |
|
| 104 |
<h2>NOTES</h2> |
| 105 |
|
| 106 |
Frequently, this program is used to patch together adjacent map layers which |
| 107 |
have been digitized separately. The program |
| 108 |
<em><a href="v.mkgrid.html">v.mkgrid</a></em> can be used to make adjacent |
| 109 |
maps align neatly. |
| 110 |
|
| 111 |
<p>The user should check the current geographic region settings before running |
| 112 |
<em>r.patch</em>, to ensure that the region boundaries encompass all |
| 113 |
of the data desired to be included in the composite map and to ensure that the |
| 114 |
region resolution is the resolution of the desired data. To set the |
| 115 |
geographic region settings to one or several raster maps, the <em>g.region</em> |
| 116 |
program can be used: |
| 117 |
|
| 118 |
<div class="code"><pre> |
| 119 |
g.region raster=map1[,map2[,...]] |
| 120 |
</pre></div> |
| 121 |
|
| 122 |
<p> |
| 123 |
Use of <em>r.patch</em> is generally followed by use of the GRASS programs |
| 124 |
<em><a href="g.remove.html">g.remove</a></em> and |
| 125 |
<em><a href="g.rename.html">g.rename</a></em>; |
| 126 |
<em>g.remove</em> is used to remove the original (un-patched) raster map |
| 127 |
layers, while <em>g.rename</em> is used to then assign to the newly-created |
| 128 |
composite (patched) raster map layer the name of the original raster map |
| 129 |
layer. |
| 130 |
|
| 131 |
<p> |
| 132 |
<em>r.patch</em> reads the existing category label files and color tables |
| 133 |
from the <em>input</em> maps and creates these files for the patched, |
| 134 |
composite <em>output</em> map. This can be quite time consuming for |
| 135 |
certain maps, especially if there are many different category values |
| 136 |
across the patched maps. The <em>-s</em> flag allows disabling the reading |
| 137 |
and creation of these support files, meaning that the <em>output</em> |
| 138 |
map will have no category labels and no explicit color table. |
| 139 |
|
| 140 |
<p> |
| 141 |
Number of raster maps to be processed is given by the limit of the |
| 142 |
operating system. For example, both the hard and soft limits are |
| 143 |
typically 1024. The soft limit can be changed with e.g. <code>ulimit -n |
| 144 |
1500</code> (UNIX-based operating systems) but not higher than the hard |
| 145 |
limit. If it is too low, you can as superuser add an entry in |
| 146 |
|
| 147 |
<div class="code"><pre> |
| 148 |
/etc/security/limits.conf |
| 149 |
# <domain> <type> <item> <value> |
| 150 |
your_username hard nofile 1500 |
| 151 |
</pre></div> |
| 152 |
|
| 153 |
This would raise the hard limit to 1500 file. Be warned that more |
| 154 |
files open need more RAM. See also the Wiki page |
| 155 |
<a href="https://grasswiki.osgeo.org/wiki/Large_raster_data_processing">Hints for large raster data processing</a>. |
| 156 |
|
| 157 |
<p> |
| 158 |
Operating systems usually limit the length of the command line |
| 159 |
which limits the number of input raster maps user can pass to the module |
| 160 |
using the option <b>input</b>. In that case, |
| 161 |
<em><a href="r.series.html">r.series</a></em> can be used instead of |
| 162 |
<em>r.patch</em>. |
| 163 |
|
| 164 |
<h3>PERFORMANCE</h3> |
| 165 |
<p>By specifying the number of parallel processes with <b>nprocs</b> option, |
| 166 |
<em>r.patch</em> can run significantly faster, see benchmarks below. |
| 167 |
|
| 168 |
<div align="center" style="margin: 10px"> |
| 169 |
<img src="r_patch_benchmark_size.png" alt="benchmark for number of cells" border="0"> |
| 170 |
<img src="r_patch_benchmark_memory.png" alt="benchmark for memory size" border="0"> |
| 171 |
<br> |
| 172 |
<i>Figure: Benchmark on the left shows execution time for different |
| 173 |
number of cells, benchmark on the right shows execution time |
| 174 |
for different memory size for 5000x5000 raster. See benchmark scripts in source code. |
| 175 |
(Intel Core i9-10940X CPU @ 3.30GHz x 28) </i> |
| 176 |
</div> |
| 177 |
<p>To reduce the memory requirements to minimum, set option <b>memory</b> to zero. |
| 178 |
To take advantage of the parallelization, GRASS |
| 179 |
needs to compiled with OpenMP enabled. |
| 180 |
|
| 181 |
<h2>EXAMPLES</h2> |
| 182 |
|
| 183 |
<h3>Example with three maps</h3> |
| 184 |
|
| 185 |
The input are three maps called roads, water and forest. Primarily, |
| 186 |
we want to use the values from roads, then from water and if no |
| 187 |
other values are available we want to use forest. |
| 188 |
First we set the computation region assuming that the all three maps |
| 189 |
fully overlap and have the same resolution (so we can safely use the |
| 190 |
just the one without further modifications of the region). |
| 191 |
Then we perform the patching. |
| 192 |
|
| 193 |
<div class="code"><pre> |
| 194 |
g.region raster=roads |
| 195 |
r.patch input=roads,water,forest output=result |
| 196 |
</pre></div> |
| 197 |
|
| 198 |
<h3>Map mosaic example using Bash syntax</h3> |
| 199 |
|
| 200 |
Create a list of maps matching a pattern, extend the region to include them |
| 201 |
all, and patch them together to create a mosaic. Overlapping maps will be |
| 202 |
used in the order listed. |
| 203 |
|
| 204 |
<div class="code"><pre> |
| 205 |
MAPS=`g.list type=raster separator=comma pat="map_*"` |
| 206 |
g.region raster=$MAPS -p |
| 207 |
r.patch input=$MAPS output=maps_mosaic |
| 208 |
</pre></div> |
| 209 |
|
| 210 |
<h2>SEE ALSO</h2> |
| 211 |
|
| 212 |
<em> |
| 213 |
<a href="g.region.html">g.region</a>, |
| 214 |
<a href="g.remove.html">g.remove</a>, |
| 215 |
<a href="g.rename.html">g.rename</a>, |
| 216 |
<a href="r.mapcalc.html">r.mapcalc</a>, |
| 217 |
<a href="r.support.html">r.support</a>, |
| 218 |
<a href="r.series.html">r.series</a>, |
| 219 |
<a href="v.mkgrid.html">v.mkgrid</a> |
| 220 |
</em> |
| 221 |
<p> |
| 222 |
<a href="https://grasswiki.osgeo.org/wiki/Large_raster_data_processing">Hints for large raster data processing</a> |
| 223 |
|
| 224 |
<h2>AUTHORS</h2> |
| 225 |
|
| 226 |
Michael Shapiro, |
| 227 |
U.S. Army Construction Engineering Research Laboratory |
| 228 |
<br> |
| 229 |
Huidae Cho (-z flag and performance improvement) |
| 230 |
<br> |
| 231 |
Aaron Saw Min Sern (OpenMP support). |