Hi list,

just want to share some code that could be useful to some of you.

I was looking for a way to do other patch based statistics than those provided 
in r.statistics. Using masks and a loop in GRASS was terribly slow (certainly 
for very patchy areas (in my case some 100000). So this is the fastest I could 
make it without programming a module myself. I use R, but not the spgrass6 
link, from within a bash script.

The script uses a map with all numbered patches as can be provided by 
r.le.patch or r.le.stats and a cover map which contains the values u want to 
evaluate with some exotic statistic or function.

In my setup I formatted the output as a rules file so the results could be 
translated back into an actual map using the patch number map as common factor. 
Other approaches are also possible, depending on the need of the user.

Hope some people can benefit from it.

Cheers,
Koen

-- THE BASH SCRIPT ---->

#!/bin/bash
# 
###########################################################################################################################
# 
# Grass bash script to evaluate the content of patches based upon a patch and 
cover map...
#
# !!! Only works within the GRASS terminal !!!
#
#
# Calculate the a map with numbered patches with r.le.patch or r.le.trace. Use 
this as input for the patches.number.map.
# The cover.map map is the map you want to evaluate based upon the delineated 
patches with another function than those
# provided by r.statistics.
#
# use: sh myscript.sh patch.number.map cover.map (name of the script depends on 
how you save it ofcourse)
#
###########################################################################################################################

r.out.xyz input=$1 output=- fs=' ' | awk '{print $3}' > patches
r.out.xyz input=$2 output=- fs=' ' | awk '{print $3}' > cover

#
# Make a two column matrix with first the patch number than the associated 
cover value
#

paste patches cover > Rinput


###########################################################################################################################
# 
# R code starts here! 
#
###########################################################################################################################
R --slave --vanilla --quiet --no-save  <<EEE


rdata <- read.table("Rinput")

for (i in 1:nrpatches){

rdatasub <- subset(rdata[,2],rdata[,1]==i)

#
# complicated patch based statistics go here...
#


}
EEE

#
# Close R session within bash GRASS script...
#

###########################################################################################################################


#
# The output of the R script is best printed according to the rules 
specifications so you can use the results
# directly as a reclassify rule within GRASS.
#


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

Reply via email to