[GRASS-user] How to initialize raster maps.

Marcello Gorini gorini at gmail.com
Fri Dec 9 18:57:29 EST 2011


> 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

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 :) )

Thanks for your comments.


> There is no point in referring to your original map in the r.mapcalc
> expression; the result will always be created with the bounds and
> resolution of the current region. AFAIK, the only modules which don't
> behave this way are r.in.*, which create the output to match the size
> of the input file.
Yes, Moritz's suggestion showed me that. I perfectly understand it now,


I'm sure that there are other ways to create a map consisting of
> zeros, but there's no reason to suspect that any of them will be
> faster than r.mapcalc.
 There really isn't. I just suspected it was taking too long and found out
in my little experiment that was only my fault, putting a logic expression
where there was no need of it. Setting the region and r.mapcalc zero_map =
0 does the job.

Thank you.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/grass-user/attachments/20111209/86bdfc37/attachment-0001.html

More information about the grass-user mailing list