[GRASSLIST:3011] Re: Vector manipulation

M Lennert fa1079 at qmul.ac.uk
Mon Jan 28 10:44:47 EST 2002


Emmanuel Poizot <poizot at cnam.fr> wrote:
> Hi,
> I'm a newbie in GRASS world.
> It sems that GRASS better manipulate raster data, however
> I should like to manipulate vector data, thinks that GRASS seems to do
> also.
> I've got some difficulties to find the operations that permits to
> display
> a vector with different colors for vectors that have a specific value on
> a field.
> I also have some difficulties to find informations that permits me to
> build and
> print a map with scale, legend, graticules, etc.
> 
> -- 
> Cordialement
> 

Emmanuel,

I don't know if this would help, but I wrote a little script that allows 
you to display vector areas in greyscale (sorry, no color, yet) 
according to defined class boundaries. It uses the latest version of 
d.area which is in Grass 5.0.0pre3. You can combine this with the 
PNG driver for map output.

Usage: d.area.class map=name catfile=name 
classes=boundary1[,boundary2,..]

Parameters:

map = vector map to display

catfile = variable file with 2 columns seperated by whitespace: 
1=category 2=variable by which to class)

classes = class boundaries separated by comma, starting with 
boundary between class 1 and 2 and ending with boundary 
between class max-1 and max

example:

d.area.class map=communes catfile=ACP_scores1 classes=-
0.85421,-0.48965,-0.14563,0.35487,0.63214

This would take the file ACP_scores1 as input and display 
'communes' with its areas divided into 6 classes:

1: < -0.85421
2: -0.85421 -> -0.48965
3: -0.48964 -> -0.14563
4: -0.14562 -> 0.35487
5: 0.35488 -> 0.63214
6: > 0.63215 


The catfile should look like this:

catnum1 variable
catnum2 variable
catnum3 variable
etc... (can be integer or floating point variable values).

So in my example it would be

11001 -0.14568
11002 0.32287
11003 -0.24587
etc...

The script then displays each category in greyscale, first=lightest, 
last =darkest. It can be used in conjunction with the PNG driver in 
order to rapidly create thematic map output.

Just save the script into a file called d.area.class (or anything else 
you like). Make it executable (chmod 0755 d.area.class) and run it 
from within Grass.

Moritz

#*********script begin***********************
#! /bin/sh
# script d.area.class
# displays vector maps in grayscales according 
to given class boundaries
# written by Moritz Lennert - 2002
# mlennert at club.worldonline.be


if test "$GISBASE" = ""; then
echo "You must be in GRASS to run this program."
exit
fi

if [ $# != 3 -o "$1" = "-h" -o "$1" = "-help" -o 
"$1" = "help" ]
then
    echo ""
    echo "Usage: d.area.class map=name 
catfile=name classes=boundary1[,boundary2,..]"
    echo ""
    echo "Parameters:"
    echo "      map = vector map to display"
    echo "  catfile = variable file with 2 
columns seperated by whitespace: 1=category 
2=variable by which to class)"
    echo "  classes = class boundaries separated 
by comma (but no whitespaces), starting with 
boundary between class 1 and 2 and ending with 
boundary between class max-1 and max" 
    echo ""
    exit
fi

for i
do
        case $i in
                m=*|ma=*|map=*)
                                MAP=`echo $i | 
awk -F '=' '{print $2}'` ;;

                
ca=*|cat=*|catf=*|catfi=*|catfil=*|catfile=*) 
                                CATFILE=`echo 
$i| awk -F '=' '{print $2}'` ;;

                
cl=*|cla=*|clas=*|class=*|classe=*|classes=*) 
                                CLASSES=`echo $i 
| awk -F '=' '{print $2}'` ;;
        esac
done


if [ ! "$MAP" ] 
then
        echo
        echo "No vector map name given"
        echo
        exit 1
fi


if [ ! "$CATFILE" ] 
then
        echo
        echo "No variable file name given"
        echo
        exit 1
fi

if [ ! "$CLASSES" ]
then
        echo
        echo "No class boundaries given"
        echo
        exit 1
fi


export MAP
export CLASSES


awk '

BEGIN{

map=ENVIRON["MAP"]

#num is number of classes of which the 
boundaries are put into array 'classes'
num=(split(ENVIRON["CLASSES"], classes, /,/) +1)
}

# create the lists of catnums for the d.area 
command according to class boundaries
{
for(i=1;i<=num;i++) {
    if(i==1){
        if($2<=classes[i]) catnum[i]=catnum[i] 
sprintf("%s,",$1)
    }else{
     if(i==num){
        if($2>classes[i-1]) catnum[i]=catnum[i] 
sprintf("%s,",$1)
     }else{
        if($2>classes[i-1] && $2 <= classes[i]) 
catnum[i]=catnum[i] sprintf("%s,",$1)
     }
}
}
}
 
END{

for(i=1;i<=(num);i++){

#calculate greyshade
   col=230-((230-20)/(num)*i)

#create the d.area command
   syscom=sprintf("d.area map=%s linecolor=black 
fillcolor=\"rgb(%i %i %i)\" catnum=", map, col, 
col, col)
   syscom = syscom catnum[i]

#avoid empty catnum list since this would 
display entire vector file
   if(length(catnum[i])>0){ 
        print "displaying class " i
        system(syscom)
        } else {
        printf"class number %d is empty\n", i
        }
   }
}
' $CATFILE 
#************script end**************



More information about the grass-user mailing list