[GRASS-user] sum one raster map conditional on another raster map

Martin Wegmann martin.wegmann at uni-wuerzburg.de
Thu Mar 18 09:07:02 EDT 2010

Hello Stephen,

On Mittwoch, 17. März 2010 23:06:55 stephen sefick wrote:
> I would like to sum all of the positive values of one raster map
> (NDVI) based on a raster containing many watersheds that have values
> 1-22.  Is there a way to do this in GRASS?  Or a way to export this

Glynn already pointed you to r.statistics hence you only need to combine it 
with a few if-else statement in r.mapcalc:

r.mapcalc "watershed_new = if(watershed >=1 && watershed <=22,1,0)"
# if watersheds values are larger-equal 1 AND smaller-equal 22, assign value 1 
otherwise value 0

r.mapcalc "ndvi_new = if(ndvi_raster >=0,ndvi_raster,null())"
#all values above 0 keep their values, below 0 are converted to null() 

r.mapcalc "ndvi_new_clip = if(watershed_new == 1,ndvi_new,null())"
# if watershed_new is equal 1 (see above), then assign the values of ndvi_new, 
otherwise nothing

now you can perform the r.statistics method=sum analysis on NDVI values above 
0 only and for areas where watershed is above 1 and below 22.

hope this helps and I understood your question right, Martin

P.S.: in case you want to do the analysis on single watersheds (1-22) then you 
would need another statement like: 
for i in 1 2 3 4 5 ... 22 ; do 
r.mapcalc "new = if(watershed == $i,1,null())"
r.statistics ....
; done

Dr. Martin Wegmann

University of Wuerzburg
Institute of Geography
Department of Remote Sensing
Remote Sensing and Ecosystem Research
Am Hubland
97074 Wuerzburg, Germany
German Aerospace Center (DLR)
German Remote Sensing Data Center (DFD)

Phone:  +49-(0)931-31-83446
Fax:    +49-(0)931-888-4961
Email:  martin.wegmann at uni-wuerzburg.de
url: http://www.remote-sensing.uni-wuerzburg.de
url: http://www.dlr.de/caf
url: http://www.global-change-ecology.de

More information about the grass-user mailing list