[GRASS-user] Re: r.mapcalc and nested if

Glynn Clements glynn at gclements.plus.com
Fri Apr 16 11:10:05 EDT 2010


thedok78 wrote:

> I have got a raster map with a lot of small area like this one:
> 
> 112 112 112 122 112
> 112 112   1     1   122
> 122   1     1     1   122
> 112 122 122 112 112
> 
> The boundary is made cells with values from 100 to 200 instead the inner
> ones have got 1 as value.
> I need to fill the inner cells with the mode of a 3x3 window.
> Using a loop until there are no more cell with 1 as value, I started with
> this approach:
> 
> map=if(map ==
> 1,mode(map[-1,-1],map[-1,0],map[-1,1],map[0,1],map[1,1],map[1,0],map[1,-1],map[0,-1]),0)
> 
> I added the map == 1 condition due the null cells outside the boundary,
> which set the mode to null and slowly eat all my areas :)
> 
> Using that syntax the loop goes smooth but after a certain point the cells=1
> remain the same because the mode is always 1.
> 
> I discarded r.neighbors because it changes also the values on the boundary
> which should remain the same.

r.mapcalc 'map.tmp = if(map == 1, null(), map)'
r.neighbors in=map.tmp out=map.mode method=mode size=3
r.mapcalc 'map.filled = if(map == 1, map.mode, map)'

Replacing the ones with nulls in the first step will cause r.neighbors
to ignore those cells when calculating the mode.

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


More information about the grass-user mailing list