Debian Codegraph @

raster/r.neighbors/r.neighbors.html

raw
1 <h2>DESCRIPTION</h2>
2
3 <em><b>r.neighbors</b></em> looks at each cell in a raster input
4 map, and examines the values assigned to the
5 cells in some user-defined "neighborhood" around it. It
6 outputs a new raster map layer in which each cell is
7 assigned a value that is some (user-specified)
8 function of the values in that cell's neighborhood. For
9 example, each cell in the output layer might be assigned a
10 value equal to the average of the values
11 appearing in its 3 x 3 cell "neighborhood" in the input
12 layer. Note that the centre cell is also included in the calculation.
13
14 <center>
15 <img src="r_neighbors.png">
16 <p><em>Figure: Illustration for an 3x3 sum neighborhood</em></p>
17 </center>
18
19 <h3>OPTIONS</h3>
20
21 The user must specify the names of the raster map layers to
22 be used for <b>input</b> and <b>output</b>, the
23 <b>method</b> used to analyze neighborhood
24 values (i.e., the neighborhood function or operation to be
25 performed), and the <b>size</b> of the neighborhood.
26 <p>The user can optionally
27 specify a <b>selection</b> map, to compute new values only where the raster
28 cells of the selection map are not NULL. In case of a NULL cells,
29 the values from the input map are copied into the output map.
30 This may useful to smooth only parts of an elevation map (pits, peaks, ...).
31
32 <p><em>Example how to use a selection map with method=average:</em><br>
33 input map:
34 <div class="code"><pre>
35 1 1 1 1 1
36 1 1 1 1 1
37 1 1 10 1 1
38 1 1 1 1 1
39 1 1 1 1 1
40 </pre></div>
41 selection map, NULL values are marked as *:
42 <div class="code"><pre>
43 * * * * *
44 * * 1 * *
45 * 1 1 1 *
46 * * 1 * *
47 * * * * *
48 </pre></div>
49 The output map:
50 <div class="code"><pre>
51 1 1 1 1 1
52 1 1 2 1 1
53 1 2 2 2 1
54 1 1 2 1 1
55 1 1 1 1 1
56 </pre></div>
57 Without using the selection map, the output map would look like this:
58 <div class="code"><pre>
59 1 1 1 1 1
60 1 2 2 2 1
61 1 2 2 2 1
62 1 2 2 2 1
63 1 1 1 1 1
64 </pre></div>
65
66 <p>It is also possible to weigh cells within the neighborhood. This can
67 be either done with a custom weights matrix or by specifying a weighting
68 function.
69
70 <p>In order to use a custom weights matrix, <i>file</i> needs to be
71 specified as a <b>weighting_function</b> and a path to a text file
72 containing the weights needs to be given in the <b>weight</b> option.
73
74 <p>Alternatively, <i>gaussian</i> and <i>exponential</i> weighting
75 functions can be selected as weighting function.
76
77 <p>For the <i>gaussian</i> weighting function, the user specifies the
78 sigma value (&sigma;) for the gauss filter in the <b>weighting_factor</b>
79 option. The sigma value represents the standard deviation of the gaussian
80 distribution, where the weighting formula for the gaussian filter is
81 defined as follows:
82 <p>exp(-(x&#42;x+y&#42;y)/(2&#42;&sigma;&#94;2))/(2&#42;&pi;&#42;&sigma;&#94;2)
83 <p>Lower values for sigma result in a steeper curve, so that more weight
84 is put on cells close to the center of the moving window with a steeper
85 decrease in weights with distance from the center.
86
87 <p>For the <i>exponential</i> weighting function, the user specifies a
88 factor for an exponential kernel in the <b>weighting_factor</b>.
89 Negative factors result in negative exponential decrease in weights from
90 the center cell. The weighting formula for the exponential kernel is
91 defined as follows:
92 <p>exp(factor*sqrt(x&#42;x+y&#42;y))
93 <p>Stronger negative values for the factor result in a steeper curve,
94 so that more weight is put on cells close to the center of the moving
95 window with a steeper decrease in weights with distance from the center.
96
97 <p>Optionally, the user can also run <em><b>r.neighbors</b></em> specify
98 the <b>TITLE</b> to be assigned to the raster map layer <b>output</b>,
99 select to not align the resolution of the output with that of the
100 input (the <b>-a</b> option).
101 These options are described further below.
102
103 <p>
104 <em>Neighborhood Operation Methods:</em>
105 The <b>neighborhood</b> operators determine what new
106 value a center cell in a neighborhood will have after examining
107 values inside its neighboring cells.
108 Each cell in a raster map layer becomes the center cell of a neighborhood
109 as the neighborhood window moves from cell to cell throughout the map layer.
110 <em><b>r.neighbors</b></em> can perform the following operations:
111
112 <p><dl>
113
114 <dt><b>average</b>
115
116 <dd>The average value within the neighborhood.
117 In the following example, the result would be:
118
119 <br>
120
121 (7*4 + 6 + 5 + 4*3)/9 = 5.6667
122
123 <br>
124
125 The result is rounded to the nearest integer (in this case 6).
126
127 <div class="code"><pre>
128 Raw Data Operation New Data
129 +---+---+---+ +---+---+---+
130 | 7 | 7 | 5 | | | | |
131 +---+---+---+ average +---+---+---+
132 | 4 | 7 | 4 |---------&gt;| | 6 | |
133 +---+---+---+ +---+---+---+
134 | 7 | 6 | 4 | | | | |
135 +---+---+---+ +---+---+---+
136 </pre></div>
137
138 <dt><b>median</b>
139
140 <dd>The value found half-way through a list of the
141 neighborhood's values,
142 when these are ranged in numerical order.
143
144 <dt><b>mode</b>
145
146 <dd>The most frequently occurring value in the neighborhood.
147
148 <dt><b>minimum</b>
149
150 <dd>The minimum value within the neighborhood.
151
152 <dt><b>maximum</b>
153
154 <dd>The maximum value within the neighborhood.
155
156 <dt><b>range</b>
157
158 <dd>The range value within the neighborhood.
159
160 <dt><b>stddev</b>
161
162 <dd>The statistical standard deviation of values
163 within the neighborhood (rounded to the nearest integer).
164
165 <dt><b>sum</b>
166
167 <dd>The sum of values within the neighborhood.
168
169 <dt><b>count</b>
170 <dd>The count of filled (not NULL) cells.
171
172 <dt><b>variance</b>
173
174 <dd>The statistical variance of values
175 within the neighborhood (rounded to the nearest integer).
176
177 <dt><b>diversity</b>
178
179 <dd>The number of different values within the neighborhood.
180 In the above example, the diversity is 4.
181
182 <dt><b>interspersion</b>
183
184 <dd>The percentage of cells containing values which differ from the values
185 assigned to the center cell in the neighborhood, plus 1.
186 In the above example, the interspersion is:
187
188 <br>
189
190 5/8 * 100 + 1 = 63.5
191
192 <br>
193
194 The result is rounded to the nearest integer (in this case 64).
195
196 <dt><b>quart1, quart3</b>
197
198 <dd>The result will be the first or the third quartile (equal of 25th and 75th percentiles).
199
200 <dt><b>perc90</b>
201
202 <dd>The result will be the 90th percentile of neighborhood.
203
204 <dt><b>quantile</b>
205
206 <dd>Any quantile as specified by "quantile" input parameter.
207
208 </dl>
209 <p><br>
210
211 <em>Neighborhood Size:</em>
212 The neighborhood <b>size</b> specifies which cells surrounding any given
213 cell fall into the neighborhood for that cell.
214 The <b>size</b> must be an odd integer and represent the length of one of
215 moving window edges in cells.
216 For example, a size value of 3 will result in
217 <div class="code"><pre>
218 _ _ _
219 |_|_|_|
220 3 x 3 neighborhood ---&gt; |_|_|_|
221 |_|_|_|
222
223 </pre></div>
224 <p>
225 <em>Matrix weights:</em>
226 A custom matrix can be used if none of the neighborhood operation
227 methods are desirable by using the <b>weight</b>. This option must
228 be used in conjunction with the <b>size</b> option to specify the
229 matrix size and <i>file</i> needs to be specified as
230 <b>weighting_function</b>. The weights desired are to be entered into a
231 text file. For example, to calculate the focal mean with a matrix
232 <b>size</b> of 3,
233 <div class="code"><pre>
234 r.neigbors in=input.map out=output.map size=3 weighting_function=file \
235 weight=weights.txt
236 </pre></div>
237
238 The contents of the weight.txt file:
239 <div class="code"><pre>
240 3 3 3
241 1 4 8
242 9 5 3
243 </pre></div>
244
245 This corresponds to the following 3x3 matrix:
246 <div class="code"><pre>
247 +-+-+-+
248 |3|3|3|
249 +-+-+-+
250 |1|4|8|
251 +-+-+-+
252 |9|5|3|
253 +-+-+-+
254 </pre></div>
255
256 To calculate an annulus shaped neighborhood the contents of weight.txt file
257 may be e.g. for size=5:
258 <div class="code"><pre>
259 0 1 1 1 0
260 1 0 0 0 1
261 1 0 0 0 1
262 1 0 0 0 1
263 0 1 1 1 0
264 </pre></div>
265
266 The way that weights are used depends upon the specific aggregate
267 (<b>method</b>) being used.
268
269 However, most of the aggregates have the property that multiplying all
270 of the weights by the same factor won't change the final result (an
271 exception is <b>method=count</b>).
272
273 Also, most (if not all) of them have the properties that an integer
274 weight of N is equivalent to N occurrences of the cell value, and
275 having all weights equal to one produces the same result as when
276 weights are not used.
277
278 When weights are used, the calculation for <b>method=average</b> is:
279
280 <div class="code"><pre>
281 sum(w[i]*x[i]) / sum(w[i])
282 </pre></div>
283
284 In the case where all weights are zero, this will end up with both the
285 numerator and denominator to zero, which produces a NULL result.
286
287 <h3>FLAGS</h3>
288 <dl>
289 <dt><b>-a</b>
290
291 <dd>If specified, <em><b>r.neighbors</b></em> will not align the output
292 raster map layer with that of the input raster map layer.
293 The <em><b>r.neighbors</b></em> program works in the current geographic region.
294 It is recommended, but not required, that the resolution
295 of the geographic region be the same as that of the raster map layer.
296 By default, if unspecified,
297 <em><b>r.neighbors</b></em> will align these geographic region settings.
298 <p>
299 <dt><b>-c</b>
300 <dd>
301 This flag will use a circular neighborhood for the moving analysis window,
302 centered on the current cell.
303
304 <p>The exact masks for the first few neighborhood sizes are as follows:
305 <div class="code"><pre>
306 3x3 . X . 5x5 . . X . . 7x7 . . . X . . .
307 X O X . X X X . . X X X X X .
308 . X . X X O X X . X X X X X .
309 . X X X . X X X O X X X
310 . . X . . . X X X X X .
311 . X X X X X .
312 . . . X . . .
313
314
315 9x9 . . . . X . . . . 11x11 . . . . . X . . . . .
316 . . X X X X X . . . . X X X X X X X . .
317 . X X X X X X X . . X X X X X X X X X .
318 . X X X X X X X . . X X X X X X X X X .
319 X X X X O X X X X . X X X X X X X X X .
320 . X X X X X X X . X X X X X O X X X X X
321 . X X X X X X X . . X X X X X X X X X .
322 . . X X X X X . . . X X X X X X X X X .
323 . . . . X . . . . . X X X X X X X X X .
324 . . X X X X X X X . .
325 . . . . . X . . . . .
326 </pre></div>
327
328 </dl>
329
330 <h2>NOTES</h2>
331
332 The <em><b>r.neighbors</b></em> program works in the current geographic region
333 with the current mask, if any. It is recommended, but not required,
334 that the resolution of the geographic region be the same as that
335 of the raster map layer. By default, <em><b>r.neighbors</b></em> will align
336 these geographic region settings. However, the user can select to keep
337 original input and output resolutions which are not aligned by specifying
338 this (e.g., using the <b>-a</b> option).
339 <p>
340 <em><b>r.neighbors</b></em> doesn't propagate NULLs, but computes the
341 aggregate over the non-NULL cells in the neighborhood.
342 <p>
343 The <b>-c</b> flag and the <b>weights</b> parameter are mutually exclusive. Any
344 use of the two together will produce an error. Differently-shaped neighborhood
345 analysis windows may be achieved by using the <b>weight=</b> parameter to
346 specify a weights file where all values are equal. The user can also vary the
347 weights at the edge of the neighborhood according to the proportion of the cell
348 that lies inside the neighborhood circle, effectively anti-aliasing the analysis
349 mask.
350 <p>
351 For aggregates where a weighted calculation isn't meaningful
352 (specifically: minimum, maximum, diversity and interspersion), the
353 weights are used to create a binary mask, where zero causes the cell
354 to be ignored and any non-zero value causes the cell to be used.
355 <p>
356 <em><b>r.neighbors</b></em> copies the GRASS <em>color</em> files associated with
357 the input raster map layer for those output map layers that are based
358 on the neighborhood average, median, mode, minimum, and maximum.
359 Because standard deviation, variance, diversity, and interspersion are indices,
360 rather than direct correspondents to input values,
361 no <em>color</em> files are copied for these map layers.
362 (The user should note that although the <em>color</em> file is copied
363 for <em>average</em> neighborhood function output,
364 whether or not the color file makes sense for the output
365 will be dependent on the input data values.)
366
367 <h3>Propagation of output precision</h3>
368
369 The following logic has been implemented: For any aggregate, there are
370 two factors affecting the output type:
371
372 <ol>
373 <li> Whether the input map is integer or floating-point.</li>
374 <li> Whether the weighted or unweighted version of the aggregate is used.</li>
375 </ol>
376
377 These combine to create four possibilities:
378 <p>
379 <table border="1">
380 <tr><th rowspan="2">input type/weight</th><th colspan="2">integer</th><th colspan="2">float</th></tr>
381 <tr><th>no</th><th>yes</th><th>no</th><th>yes</th></tr>
382 <tr><td>average</td><td>float</td><td>float</td><td>float</td><td>float</td></tr>
383 <tr><td>median</td><td>[1]</td><td>[1]</td><td>float</td><td>float</td></tr>
384 <tr><td>mode</td><td>integer</td><td>integer</td><td>[2]</td><td>[2]</td></tr>
385 <tr><td>minimum</td><td>integer</td><td>integer</td><td>float</td><td>float</td></tr>
386 <tr><td>maximum</td><td>integer</td><td>integer</td><td>float</td><td>float</td></tr>
387 <tr><td>range</td><td>integer</td><td>integer</td><td>float</td><td>float</td></tr>
388 <tr><td>stddev</td><td>float</td><td>float</td><td>float</td><td>float</td></tr>
389 <tr><td>sum</td><td>integer</td><td>float</td><td>float</td><td>float</td></tr>
390 <tr><td>count</td><td>integer</td><td>float</td><td>integer</td><td>float</td></tr>
391 <tr><td>variance</td><td>float</td><td>float</td><td>float</td><td>float</td></tr>
392 <tr><td>diversity</td><td>integer</td><td>integer</td><td>integer</td><td>integer</td></tr>
393 <tr><td>interspersion</td><td>integer</td><td>integer</td><td>integer</td><td>integer</td></tr>
394 <tr><td>quart1</td><td>[1]</td><td>[1]</td><td>float</td><td>float</td></tr>
395 <tr><td>quart3</td><td>[1]</td><td>[1]</td><td>float</td><td>float</td></tr>
396 <tr><td>perc90</td><td>[1]</td><td>[1]</td><td>float</td><td>float</td></tr>
397 <tr><td>quantile</td><td>[1]</td><td>[1]</td><td>float</td><td>float</td></tr>
398 </table>
399 <p>
400 [1] For integer input, quantiles may produce float results from
401 interpolating between adjacent values.
402 <br>
403 [2] Calculating the mode of floating-point data is essentially
404 meaningless.
405 <p>
406
407 With the current aggregates, there are 5 cases:
408
409 <ol>
410 <li> Output is always float: average, variance, stddev, quantiles (with
411 interpolation).</li>
412 <li> Output is always integer: diversity, interspersion.</li>
413 <li> Output is integer if unweighted, float if weighted: count.</li>
414 <li> Output matches input: minimum, maximum, range, mode (subject to
415 note 2 above), quantiles (without interpolation).</li>
416 <li> Output is integer for integer input and unweighted aggregate,
417 otherwise float: sum.</li>
418 </ol>
419
420
421 <h3>Performance</h3>
422 To enable parallel processing, the user can specify the number of threads to be
423 used with the <b>nprocs</b> parameter (default 1). The <b>memory</b> parameter
424 (default 300) can also be provided to determine the size of the buffer for
425 computation.
426
427 <div align="center" style="margin: 10px">
428 <img src="r_neighbors_benchmark_nprocs.png" alt="benchmark for number of cells" border="0">
429 <img src="r_neighbors_benchmark_size.png" alt="benchmark for neighborhood size" border="0">
430 <img src="r_neighbors_benchmark_memory.png" alt="benchmark for memory size" border="0">
431 <br>
432 <i>Figure: Benchmark on the left shows execution time for different
433 number of cells, benchmark in the middle shows execution time for different sizes of neighborhood
434 for a 10000x10000 raster and benchmark on the right shows execution time
435 for different memory size for a 10000x10000 raster. See benchmark scripts in source code.
436 (Intel Core i9-10940X CPU @ 3.30GHz x 28) </i>
437 </div>
438
439 <p>To reduce the memory requirements to minimum, set option <b>memory</b> to zero.
440 To take advantage of the parallelization, GRASS
441 needs to be compiled with OpenMP enabled.
442
443 <h2>EXAMPLES</h2>
444
445 <h3>Measure occupancy of neighborhood</h3>
446
447 Set up 10x10 computational region to aid visual inspection of results
448 <div class="code"><pre>
449 g.region rows=10 cols=10
450 </pre></div>
451
452 Fill 50% of computational region with randomly located cells.
453 "distance=0" will allow filling adjacent cells.
454 <div class="code"><pre>
455 r.random.cells output=random_cells distance=0 ncells=50
456 </pre></div>
457
458 Count non-empty (not NULL) cells in 3x3 neighborhood
459 <div class="code"><pre>
460 r.neighbors input=random_cells output=counts method=count
461 </pre></div>
462
463 Optionally - exclude centre cell from the count (= only look around)
464 <div class="code"><pre>
465 r.mapcalc "count_around = if( isnull(random_cells), counts, counts - 1)"
466 </pre></div>
467
468 <h2>SEE ALSO</h2>
469
470 <em>
471 <a href="g.region.html">g.region</a>,
472 <a href="r.clump.html">r.clump</a>,
473 <a href="r.mapcalc.html">r.mapcalc</a>,
474 <a href="r.mfilter.html">r.mfilter</a>,
475 <a href="r.statistics.html">r.statistics</a>,
476 <a href="r.support.html">r.support</a>
477 </em>
478
479 <h2>AUTHORS</h2>
480
481 Original version: Michael Shapiro,
482 U.S.Army Construction Engineering Research Laboratory
483 <br>
484 Updates for GRASS GIS 7 by Glynn Clements and others