[GRASS-user] center of gravity: solution

Hamish hamish_b at yahoo.com
Thu Dec 10 17:16:51 EST 2009


Achim Kisseler  wrote:
> 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)


beware it will only be "valid" for small ranges of latitude.

otherwise the weights of the longitudes at high latitudes will
have more weight in the average than they should.


Hamish



      


More information about the grass-user mailing list