[GRASS-user] center of gravity: solution

Achim Kisseler ak7 at jupiter.uni-freiburg.de
Thu Dec 10 12:27:26 EST 2009


Hi all,

I found a solution for the problem of getting the cog for each area with
weight of a value map. Its simply r.stats and one! awk-command. And its
quite fast!


-%<--

#####################################
## get stats into file
r.stats -c -n -g input=$1_areas,$1_values output=temp_stats
# eg.
# (east north cat val)
# 49.2625333333333 -11.9458623936673 14 1
# 49.2375333333333 -11.9541956915396 14 2

#####################################
## calc cog:
##     sum coor*value
## devided by
##     sum value
## for east and north
echo "cat lon lat" > temp_stats2
awk '{a[$3]+=$1*$4; b[$3]+=$4; c[$3]+=$2*$4; d[$3]+=$4}
END{for (i in a){print i,a[i]/b[i],c[i]/d[i]}}
' temp_stats >> temp_stats2

-%<--


temp_stats2 looks like

cat lon lat
14 49.123 -11.123
15 50.123 -10.123
...

(or whatever)

Cheers,
Achim


More information about the grass-user mailing list