[GRASS-user] r.neighbors with a custom mode calculation?

Glynn Clements glynn at gclements.plus.com
Mon Mar 8 22:00:55 EST 2010

Don MacQueen wrote:

> I'm pretty new to GRASS, and would appreciate some help/guidance/suggestions:
> I have been smoothing a raster with r.neighbor, as in this example:
>    r.neighbors input=alandcov at PERMANENT output=alandcov_smooth 
> method=mode size=45 {title=Smoothed}
> The raster values are integers representing qualitative categories.
> I'd like to modify the smoothing to exclude a particular value. 
> Specifically, if a cell (is that the right term?) has a particular 
> value, say 11, then I want to exclude it, as follows:
>   -  if the center of the neighborhood = 11, leave it unchanged (do 
> not calculate the mode, just return 11)
>   -  if the center is not 11, calculate the mode, but exclude any 
> points in the neighborhood whose value = 11 (that is, take the mode 
> only of points not equal to 11)
> The real goal is to preserve boundaries between 11 and not 11. This 
> is a land cover raster; 11 represents water, and I would like the 
> smoothing to not change water to land or vice versa.
> It seems like it might be possible to do it by creating a mask, 
> smoothing the masked raster, and then putting the 11's back in maybe 
> using r.mapcalc, but the details are a bit tough for me to put 
> together, as a GRASS newbie.

r.mapcalc 'elevens = if(alandcov == 11,11,null()) ; others = if(alandcov == 11,null(),alandcov)'
r.neighbors input=others output=alandcov_tmp  method=mode size=45
r.mapcalc 'alandcov_smooth = if(not(isnull(elevens)),elevens,alandcov_tmp)'
g.remove rast=elevens,others,alandcov_tmp

Glynn Clements <glynn at gclements.plus.com>

More information about the grass-user mailing list