[GRASS5] d.vect.leg

Jachym Cepicky jachym.cepicky at centrum.cz
Tue May 17 12:48:44 EDT 2005


here I post new version with patch from Martin. 

We have one problem: 

if one uses "for line in `echo "SELECT...."|db.select` 

and if db.select returns more words from the dabase, each string is handeld as
separate variable $line


nlines=0;
new_cat=0;

# searching for categories
for line in `echo "SELECT $GIS_OPT_column,GRASSRGB FROM $table ORDER BY grassrgb"|db.select -c fs=";"`;
do
    eval `echo "$line"|sed -e s/^/cat=\"/|sed -e s/\;/\"\;\ color=\"/|sed -e s/$/\"/`

    # is this new category?
    for index in `seq 1 $nlines`; do
        cur_cat=${cat_vals[$index]}
        #echo "#$cat# #$cur_cat#"
        if [ "$cur_cat" == "$cat" ]; then
            new_cat=1;
        fi
    done

    # if it is new category, add to arrays cat_vals and cat_clrs
    if [ "$new_cat" == "0"  ]; then
        index=`expr $index + 1`;
        cat_vals[$index]="$cat";
        cat_clrs[$index]="$color";
        nlines=`expr $nlines + 1`;
        new_cat=0;
    fi
    #echo $nlines # if you use 'echo "SELECT ..."|while read line' here it is OK
done
#echo $nlines # but here it gots value from line no 125


we tryed to implement something like

echo "SELECT $GIS_OPT_column...."|db.execute| while read line; do

...

done

