I new man in processing and modeling rasters by GRASS GIS. But I have one unsolved problem.

I have a lot of rasters with similar names: raster_1, raster_2, ..., raster_100.

Also in my work environment there are more rasters with unique names.

I need to do some steps:

  1. seting to all rasters with names "raster_..." null values against zero;

  2. calculation total (for all) minimum and maximum values for rasters with names "raster_...";

  3. setting total for all rasters (with names "raster_...") color palette based on total minimum and maximum values.

How can I to solve this problem? Do I must to use batch processing with names mask? Or GRASS give another ways?


1 Answer 1


For 1. you can run a loop and set 0 values to null in each raster. First save a list of all the rasters (and set the region, as always):

RAST_LIST=`g.list rast pattern="raster_* separator=comma"
g.region -p rast=${RAST_LIST}

Now to set null values:

for r in $RAST_LIST; do
r.null $r setnull=0

For 2, you can take advantage of the fact that the r.univar module can take a list of rasters as input, and calculate univariate statistics for the whole list at once:

r.univar input=${RAST_LIST}

This will output several univariate stats, including min and max. However, if the number of rasters is very large, this might not work, due to length of the command line. Plan B would be to create two newe series rasters, one minimum and one maximum, then get the min and max from each respectively:

r.series input=${RAST_LIST} method=minimum,maximum output=rasters_min,rasters_max
r.univar rasters_min
r.univar rasters_max

For 3. You would create a color ramp with whatever colors you like, using the minimum and maximum values that you find above. Save this as a text file (see the r.colors manpage for details). Then apply your color ramp to all rasters in a loop.

for r in $RAST_LIST; do
r.colors $r rule=<your color ramp file>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.