[GRASS-user] Alternative to r.stats to generate % of surface

Greetings

I want to calculte % of surface in a raster map for each Integer class value. The thing is that I’m, using r.stats but the sum % values is always much more (104 or 102 or 105). Is there any other (more precised) method to calculate this ?
THanks
Helena

Hi Helena,

I have come to the same problem and solved it with ugly and very specific shell scripts. There is most certainly a better way within GRASS and no doubt you can code it much better in Shell, but anyway, this may get you going somehow.

Code snippet:

MAP=$1

g.region rast=$MAP
max_cats=9
file=true_percentages.txt
rm $file

tot=$(r.univar $MAP | grep "^n: " | awk ‘{print $2}’)
r.stats -cn $MAP > stats.txt

cats=1
while [ $cats -le $max_cats ]; do

ncat=“head -$cats stats.txt | tail -1 | awk '{print $2}'
cat=“head -$cats stats.txt | tail -1 | awk '{print $1}'
perc=“echo $tot $ncat | awk '{printf("%.5f", $2 / $1 * 100);}'

echo $cat $perc >> $file

cats=“$(expr $cats + 1)”

done

I wanted everything in text files, so I generated these stats.txt and true_percentages.txt. The idea of the script is to use the actual cell count given by r.stats -cn and divide it by the total count given by r.univar.

Note that this script is very specific and only work for categories that are incremented by 1. In my case, they go from 1 to 9.

How brave I am to publicize such an ugly script, huh? :slight_smile: The result sums up to 1 at least. Just give the name of the categorical map as input.

Hope it helps somehow.

Marcello.

On Tue, Nov 15, 2011 at 9:36 AM, Helena Herrera <helenaherrera1980@gmail.com> wrote:

Greetings

I want to calculte % of surface in a raster map for each Integer class value. The thing is that I’m, using r.stats but the sum % values is always much more (104 or 102 or 105). Is there any other (more precised) method to calculate this ?
THanks
Helena


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user

On Tue, Nov 15, 2011 at 1:35 PM, Marcello Gorini <gorini@gmail.com> wrote:

Hi Helena,

I have come to the same problem and solved it with ugly and very specific shell scripts. There is most certainly a better way within GRASS and no doubt you can code it much better in Shell, but anyway, this may get you going somehow.

Code snippet:

MAP=$1

g.region rast=$MAP
max_cats=9
file=true_percentages.txt
rm $file

tot=$(r.univar $MAP | grep "^n: " | awk ‘{print $2}’)
r.stats -cn $MAP > stats.txt

cats=1
while [ $cats -le $max_cats ]; do

ncat=“head -$cats stats.txt | tail -1 | awk '{print $2}'
cat=“head -$cats stats.txt | tail -1 | awk '{print $1}'
perc=“echo $tot $ncat | awk '{printf("%.5f", $2 / $1 * 100);}'

echo $cat $perc >> $file

cats=“$(expr $cats + 1)”

done

I wanted everything in text files, so I generated these stats.txt and true_percentages.txt. The idea of the script is to use the actual cell count given by r.stats -cn and divide it by the total count given by r.univar.

Note that this script is very specific and only work for categories that are incremented by 1. In my case, they go from 1 to 9.

How brave I am to publicize such an ugly script, huh? :slight_smile: The result sums up to 1 at least. Just give the name of the categorical map as input.

Hope it helps somehow.

What about the -c flag for r.stats? You will get the cell count and can calculate the percentage yourself.

Rainer

Marcello.

On Tue, Nov 15, 2011 at 9:36 AM, Helena Herrera <helenaherrera1980@gmail.com> wrote:

Greetings

I want to calculte % of surface in a raster map for each Integer class value. The thing is that I’m, using r.stats but the sum % values is always much more (104 or 102 or 105). Is there any other (more precised) method to calculate this ?
THanks
Helena


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


grass-user mailing list
grass-user@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-user


Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel : +33 - (0)9 53 10 27 44
Cell: +33 - (0)6 85 62 59 98
Fax (F): +33 - (0)9 58 10 27 44

Fax (D): +49 - (0)3 21 21 25 22 44

email: Rainer@krugs.de

Skype: RMkrug