Distance analysis

Lars Schylberg larss at fmi.kth.se
Thu Mar 18 03:33:21 EST 1993


I wrote a little script (r.distance.sh) last year that makes something
that is close to this.  I used r.buffer on a clump map (produced by
r.clump).  Maybe you could start with this script and modify it for your
needs.  This scripts lets the user point on two clumps and then the 
scripts computes the shortest distance between those. 
The script is included in the end of this message.  Please let me know
if you come up with some bright idea concening this.  I am allways 
interested to see new scritps if you write any concerning this.
I guess you could include the r.clump operation in your script, display 
the cateogory map and compute distances on the clump map.

Lars

Lars Schylberg                          Email: larss at fmi.kth.se  
Dept. of Geodesy and Photogrammetry 
Royal Institute of Technology (KTH)     Tel.   +46 8 790 86 33   
S-100 44  STOCKHOLM, SWEDEN             Fax.   +46 8 790 66 10


> I've been searching the grass users archives for scripts that have to do with
> distance analysis. So far I have not found any.
> 
> I want to write a script that would return the shortest distance
> from a specificparcel of land to a specific landuse class e.g. urban
> land.
>  
> r.cost seemed like a good place but if someone out there has some experience 
> wih this I would like to hear from you.
> 
> Thanks in advance.
> 
> Bill Jackson


#!/bin/sh
#
# r.distance.sh
#
# Author: Lars Schylberg (larss at fmi.kth.se)  921019
#        
#--------------------------------------------------------------------------
# Check if GRASS is running
#
test "$GISRC" || echo "GRASS is not running" || exit 2
#----------------------------------------------------------------------------
#
#  Evaluate arguments
#
if [ $# != 1 ]
then
    echo
    echo Usage: `basename $0` 
    echo '       clumpmap=mapname '
    echo
    exit 1
fi
#
#  parse input arguments
#
for i do
	case $i in
		clumpmap=*)
			CLUM=`echo $i | sed s/clumpmap=//` ;;
		clum=*)
			CLUM=`echo $i | sed s/clum=//` ;;
		c=*)
			CLUM=`echo $i | sed s/c=//` ;;
		*)
			echo ""
			echo "Unrecognized option: $i"
			echo 'Options: clumpmap=mapname '
                        echo
			exit 1
	esac
done
#-----------------------------------------------------------------------
#
#  Check the input arguments
#
eval `g.findfile element=cell file=$CLUM`
if [ ! "$file" ]
then
    echo "$CLUM - clump cell file not found"
    exit 2
fi
#
#---------------------------------------------------------------------
#
#  Show category map
#
d.rast $CLUM
#
#  retrieve the two objects by pointing with cursor
#
echo
echo "POINT WITH CURSOR ON THE FIRST OBJECT"
#
d.what.rast -1t map=$CLUM > stat
#
obj1=`cat stat | sed -n '3p' | awk -f: '{print $2}'`
#
#
echo
echo "Object 1 has object number $obj1"
echo
echo "POINT WITH CURSOR ON THE SECOND OBJECT"
#
d.what.rast -1t map=$CLUM > stat
#
obj2=`cat stat | sed -n '3p' | awk -f: '{print $2}'`
#
#
echo
echo "Object 2 has object number $obj2"
echo
/bin/rm stat
#
#  Determine clostest distance between the two objects
#
echo "$obj1 = 1" | r.reclass input=$CLUM output=obj1
echo "$obj2 = 1" | r.reclass input=$CLUM output=obj2

MAX_DISTANCE=200

ZONES=`echo $MAX_DISTANCE | awk 'BEGIN { printf "5"} 
                          { for (i=10; i<=$1; i+=5) printf ",%d", i }'`	

r.buffer input=obj1 output=zones dis=$ZONES 2>&1

r.cats map=zones fs=space | sed '1,2d' | awk '{ print $1"="$2 }' > rcrule1

cat rcrule1 | r.reclass input=zones output=dist 2>&1

DIST=`r.stats -q input=obj2,dist | \
      awk '{ if ( $1 == 1 && $2 > 0 ) print $2 }' | sed -n '1p'`

echo 'distance = '$DIST

g.remove rast=obj1,obj2,zones 2>&1






More information about the grass-user mailing list