hoewer the $nlines variable gets its original value (=0) :-(

any idea ?

Jachym


On Tue, May 17, 2005 at 08:09:24AM -0700, Michael Barton wrote:
> tatus: RO
> Content-Length: 828
> Lines: 35
> 
> I'll take a look at it.
> 
> 
> On 5/17/05 2:50 AM, "Jachym Cepicky" <jachym.cepicky at centrum.cz> wrote:
> 
> > Hallo developers,
> > before I rewrite d.legend so it is able to work with vector maps too (and I'm
> > still not sure, if I ever get it), i wrote a script, based on d.vect.thematic,
> > which should solve as d.vect.leg. It does the same, as d.rast.leg does too.
> > The
> > difference from d.vect.thematic is, that this script works with GRASSRGB
> > column
> > from the database.
> > 
> > Anyone for testing?
> > 
> > Screenshot at http://les-ejk.cz/.../d.vect.leg.png
> > 
> > Jáchym
> 
> ____________________
> C. Michael Barton, Professor of Anthropology
> School of Human Evolution and Social Change
> PO Box 872402
> Arizona State University
> Tempe, AZ  85287-2402
> USA
> 
> Phone: 480-965-6262
> Fax: 480-965-7
-- 
Jachym Cepicky
e-mail: jachym.cepicky at centrum.cz
URL: http://les-ejk.cz
GPG: http://www.fle.czu.cz/~jachym/gnupg_public_key/
-------------- next part --------------
#!/bin/sh
#
############################################################################
#
# MODULE:       d.vect.leg
# AUTHOR(S):	Jachym Cepicky, Ustav geoinformacnich technologii, MZLU v
#               Brne, Czech Rep.
# PURPOSE:	Displays legend from database table
# COPYRIGHT:	(C) 2005 by the GRASS Development Team
#
#		This program is free software under the GNU General Public
#		License (>=v2). Read the file COPYING that comes with GRASS
#		for details.
#
#############################################################################


#%Module
#%  description: Displays  a  legend  for a vector map layer in the active frame on the graphics monitor.
#%End
#%option
#% key: map
#% type: string
#% gisprompt: old,vector,vector
#% description: Vector map to display legend
#% required : yes
#%end
#%option
#% key: column
#% type: string
#% description: Column to use for thematic display (must be numeric)
#% required : yes
#%end
#%option
#% key: layer
#% type: integer
#% description: Layer to use for thematic display
#% answer: 1
#% required : no
#%end
#%option
#% key: type
#% type: string
#% description: Vector map type
#% options: area,point,centroid,line,boundary
#% answer: area
#% required : yes
#%end
 


if  [ -z $GISBASE ] ; then
 echo "You must be in GRASS GIS to run this program."
 exit 1
fi   

if [ "$1" != "@ARGS_PARSED@" ] ; then
  exec g.parser "$0" "$@"
fi

# setting environment, so that awk works properly in all languages
export LC_NUMERIC=C

# create temporary file to hold d.graph commands for legend
TMP="`g.tempfile pid=$$`"
if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
    echo "ERROR: unable to create temporary files" 1>&2
    exit 1
fi

cleanup()
{
    echo -n ""
 # echo $TMP
 \rm -f $TMP
}

# what to do in case of user break:
exitprocedure()
{
 echo "User break!"
 cleanup
 exit 1
}
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

table=`v.db.connect $GIS_OPT_map -g | grep -w $GIS_OPT_layer | awk '{print $2}'`
if [ -z "$table" ]
    then
    echo "No table connected or layer <$GIS_OPT_layer> doesn't exist!"
    exit 1
fi


d.frame -e
d.frame -s at=0,100,0,65 frame=left
d.vect -a map=$GIS_OPT_map type=$GIS_OPT_type layer=$GIS_OPT_layer

d.frame -s at=0,100,65,100 frame=right
 
heading="$GIS_OPT_map: $GIS_OPT_column"
head_length=`echo $heading | wc -m`;

echo "color black"      > $TMP
echo "size 5 2"         >>$TMP 
echo "move 5 95"        >>$TMP 
echo "text $heading"    >>$TMP
echo "move 5 93"        >>$TMP
echo -en "text "        >>$TMP

# print underline
for ((i=1; i < $head_length ; i++)); do 
    echo -n "-" >> $TMP
done
echo "" >>$TMP # new line
echo "size 4.5 1.8"     >>$TMP

# layout the legend
step=6
line1=90
line2=88
line3=86

nlines=0;
new_cat=0;

# searching for categories
for line in `echo "SELECT $GIS_OPT_column,GRASSRGB FROM $table ORDER BY grassrgb"|db.select -c fs=";"`;
do
    eval `echo "$line"|sed -e s/^/cat=\"/|sed -e s/\;/\"\;\ color=\"/|sed -e s/$/\"/`

    # is this new category?
    for index in `seq 1 $nlines`; do
        cur_cat=${cat_vals[$index]}
        #echo "#$cat# #$cur_cat#"
        if [ "$cur_cat" == "$cat" ]; then
            new_cat=1;
        fi
    done

    # if it is new category, add to arrays cat_vals and cat_clrs
    if [ "$new_cat" == "0"  ]; then
        index=`expr $index + 1`;
        cat_vals[$index]="$cat";
        cat_clrs[$index]="$color";
        nlines=`expr $nlines + 1`;
        new_cat=0;
    fi
    #echo $nlines # if you use 'echo "SELECT ..."|while read line' here it is OK
done
#echo $nlines # but here it gots value from line no 125


# print the legend
for cat in `seq 1 $nlines`; do 

    echo "color ${cat_clrs[$cat]}"      >>$TMP
    echo "polygon"              >>$TMP
    echo "5 $line1"             >>$TMP
    echo "15 $line1"             >>$TMP
    echo "15 $line3"             >>$TMP
    echo "5 $line3"             >>$TMP
    echo "color black"          >>$TMP
    echo "move 16 $line2"        >>$TMP
    echo "text ${cat_vals[$cat]}"       >>$TMP

    line1=`expr $line1 - $step`;
    line2=`expr $line2 - $step`;
    line3=`expr $line3 - $step`;

done
d.graph input=$TMP
d.frame -s frame=left
cleanup


More information about the grass-dev mailing list