# Neighborhood analysis based on occurrence of value

How can I do, either in ArcGIS, grass or another tool, the following: for each cell (pixel), if there is at least one cell with value '4' in a neighborhood of 5 cells around it, reclassify that cell to 0, otherwise don't change the value? Thanks.

OK I found it through help of an ex-colleague. It is (to me) not intuitive, so I'll explain for any interested readers: using Spatial Analyst, first you reclassify your map into one where all cells with values other than 4 are set to NODATA. Then you make a map with for each cell the majority in the neighborhood (focal statistic with circle neighborhood and 'majority' statistics type). That gives you, in effect, a map that has value 4 for all cells that have another cell with 4 in the neighborhood. Then finally you combine (raster calculator) the original input map with this last map: if the value in the original map is 4; and the corresponding cell in the 'mask' map is also 4; then reclassify to 0, otherwise do nothing.

This also works when you add more than 1 value you want to look for. If you, in the first step, reclassify to a map with more than 1 valid value, you are in effect saying 'reclassify to this value if there are at least one of the given values in the neighborhood, and if there are both, reclassify to that of with the largest amount of cells around it'.

Pretty spiffy :)

• (+1) This is a workable approach. Because neighborhood majority calculations are inefficient compared to sums or averages, though, you should prefer a related method: (1) equating the original map with the value 4 produces a binary indicator grid having values of 0 (false) and 1 (true). (2) Equating a neighborhood sum of that map to 0 indicates when a cell does not have a 4 in its neighborhood. (3) Either multiply the original grid by this result or use this result in a `Con` operation. – whuber Oct 2 '13 at 14:34
• Thanks, yes that's a valid concern, and I'd say that conceptually it matches the intent better than 'majority', too. – Roel Oct 2 '13 at 19:31

After further thinking about this method today, here's another variation that I needed after the question above and that might be useful for people who end up here google'ing: to make a map with for each cell the count of how many cells with a certain value are in the neighborhood, do the following. First reclassify the map into a map with in each cell that has the value you're looking for, the amount of cells in the neighborhood you're investigating (so in the example above but with a neighborhood of 3 to make the example easier, change all cells with value '4' to '8' (3x3 - the centroid)) and all other cells to 0. Then, on that map, do a focal statistic of the average of the neighborhood. This works because e.g. when there are 6 cells in the neighborhood, the average is 6x8/8 = 6.

• I don't see the point of changing the values. If you want to count the number of occurrences of a cell of a given value within focal neighborhoods, create an indicator grid by comparing the original grid to the desired value and simply compute its focal sum: you're done. – whuber Oct 2 '13 at 19:56
• Ah yes of course, that's what you get when you over think things I guess, you come up with convoluted ways to do simple things ;) – Roel Oct 2 '13 at 20:02