r.neighbors.html 15 KB

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