[GRASSLIST:5012] Re: reporting statistics from clumps of pixels

Scott W Mitchell smitch at eos.geog.utoronto.ca
Wed Nov 20 12:59:06 EST 2002

I've done this a few different ways.  #1 is most powerful, my preferred  
method these days as long as data volume / processing time isn't a  
problem.  But it requires another software package to be installed.  #2  
is the most portable solution, using tools programs you probably  
already have.  #3 creates exactly what you want with one command, but  
is a custom local command here with no documentation yet and very  
little testing - having said that, I'm quite willing to share if you  
can be patient.

Also note that similar questions have been asked on this list in the  
past, and if you haven't already, do a search of the archives, there  
are probably more possiblities than I list here...

(1) Use Roger Bivand's GRASS/R interface.  Very elegant and enables  
much more.  Start GRASS, set region etc, then start R, read in the  
elevation map and the clump map, then use the tapply() function in R to  
get averages of elevation in each clump.  There is more learning curve  
in this than some other solutions, but if like me you already use R, no  
problem.  See www.R-project.org for info on R, and  
index.html#grassR (or equivalent URL at GRASS mirror of your choice)  
for info on the interface.

(2) Use r.average as you've said, the problem is that it produces a map  
not a table - so for one project I created a temporary raster of the  
averages, then used r.stats (&/or r.cats if necessary depending on the  
data) and some unix text processing utilities to print out a table of  
the values that are produced in the  raster.  Also possible (and for  
other statistics) with r.statistics.  These used to be integer-only  
tools, I think, perhaps they've been updated ?  If this much detail  
doesn't make sense to you, contact me directly and I can dig up some  
old example scripts I used to do this.

(3) We developed our own program in the lab here, back in grass4.3  
days, which I now call "r.polystats", and I have it working in grass5.   
The advantage is that it does produce text tables by default as opposed  
to maps (which are also an option).
The problem is that I haven't updated it to work with floating point  
data yet. Also, it has next to no documentation and the grass5 version  
isn't tested by anyone besides myself yet.  However, if after looking  
at all the options this still sounds interesting to you and you're  
willing to compile software, let me know and we can talk about getting  
it on your system.

Scott Mitchell
smitch at geog.utoronto.ca

On Wednesday, November 20, 2002, at 12:09 PM, Thomas Dewez wrote:

> Dear all,
> I am struggling on how to extract statistics from clumps of data. I
> have a layer containing clumps ID and I would like to obtain an ascii
> output of the average elevation of those clumps.
> Is there a command that does this?
> r.average was used to compute the average value of each clump.
> The problem is simply to report the value contained in one layer
> while looking up the category value in another layer. The report
> would look something like this:
> clump1, value...
> clump2, value...
> ...

More information about the grass-user mailing list