[GRASS-user] How to initialize raster maps.

Marcello wrote:

> But there is no way to avoid r.mapcalc, is there?

I doubt you'd get very much faster or more efficient even by
writing your own C module. (it would probably only take you a
minute to create a r.zero module out of the doc/raster/r.example
code; just change "return x;" to "return 0;" and compile.

r.mapcalc "initialized_map=if(base_map>999999,1,0)"

...
Moritz

r.mapcalc initialized_map=0

tip: to avoid all your scripts breaking when you want to use
grass7, always add a space around the '=' in r.mapcalc.

r.mapcalc "initialized_map = 0"

Hamish

Marcello:

But there is no way to avoid r.mapcalc, is there?

Hamish:

I doubt you’d get very much faster or more efficient even by
writing your own C module. (it would probably only take you a
minute to create a r.zero module out of the doc/raster/r.example
code; just change “return x;” to “return 0;” and compile.

Well, out of curiosity, I made the changes to r.example and compiled it. I then made a small script that initializes a map with the dimensions of one of my maps ( 677 x 1114) inside a loop using the three approaches mentioned in this thread:

1 - r.mapcalc “initialized_map=if(base_map>999999,1,0)” # my very smart method :slight_smile:
2 - r.mapcalc initialized_map=0 # Moritz method
3 - r.example input=initialized_map output=zero_map # Hamish idea

Since I initialize rasters 374 times in my original script, I made the loop in this way.

Results:
1 - 49 seconds
2 - 19 seconds
3 - 58 seconds

Bottom line:

  • Thanks Moritz for the tip, it will save me at least 30 seconds from now on.
  • Thanks Hamish, because I never knew r.example existed and it was fun to test the different approaches. But, no, r.example should stay just as that… an example :slight_smile:

Thanks to all.

Cheers,
Marcello.

P.S.:

Hamish:

tip: to avoid all your scripts breaking when you want to use
grass7, always add a space around the ‘=’ in r.mapcalc.

r.mapcalc “initialized_map = 0”

I will keep it in mind from now on. Tks.