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

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"

Sorry, I forgot to control region settings.
This is the correct script:

#!/bin/sh

############################################################################
#
# MODULE: r.out.jpeg
#
# AUTHOR(S): Giovanni Allegri <giohappy AT gmail.com
#
# PURPOSE: exports a 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

exec `g.region rast="$RAST"`
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 }' `

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

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"

G. Allegri wrote:

exec `g.region rast="$RAST"`

This is bogus on two counts. First, g.region doesn't output a shell
command, so the use of exec and backticks makes no sense. Second,
shell scripts shouldn't change the current region; they should create
a temporary region with "g.region ... save=..." and use WIND_OVERRIDE.

--
Glynn Clements <glynn@gclements.plus.com>

Thanks Glynn. I'm writing my first grass scripts...
About region settings do you mean to use something like this?:

g.region -u rast=$RAST save=region.tmp.$$
WIND_OVERRIDE=region.tmp.$$
export WIND_OVERRIDE
(my code)
g.remove region=region.tmp.$$

The exec command obviously was an error!

Giovanni

2008/1/6, Glynn Clements <glynn@gclements.plus.com>:

G. Allegri wrote:

> exec `g.region rast="$RAST"`

This is bogus on two counts. First, g.region doesn't output a shell
command, so the use of exec and backticks makes no sense. Second,
shell scripts shouldn't change the current region; they should create
a temporary region with "g.region ... save=..." and use WIND_OVERRIDE.

--
Glynn Clements <glynn@gclements.plus.com>

G. Allegri wrote:

Thanks Glynn. I'm writing my first grass scripts...
About region settings do you mean to use something like this?:

g.region -u rast=$RAST save=region.tmp.$$
WIND_OVERRIDE=region.tmp.$$
export WIND_OVERRIDE
(my code)
g.remove region=region.tmp.$$

That looks about right.

--
Glynn Clements <glynn@gclements.plus.com>