[GRASS-user] script to generate georeferenced jpeg colored images from FCELL/CELL rasters

G. Allegri giohappy at gmail.com
Fri Jan 4 18:20:29 EST 2008


The following simple script generates georeferenced jpeg images from
FCELL/CELL rasters, mmantaining the color table associated to the
raster.
It needs ImageMagick to be installed (convert command-line tool).
As soon as I find some minutes I will upload it to the Add-On Wiky.

Hints to improve it are wellcome!

Giovanni


#!/bin/sh

############################################################################
#
# MODULE:       r.out.jpeg
#
# AUTHOR(S):    Giovanni Allegri <giohappy AT gmail.com
#
# PURPOSE:      exports a georeferenced jpeg image from an FCELL raster
mantaing the color table
#
# COPYRIGHT:    (c) 2008 Giovanni Allegri
#
#               This program is free software under the GNU General Public
#               License (>=v2). Read the file COPYING that comes with GRASS
#               for details.
#
# updates:
#
#4 January 2008: first version
#
#############################################################################
#
#%Module
#%  description: exports a georeferenced jpeg from an FCELL raster
#%End
#%option
#%  key: rastmap
#%  type: string
#%  gisprompt: old,cell,raster
#%  description: Input FCELL Raster
#%  required : yes
#%end
#%option
#%  key: output
#%  type: string
#%  description: Output image (path_to_output_folder/filename_without_extension)
#%  required : yes
#%end


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

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

RAST=$GIS_OPT_RASTMAP
OUTPATH=${GIS_OPT_OUTPUT%/*}
tmp=${GIS_OPT_OUTPUT##*/}
OUTPUT=$OUTPATH'/'${tmp%.*}

# check if we have awk
if [ ! -x "`which awk`" ] ; then
   echo "$PROG: awk required, please install awk first" 2>&1
   exit 1
fi

# check if we have ImageMagick
if [ ! -x "`which convert`" ] ; then
   echo "$PROG: imagemagick is required, please install it first" 2>&1
   exit 1
fi

#check if the specified map exists
g.findfile element=cell file="$RAST" > /dev/null
if [ "$?" -ne 0 ] ; then
 echo "Raster map '$RAST' not found in mapset search path"
 exit 1
fi

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

RRES=`r.info -s map=$RAST | head -1 | awk 'BEGIN { FS="=" } { print $2 }' `
RNORTH=`r.info -g map=$RAST | awk 'BEGIN {FS="="} { if(NR==1)  print $2 }' `
RWEST=`r.info -g map=$RAST | awk 'BEGIN {FS="="} { if(NR==4)  print $2 }' `

# the first pass conversion generates a temporary ppm (portable pixmax format)
exec `r.out.ppm -q in="$RAST" out="$OUTPUT.ppm"`
if [ "$?" -ne 0 ] ; then
 echo "Jpeg Image '$OUTPUT' could not be created"
 exit 1
fi

# convert command-line tool (ImageMagick) is used as the second pass
to convert ppm to jpeg format
exec `convert $OUTPUT'.ppm' $OUTPUT'.jpg'`
if [ "$?" -ne 0 ] ; then
 echo "Jpeg Image '$OUTPUT' could not be created"
 exit 1
fi

echo $RRES'\n0\n0\n-'$RRES'\n'$RWEST'\n'$RNORTH > $OUTPUT'.jgw'

exec `rm $OUTPUT'.ppm'`
echo "$OUTPUT.jpg has been created"


More information about the grass-user mailing list