[GRASS5] collecting into a vector polygons statistics of raster pixels

Hamish hamish_nospam at yahoo.com
Sun Apr 2 21:48:58 EDT 2006


> How to collect raster pixels statistics (mean, min, max, stdev,
> etc...) into polygons of a vector? I have been asked if this is
> possible within GRASS GIS, but was unable to find a tool for that. In
> Erdas, it is called "Zonal Attributes".
> Anybody could please suggest a way,

[cc grasslist as this is probably more suited for that list]

[I'm assuming GRASS 6.1-cvs]

v.what.rast will do values at vector points.
r.what will query at given x,y.
v.to.db will upload vector feature info (area,etc).


for full vector area stats from a raster, you would need to do it
using a series of grass commands to calculate the values then upload
them to the vector's tables. The following is untested, but you should
be able to adapt it to make it work...


g.region rast=query_map
v.to.rast use=attr

#add new coulumns
v.db.addcol columns=min,max,mean,stdev

then use values from `r.describe -q1` to give to r.mapcalc in a loop,

for VALUE in `r.describe -q1 v_to_rast_map` ; do
  echo "Processing $VALUE ..."
  # set mask to just the polygon of interest
  r.mapcalc "MASK=if(v_to_rast_map == $VALUE)"
  # calc stats on that
  eval `r.univar -g query_map`
  if [ $? -ne 0 ] ; echo "error on val $VALUE" ; exit 1 ; fi 
  # upload values to vector map
  echo "UPDATE vect_map SET min=$min WHERE cat=$VALUE" | db.execute
  echo "UPDATE vect_map SET max=$max WHERE cat=$VALUE" | db.execute
  g.remove MASK
done

# faster: save UPDATES to a file then run db.execute on that file when done

This could be added to v.what.rast I guess. I am more interested in
stats from a raster around a given buffer from each vector point..



Hamish




More information about the grass-dev mailing list