r.neighbors.html 14 KB

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