[GRASS-user] [GRASSLIST:1141] Re: Qn. on extracting from multiple
 rasters, data corresponding to vector polygons
    Hamish 
    hamish_nospam at yahoo.com
       
    Wed Jun 28 04:21:23 EDT 2006
    
    
  
Vishal Mehta wrote:
> I have a vector polygon shapefile which contains 8 polygons (each
> watershed is a polygon). I also have some 100 GRASS raster files of
> environmental data (rainfall, temperature etc).
>
> Whats the best way to extract for each watershed, the corresponding
> average data from each raster file and put it in a text file?
>
> I've looked at v.what.rast but it seems to be quite cumbersome for
> this application. I'm using Grass 6.0.1 on Ubuntu linux.
I think this can be easily done.
why not just
g.region rast=biggest_map
v.to.rast in=vectmap out=watersheds use=cat # or attr if ID column exists
r.mapcalc "MASK=if(watersheds == 1, 1, null() )"
r.univar rainfall
putting that in a loop:
g.region rast=typical_map
v.to.rast in=vectmap out=watersheds use=cat
r.info -r watersheds
min=1
max=8
eval `r.info -r watersheds`
#if IDs are not sequential use
# for WATERSHED in `r.stats -qn watersheds` ; do
# this is untested, but the general method works
echo "watershed|map|average_val" > results.txt
for WATERSHED in `seq $min $max` ; do
  g.remove MASK
  unset MEAN
  echo "Watershed: $WATERSHED   Map: $MAP"
  r.mapcalc "MASK=if(watersheds == $WATERSHED, 1, null() )"
  for MAP in `g.mlist type=rast pat=* fs=space` ; do
     #g.region rast=$MAP  # adjust to match res of each map
     MEAN=`r.univar -g $MAP | grep '^mean=' | cut -f2 -d=`
     echo "$WATERSHED|$MAP|$MEAN" >> results.txt 
  done
done
g.remove MASK
if maps are all different sizes and resolutions do the v.to.rast
step at the finest resolution to avoid aliasing problems.
good luck,
Hamish
    
    
More information about the grass-user
mailing list