[GRASS-user] How to initialize raster maps.

Moritz Lennert mlennert at club.worldonline.be
Sat Dec 10 09:25:32 EST 2011


On 10/12/11 00:57, Marcello Gorini wrote:
> Glynn:
>
>
>     Note that using the same map as both input and output results in
>     undefined behaviour. Even if it happens to work, there's no guarantee
>     that it will continue to work in future versions.
>
>     If you need to implement an iterative algorithm, you should generate a
>     new map, then replace the original with g.remove+g.rename afterwards.
>
>
> Now, you scared me, because my ongoing work is pretty much based on this
> kind of code. I use r.mapcalc to create maps that serve as counters such
> as the command i++ in C. I initialize them as zero maps and then based
> on some logic I keep incrementing them, pretty much like the following
> pseudo-code to implement a sum:
>
> sum = 0
> for i = 1 to 10
>       sum = sum + i
> end
>
> But I do more that that.
>
> For instance, if certain conditions are met in other maps at a given
> iteration and the initialized map is zero, then update the initialized
> map with the value of "i". If not, don't change it. In this way, my map
> is updated every iteration until no more cells meet the condition or
> some threshold is met and processing stops. I even use d.rast to display
> the map as it is being updated.
>
> How should I proceed to avoid the "undefined behaviour" and still get
> what I want? (I hope I was able to make myself understood :) )

As Glynn suggests:

for i = 1 to 10
      sum_new = sum + i
      g.remove rast=sum
      g.rename rast=sum_new,sum


or



for i = 1 to 10
      sum_new = sum + i
      g.rename rast=sum_new,sum --overwrite

Glynn, is there any reason not to do use g.rename with --overwrite ?

Moritz



More information about the grass-user mailing list