#!/bin/sh ############################################################################ # # MODULE: r.in.proj.gdal for GRASS 6 # AUTHOR(S): Cedric Shock # PURPOSE: To project and import data # COPYRIGHT: (C) 2006 by Cedric Shock # # This program is free software under the GNU General Public # License (>=v2). Read the file COPYING that comes with GRASS # for details. # ############################################################################# #%Module #% description: Imports data, projects it into current region, and patches #%End #%flag #% key: k #% description: Keep band numbers instead of using band color names #%end #%option #% key: input #% type: string #% description: Raster file or files to be imported. If multiple files are specified they will be patched together. #% multiple: yes #% required : yes #%end #%option #% key: output #% type: string #% description: Name for resultant raster map. Each band will be name output.bandname #% required : yes #%end #%option #% key: location #% type: string #% description: Name of location (in source projection) to use to import data. #% required: yes #%end #%option #% key: mapset #% type: string #% description: Mapset in location to use to import data. #% answer: PERMANENT #% required: yes #%end #%option #% key: method #% type: string #% description: Reprojection method to use #% options:nearest,bilinear,cubic #% answer:nearest #% required: yes #%end #%option #% key: v #% type: integer #% description: Verbosity level #% answer: 1 #%end if [ -z $GISBASE ] ; then echo "You must be in GRASS GIS to run this program." 1>&2 exit 1 fi if [ "$1" != "@ARGS_PARSED@" ] ; then exec g.parser "$0" "$@" fi #### setup temporary file TMP="`g.tempfile pid=$$`" if [ $? -ne 0 ] || [ -z "$TMP" ] ; then echo "ERROR: unable to create temporary files" 1>&2 exit 1 fi defaultIFS=$IFS eval `g.gisenv` OLD_MAPSET=$MAPSET OLD_LOCATION=$LOCATION_NAME ####################################################################### # name: exitprocedure # purpose: removes all temporary files # exitprocedure() { echo "User break!" 1>&2 rm -f ${TMP}* # Switch locations g.mapset location=${OLD_LOCATION} mapset=${OLD_MAPSET} exit 1 } trap "exitprocedure" 2 3 15 ##################### # name: message # purpose: displays messages to the user # usage: message level text message () { if [ $1 -lt $GIS_OPT_v ] ; then shift echo "$@" 1>&2 fi } # Compute flags for r.in.gdal FLAGS="" if [ $GIS_FLAG_k -eq 1 ] ; then FLAGS=" -k" fi message 0 "Changing location to ${GIS_OPT_location} and mapset to ${GIS_OPT_mapset}" # Switch locations g.mapset location=${GIS_OPT_location} mapset=${GIS_OPT_mapset} message 0 "Importing data" # Import data IFS=, SUFFIXES="" # We need a way to make sure patches are intialized correctly TITER=0 for FILE in $GIS_OPT_input ; do IFS=$defaultIFS TILENAME=${GIS_OPT_output}_tile_$TITER if [ -f "$FILE" ] ; then r.in.gdal $FLAGS input="$FILE" output="$TILENAME" -o else message 0 "Missing input file: $input" fi ((TITER++)) done # Get a list of the raster maps in this mapset TILELIST=`g.mlist type=rast mapset=${GIS_OPT_mapset} pattern="${GIS_OPT_output}_tile*"` message 0 "Returning to our location" # Switch back location g.mapset location=${OLD_LOCATION} mapset=${OLD_MAPSET} message 0 "Projecting data" IFS=$defaultIFS for TILE in $TILELIST ; do message 0 "Projecting tile $TILE" r.proj input=$TILE location=${GIS_OPT_location} mapset=${GIS_OPT_mapset} method=${GIS_OPT_method} done # Delete tiles in previous location # Switch locations g.mapset location=${GIS_OPT_location} mapset=${GIS_OPT_mapset} message 0 "Removing tiles from source location" TILELIST_COMMA=`echo $TILELIST | sed "s/ /,/g"` g.remove rast=$TILELIST_COMMA # Switch back location g.mapset location=${OLD_LOCATION} mapset=${OLD_MAPSET} message 0 "Patching data" # Get a list of tiles that projected correctly: NEWTILELIST=`g.mlist type=rast mapset=${OLD_MAPSET} pattern="${GIS_OPT_output}_tile*"` # Get a list of suffixes: SUFFIXES=`echo "$NEWTILELIST" | sed "s/^${GIS_OPT_output}_tile_[0-9]*/sfx=/" | sort -u` IFS=$defaultIFS # For each suffix (including blank line if it's found) COLORS=0 for SUFFIX in $SUFFIXES ; do eval "$SUFFIX" message 0 "Patching [$sfx] channel" MEMBERS_NL=`echo "$NEWTILELIST" | grep "^${GIS_OPT_output}_tile_[0-9]*$sfx$"` MEMBERS=`echo $MEMBERS_NL | sed "s/ /,/g"` #MEMBERS="" #for MEMBER in $MEMBERS_NL ; do # MEMBERS=$MEMBERS,$MEMBER #done # Count the members. If there is one rename it; if there are more patch it. # This is one less than number of members MEMBER_COUNT=`echo "$MEMBERS" | grep -F -c ,` if [ $MEMBER_COUNT -eq 0 ] ; then # Rename the tile g.rename rast=$MEMBERS,${GIS_OPT_output}${sfx} else # Patch the data r.patch input=$MEMBERS output=${GIS_OPT_output}${sfx} # Delete the temporary tiles g.remove rast=$MEMBERS fi if [ -z $sfx ] ; then # There's already a no suffix, can't make colors COLORS=4 # Can only go up from here ;) fi if [ "$sfx" = ".red" ] || [ "$sfx" = ".green" ] || [ "$sfx" = ".blue" ]; then ((COLORS++)) fi done if [ $COLORS -eq 3 ] ; then message 0 "Building Color Image" r.composite red=${GIS_OPT_output}.red green=${GIS_OPT_output}.green blue=${GIS_OPT_output}.blue output=${GIS_OPT_output} fi # Clean up: rm -f ${TMP}*