[GRASS5] ps.atlas

Jachym Cepicky jachym.cepicky at centrum.cz
Tue Jul 12 04:49:56 EDT 2005


hallo,

I fixed most of notes from Hamish (thank you) and I added some more
options. The script can be turned to be less verbosed (redirecting
messages to /dev/null or /dev/stdout), but I'm not shure, if this works
e.g. on Mac Os or Cygwin.

Any other bugs/wishes?

Jáchym

On Tue, Jul 12, 2005 at 11:41:23AM +1200, Hamish wrote:
> another little simplification, if you use 'v.in.ascii -n' (6.1-cvs
> only) you can skip the header stuff:
> 
> > ORGANIZATION:
> > DIGIT DATE:
> etc.
> 
> 
> awk is more likely to be installed vs. bc for floating point math.
> e.g.   ANSWER=`echo "1.23 2.34" | awk '{print $1 + $2}'`
> 
> not as easy to read as bc input though so maybe better to leave it.
> 
> 
> 
> I'm not sure if it's a good idea to overwrite an existing vector file
> without asking if it already exists (when the module is no longer in the
> development stage). Exit with an error instead.
> 
> 
> > ps.map out=$out.eps
> (1st time)
> 
> you didn't use the ps.map -e flag so output is .ps not .eps
> 
> scale= is a mapping instruction so best put in the ps.map command file
> not on the command line. It is my intention to remove this from the
> command line options in future. (will leave for GRASS 6 though)
> 
> 
> > eval `g.region -p|grep '^north\|^east
> etc.
> 
> use    eval `g.region -g`
> 
> 
> > if [ "$cols" == "" ]; then
> 
> might break on cygwin?
> 
> use  if [ -z "$cols" ] ; then
> ...
> 
> 
> >   currow=$(( $currow+1 ))
> 
> $(( )) is bash only.
> 
> Use  currow=`expr $currow + 1`
> 
> for a more portable script.
> 
> 
> 
> Hamish

-- 
Jachym Cepicky
e-mail: jachym.cepicky at centrum.cz
URL: http://les-ejk.cz
GPG: http://les-ejk.cz/gnupg_public_key/
-------------- next part --------------
#!/bin/sh

############################################################################
# script:	ps.atlas
# author:	Jachym Cepicky <jachym.cepicky at centrum.cz>
# description:	Makes "atlas of maps" of region for defined number of rows 
#               and columns
# date:		2005-07-12
# copyright:	This program is free software under the GNU General Public
#		License (>=v2). 
# needs:        - sed
#               - grep
#               - awk
#               - bc
#               - ps2pdf
#############################################################################

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

#%Module
#% description: Prints atlas of maps with ps.map
#%end
#%option
#%  key: input
#%  type: string
#%  description: Mapfile for ps.map
#%  required : yes
#%End
#%option
#% key: output
#% type: string
#% description: Prefix of output files
#% required : yes
#%End
#%option
#% key: rows
#% type: integer
#% description: Number of rows in region
#% required : yes
#% answer: 1
#%End
#%option
#% key: cols
#% type: integer
#% description: Number of columns in region, maximum is 676
#% required : yes
#% answer: 1
#%End
#%option
#% key: overlap
#% type: integer
#% description: Overlap in map units
#% answer: 0
#% required : no
#%End
#%option
#% key: format
#% type: string
#% description: Format of the paper (a3, a4,...) (for ps2pdfwr).
#% answer: a4
#% required : no
#%End
#%flag
#% key: p
#% description: Create pdf
#%end
#%flag
#% key: v
#% description: Create vector file, which will contain the map boundaries
#%end
#%flag
#% key: r
#% description: Rotate plot
#%End
#%flag
#% key: o
#% description: Overwrite the vector file, if it exists
#%End
#%flag
#% key: d
#% description: Debuging - all messages are printed out.
#%End



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

#  sed
if [ -z "`which sed`" ] ; then
   echo "ERROR: Script needs sed."
   exit 1
fi

#  grep
if [ -z "`which grep`" ] ; then
   echo "ERROR: Script needs grep."
   exit 1
fi

#  awk
if [ -z "`which awk`" ] ; then
   echo "ERROR: Script needs awk."
   exit 1
fi

#  bc
if [ -z "`which bc`" ] ; then
   echo "ERROR: Script needs bc."
   exit 1
fi

#  ps2pdfwr
if [ -z "`which ps2pdfwr`" ] && [ $GIS_FLAG_p -eq 1 ]; then
   echo "ERROR: Script needs ps2pdfwr."
   exit 1
fi

# input file
if [ ! -e "$GIS_OPT_input" ]; then
    echo "ERROR: Input file $GIS_OPT_input does not exist."
    exit 1;
fi

# there can be only 'A' to 'ZZ' columns
if [ "$GIS_OPT_cols" -gt "702" ]; then
    echo "WARNING: Maximum number of columns is 676."
    echo "WARNING: You desired $input of columns."
    echo "WARNING: I will use 702 columns for this region."
    GIS_OPT_cols=702;
fi

# redirecting messages to stdout or /dev/null
if [ "$GIS_FLAG_d" -eq "1" ]; then
    OUT="/dev/stdout"
else
    OUT="/dev/null"
fi
    
## vars
eval `g.gisenv`
: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
input="$GIS_OPT_input"
out="$GIS_OPT_output"
rows="$GIS_OPT_rows"
cols="$GIS_OPT_cols"
rotate=""
overlap="$GIS_OPT_overlap"
oldregion="region_$$"
newvector=$out"_atlas"
TMP1="`g.tempfile pid=$$`"
rows_x_cols=`echo $rows $cols|awk '{print $1*$2}'`;

# removing vector, if exists
# create vector && overwrite old && vector exists
if [ "$GIS_FLAG_v" -eq "1" ] && [ "$GIS_FLAG_o" -eq "1" ] && [ -n "`g.mlist type=vect mapset=$MAPSET pattern=$newvector`" ]; then
    echo "Vector exists, removing..."
    g.remove vect=$newvector 1>$OUT
else

    # create vector && NOT overwrite old && vector exists
    if [ "$GIS_FLAG_v" -eq "1" ] && [ -n "`g.mlist type=vect mapset=$MAPSET pattern=$newvector`" ] ; then
        echo "ERROR: Vector file $newvector allready exists, remove it first!"
        exit 1;
    fi
fi

# rotate flag
if [ $GIS_FLAG_r -eq 1 ]; then
    rotate="-r";
fi

# informing
echo "Running: ps.map -e $rotate in=$input out=$out.eps"
 
# use only hlaf of overlaping
overlap=`echo "$overlap"|awk '{printf("%f", $1/2);}'`;
#######################################################################
cleanup() 
{
    g.region $oldregion 
    rm -f $GISDBASE/$LOCATION_NAME/$MAPSET/windows/$oldregion
    rm -f $TMP1 
}

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


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

g.region save=$oldregion

# must be because of awk and non-english locale
export LANG=C

# setign maxe, maxn, maxw and maxs variables with help of g.region
eval `g.region -g| sed -e s/^/max/`;

# be simple ps.map
if [ -z "$cols" ]; then
    cols=1;
fi

if [ -z "$rows" ]; then
    rows=1;
fi

########################################################################
north=$maxn;
west=$maxw;


# size of steps
estep=`echo $maxe $maxw $cols | awk '{printf("%f",($1-$2)/$3);}'`;
nstep=`echo $maxn $maxs $rows | awk '{printf("%f",($1-$2)/$3);}'`;

# setting first region
south=`echo $north $nstep | awk '{printf("%f",$1-$2)}'`;
east=`echo  $west $estep  | awk '{printf("%f",$1+$2)}'`;

# number of region
no_of_region=0;

### ascii file and db. table
if [ $GIS_FLAG_v -eq 1 ]; then
    echo "Creating ascii file + vector table"

    ### creating database
    echo "CREATE TABLE $newvector (cat int, region int, label varchar(10), row int, col varchar(5), east double, north double, west double, south double);"| db.execute
fi

# inicialization
currow=0;

### rows N-S
while [ "`echo $south $maxs |awk '{printf("%d", $1 >= $2);}'`" -eq "1" ];
do 
    echo "north->south" 1>$OUT    # test

    # number of current row
    currow=`expr $currow + 1`;

    # inicialization
    curcol=0;

    # columns W-E
    while [ "`echo $east $maxe |awk '{printf("%d", $1 <= $2);}'`" -eq "1" ];
    do
        echo "west->east"  1>$OUT # test 

        # number of current column
        curcol=`expr $curcol + 1`;

        # charakter of current column
        if [ "$curcol" -gt "26" ]; then
            pre_alpha=`echo $curcol/26| bc`;
            curcol_alpha=`echo $pre_alpha $curcol|awk '{printf("%c%c",$1+64, $2-26+64);}'`;
        else 
            curcol_alpha=`echo $curcol|awk '{printf("%c",$1+64);}'`;
        fi

        # overlapping 10% = 5% on both sides
        over_north=`echo $north $overlap|awk '{printf("%f", $1+$2);}'`;
        over_west=`echo $west $overlap|awk '{printf("%f", $1-$2);}'`;
        over_east=`echo $east $overlap|awk '{printf("%f", $1+$2);}'`;
        over_south=`echo $south $overlap|awk '{printf("%f", $1-$2);}'`;
        east_center=`echo "$over_east+(($over_west)-($over_east))/2" |bc `
        north_center=`echo "$over_south+(($over_north)-($over_south))/2" |bc `

        g.region n=$over_north s=$over_south w=$over_west e=$over_east;
        
        west=$east
        east=`echo "$west $estep" | awk '{print $1 + $2}'`;
        
        no_of_region=`expr $no_of_region + 1`;
        echo "-------------- REGION NUMBER $no_of_region OF $rows_x_cols ---------------" 1>$OUT

        # storing coordinaes to ascii file
        if [ $GIS_FLAG_v -eq 1 ]; then
            echo "B 5" >> $TMP1
            echo "$over_east $over_north" >> $TMP1
            echo "$over_east $over_south" >> $TMP1
            echo "$over_west $over_south" >> $TMP1
            echo "$over_west $over_north" >> $TMP1
            echo "$over_east $over_north" >> $TMP1
            echo -e "C 1 1\n$east_center $north_center\n1 $no_of_region" >> $TMP1
            echo "INSERT INTO $newvector (cat, region, col, row, label, north, east, south, west) VALUES ($no_of_region,$no_of_region, '$curcol_alpha', $currow, '$curcol_alpha$currow', $over_north, $over_east, $over_south, $over_west);"|db.execute
        fi

        #################################################################
        # create ps & pdf
        #################################################################
        ps.map -e $rotate in=$input out=$out$no_of_region.eps 1>$OUT
        if [ "$GIS_FLAG_p" = "1" ]; then
            ps2pdfwr -sPAPERSIZE=$GIS_OPT_format $out$no_of_region.eps
        fi
        #################################################################
        # 
        #################################################################
    
        # print progres
        echo "$no_of_region $rows_x_cols" | awk '{printf("\r%d%%",$1/$2*100);}' 1>&1
        
    done
    
    north=$south
    south=`echo "$north $nstep" |awk '{print $1 - $2}'`;
    
    # go back to west!
    west=$maxw
    east=`echo "$west $estep" | awk '{print $1 + $2}'`;
done;

if [ $GIS_FLAG_v -eq 1 ]; then
    # import the vector of regions
    outtmp="$newvector"_tmp
    v.in.ascii -n in=$TMP1 out=$outtmp format=standard  1>$OUT 2>&1
    v.clean tool=break,rmdupl in=$outtmp out=$newvector  1>$OUT 2>&1
    g.remove vect=$outtmp 1>$OUT 2>&1
    v.db.connect map=$newvector table=$newvector  1>$OUT 2>&1
fi

### cleaning
cleanup


More information about the grass-dev mailing list