[GRASS-user] r.neighbors treating NULL values

Glynn Clements glynn at gclements.plus.com
Wed Oct 8 01:41:29 PDT 2014


Orr, Andrew B. wrote:

> I'm trying to find a tool that will do the same thing as the
> r.neighbors with a "sum cells" function, except that the tool has to
> write a NULL value if a NULL is encountered within the cell "size"
> that's specified.
> 
> For example, if I have a raster:
> 
> 3 2 3
> 4    2
> 1 2 3
> 
> where the center cell is a null, I would like the tool output to write
> a NULL for the output when it passes over this part of the raster.
> 
> As the tool is now, it seems that it treats it as a zero or simply
> ignores it when "sum"ing the contents.
> 
> Any ideas on how I could do this, or what tool I would use?

Use r.neighbors method=count to count the number of non-null cells in
the neighbourhood.

	size=3 # or whatever
	area=$((size * size))
	r.neighbors in=map out=map.sum method=sum size=$size
	r.neighbors in=map out=map.count method=count size=$size
	r.mapcalc "result = if(map.count == $area, map.sum, null())"

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list