[GRASS-SVN] r30636 - grass/branches/releasebranch_6_3/scripts/r.in.wms

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Mar 19 06:58:26 EDT 2008


Author: hamish
Date: 2008-03-19 06:58:26 -0400 (Wed, 19 Mar 2008)
New Revision: 30636

Modified:
   grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.gdalwarp
   grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.wms
   grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.download
   grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.request
Log:
merge updates from HEAD

Modified: grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.gdalwarp
===================================================================
--- grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.gdalwarp	2008-03-19 10:51:18 UTC (rev 30635)
+++ grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.gdalwarp	2008-03-19 10:58:26 UTC (rev 30636)
@@ -132,7 +132,7 @@
 	fi
 
 	#Import it into a temporary map:
-	r.in.gdal $FLAGS input="$WARPFILE" output="$TMPMAPNAME"
+	r.in.gdal $FLAGS input="$WARPFILE" output="$TMPMAPNAME" --quiet
 	if [ $? -ne 0 ] ; then
 	    g.message -e message="`basename $0`: r.in.gdal failure."
 	    rm -f "${TMP}"*
@@ -144,9 +144,16 @@
 
 	#Get a list of channels:
 	PATTERN="$TMPMAPNAME*"
+	g.message -d message="Pattern: [$PATTERN]"
 	CHANNEL_LIST=`g.mlist type=rast pattern=$PATTERN`
+	g.message -d message="Channel list: [$CHANNEL_LIST]"
 	# Get a list of suffixes:
 	CHANNEL_SUFFIXES=`echo "$CHANNEL_LIST" | sed "s/^${TMPMAPNAME}/sfx=/"`
+	# test for single band data
+	if [ "$CHANNEL_SUFFIXES" = 'sfx=' ] ; then
+	   CHANNEL_SUFFIXES=""
+	fi
+	g.message -d message="Channel suffixes: [$CHANNEL_SUFFIXES]"
 
 	# Add to the list of all suffixes:
 	SUFFIXES=`echo "$SUFFIXES
@@ -170,6 +177,7 @@
 		eval "$SUFFIX"
 
 		# if no suffix, processing is simple (e.g. elevation has only 1 band)
+		# --never reached?
 		if [ -z "$sfx" ] ; then
 		    # run r.mapcalc to crop to region
 		    GRASS_VERBOSE=1 \
@@ -182,7 +190,7 @@
 		  r.mapcalc "${MAP}${sfx} = if( $ALPHALAYER, ${TMPMAPNAME}${sfx}, null() )"
 
 		# Copy the color tables:
-		r.colors map=${MAP}${sfx} rast=${TMPMAPNAME}${sfx}
+		r.colors map=${MAP}${sfx} rast=${TMPMAPNAME}${sfx} --quiet
 
 		# Make patch lists:
 		sfx2=`echo $sfx | sed "s/\./_/"`
@@ -193,16 +201,24 @@
 		eval "PATCHES_$sfx2=\${PATCHES_$sfx2},${MAP}${sfx}"
 	done
 
+	# if no suffix, processing is simple (e.g. elevation has only 1 band)
+	if [ -z "$sfx" ] ; then
+	    # run r.mapcalc to crop to region
+	    GRASS_VERBOSE=1 \
+	      r.mapcalc "${MAP}${sfx} = ${TMPMAPNAME}${sfx}"
+	    r.colors map=${MAP}${sfx} rast=${TMPMAPNAME}${sfx} --quiet
+	fi
+
 	# Remove the old channels:
-	CHANNEL_LIST_COMMA=`echo $CHANNEL_LIST | sed "s/ /,/g"`
-	g.remove rast=$CHANNEL_LIST_COMMA
+	CHANNEL_LIST_COMMA=`echo "$CHANNEL_LIST" | sed "s/ /,/g"`
+	g.remove rast="$CHANNEL_LIST_COMMA" --quiet
 }
 
 nowarpimport () {
 	FILE=$1
 	MAP=$2
 
-	r.in.gdal -o $FLAGS input="$FILE" output="$MAP"
+	r.in.gdal -o $FLAGS input="$FILE" output="$MAP" --quiet
 	if [ $? -ne 0 ] ; then
 	    g.message -e message="`basename $0`: r.in.gdal failure."
 	    rm -f "${TMP}"*
@@ -211,9 +227,17 @@
 
 	#Get a list of channels:
 	PATTERN="$MAP*"
+	g.message -d message="pattern: [$PATTERN]"
 	CHANNEL_LIST=`g.mlist type=rast pattern=$PATTERN`
+	g.message -d message="channel list: [$CHANNEL_LIST]"
+
 	# Get a list of suffixes:
 	CHANNEL_SUFFIXES=`echo "$CHANNEL_LIST" | sed "s/^${MAP}/sfx=/"`
+	# test for single band data
+	if [ "$CHANNEL_SUFFIXES" = 'sfx=' ] ; then
+	   CHANNEL_SUFFIXES=""
+	fi
+	g.message -d message="channel suffixes: [$CHANNEL_SUFFIXES]"
 
 	# Add to the list of all suffixes:
 	SUFFIXES=`echo "$SUFFIXES
@@ -222,14 +246,15 @@
 	IFS="$defaultIFS"
 	for SUFFIX in $CHANNEL_SUFFIXES ; do
 		eval "$SUFFIX"
-		
+
 		# skip if only one band ($sfx is empty)
 		if [ -z "$sfx" ] ; then
+		    g.message -d "Single band data"
 		    continue
 		fi
 
 		# Make patch lists:
-		sfx2=`echo $sfx | sed "s/\./_/"`
+		sfx2=`echo "$sfx" | sed "s/\./_/"`
 		# This is a hack to make the patch lists empty:
 		if [ $TITER -eq 0 ] ; then
 			eval "PATCHES_$sfx2=\"\""
@@ -265,6 +290,9 @@
 SUFFIXES=""
 # We need a way to make sure patches are intialized correctly
 TITER=0
+# map list is for single band patching
+MAPLIST=""
+
 # Import all the tiles:
 for input in $GIS_OPT_INPUT ; do
 	IFS="$defaultIFS"
@@ -275,6 +303,13 @@
 		else
 			warpimport "$input" "$TMPTILENAME"
 		fi
+
+		if [ "$TITER" -eq 0 ] ; then
+		    MAPLIST="$TMPTILENAME"
+		else
+		    MAPLIST="$MAPLIST,$TMPTILENAME"
+		fi
+
 		TITER="`expr $TITER + 1`"
 	else
 		g.message -w message="Missing input $input"
@@ -284,31 +319,75 @@
 
 # If there's more than one tile patch them together, otherwise just rename that tile.
 if [ $NUM_TILES -eq 1 ] ; then
-	for SUFFIX in $CHANNEL_SUFFIXES ; do
+	if [ -n "$CHANNEL_SUFFIXES" ] ; then
+	    for SUFFIX in $CHANNEL_SUFFIXES ; do
 		eval "$SUFFIX"
 		#Rename tile 0 to be the output
 		GRASS_VERBOSE=1 \
 		  g.rename rast="${GIS_OPT_OUTPUT}_tile_0${sfx}","${GIS_OPT_OUTPUT}${sfx}"
-	done
+	    done
+	else  # single-band, single-tile
+	    #Rename tile 0 to be the output
+	    GRASS_VERBOSE=1 \
+	      g.rename rast="${GIS_OPT_OUTPUT}_tile_0${sfx}","${GIS_OPT_OUTPUT}${sfx}" --quiet
+	fi
 else
 	# Patch together each channel:
-	IFS="$defaultIFS"
-	for SUFFIX in $SUFFIXES ; do
+	g.message -d message="SUFFIXES: [$SUFFIXES]"
+
+	if [ -n "$SUFFIXES" ] ; then
+	    # multi-band data
+	    IFS="$defaultIFS"
+	    for SUFFIX in $SUFFIXES ; do
 		eval "$SUFFIX"
-		sfx2=`echo $sfx | sed "s/\./_/"`
+		sfx2=`echo "$sfx" | sed "s/\./_/"`
 		eval "PATCHES=\${PATCHES_$sfx2}"
 		# Patch these together (using nulls only):
-		g.message "Patching [$sfx] channel" 
+		g.message "Patching [$sfx] channel"
 		r.patch input="$PATCHES" output="${GIS_OPT_OUTPUT}${sfx}"
 		# Remove the temporary patches we made
-		g.remove rast="$PATCHES"
-	done
+		g.remove rast="$PATCHES" --quiet
+	    done
+	else
+	    # single-band data
+	    g.message "Patching tiles  (this may take some time)"
+	    g.message -d message="patch list = [$MAPLIST]"
+
+	    # HACK: for 8bit PNG, GIF all the different tiles can have
+	    #   different color tables so patches output end up all freaky.
+	    #	r.mapcalc r#,g#,b# manual patching + r.composite?
+	    #	or d.out.file + r.in.png + r.region?
+
+	    #r.patch input="$MAPLIST" output="${GIS_OPT_OUTPUT}"
+	    IFS=" "
+	    for COLOR in r g b ; do
+		for MAP in `echo "$MAPLIST" | tr ',' ' '` ; do
+		  GRASS_VERBOSE=0 \
+		     r.mapcalc "${MAP}_${COLOR} = ${COLOR}#$MAP"
+	          r.colors  map="${MAP}_${COLOR}" color="grey255" --quiet
+	          r.null "${MAP}_${COLOR}" setnull=255  --quiet # so patching works
+	       done
+
+	       GRASS_VERBOSE=1\
+		  r.patch input=`echo "$MAPLIST" | sed -e "s/,/_${COLOR},/g" -e "s/$/_${COLOR}/"` \
+		     output="${MAP}_${COLOR}_all"
+	    done
+	    r.composite red="${MAP}_r_all" green="${MAP}_g_all" blue="${MAP}_b_all" \
+	       output="${GIS_OPT_OUTPUT}"
+
+	    if [ $? -eq 0 ] ; then
+	        IFS=','
+		for MAP in $MAPLIST ; do
+		   g.mremove -f rast="${MAP}*" --quiet
+		done
+	    fi
+	fi
 fi
 
 # Set up color counter
 COLORS=0
 
-IFS=$defaultIFS
+IFS="$defaultIFS"
 for SUFFIX in $SUFFIXES ; do
 	eval "$SUFFIX"
 	# Keep track of colors

Modified: grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.wms
===================================================================
--- grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.wms	2008-03-19 10:51:18 UTC (rev 30635)
+++ grass/branches/releasebranch_6_3/scripts/r.in.wms/r.in.wms	2008-03-19 10:58:26 UTC (rev 30636)
@@ -29,24 +29,29 @@
 #%end
 #%flag
 #% key: o
-#% description: Don't request transparent data.
+#% description: Don't request transparent data
 #% guisection: Request
 #%end
 #%flag
 #% key: c
-#% description: Clean existing data out of download directory.
+#% description: Clean existing data out of download directory
 #% guisection: Download
 #%end
 #%flag
 #% key: k
-#% description: Keep band numbers instead of using band color names.
+#% description: Keep band numbers instead of using band color names
 #% guisection: Import
 #%end
 #%flag
 #% key: p
-#% description: Don't reproject the data, just patch it.
+#% description: Don't reproject the data, just patch it
 #% guisection: Import
 #%end
+#%flag
+#% key: g
+#% description: Use alternative GET method (try if you have download problems)
+#% guisection: Request
+#%end
 #%option
 #% key: output
 #% type: string
@@ -174,8 +179,14 @@
     exit 1
 fi
 
+# save command line
 if [ "$1" != "@ARGS_PARSED@" ] ; then
-  exec g.parser "$0" "$@"
+    CMDLINE="`basename $0`"
+    for arg in "$@" ; do
+        CMDLINE="$CMDLINE \"$arg\""
+    done
+    export CMDLINE
+    exec g.parser "$0" "$@"
 fi
 
 #### setup temporary file
@@ -219,6 +230,19 @@
     fi
 fi
 
+# turn verbose mode on if requested either way
+if [ "$GIS_OPT_V" -lt 2 ] ; then
+   if [ -n "$GRASS_VERBOSE" ] && [ "$GRASS_VERBOSE" -ge 2 ] ; then
+      GIS_OPT_V=2
+   fi
+else
+   g.message -w "The verbosity option is superseded. Use --verbose instead"
+   if [ -n "$GRASS_VERBOSE" ] && [ "$GRASS_VERBOSE" -lt 2 ] ; then
+      GRASS_VERBOSE=2
+   fi
+fi
+
+
 # Remember the intial field seperator
 defaultIFS="$IFS"
 
@@ -235,7 +259,7 @@
 message () {
 	if [ $1 -lt $GIS_OPT_V ] ; then
 		shift
-		echo "$@"  
+		echo "$@" 1>&2
 	fi
 }
 
@@ -246,7 +270,8 @@
 exitprocedure()
 {
 	g.message -e 'User break!'
-	PATH=$prevPATH
+	# is this bogus? won't PATH revert on exit anyway?
+	PATH="$prevPATH"
 	export PATH
 	\rm -f "${TMP}"*
 	exit 1
@@ -291,50 +316,131 @@
 
 
 ######################
+# name: parse_capabilities
+# purpose: attempt parse XML capabilities file
+parse_capabilities() {
+    # use xml2 if we have it
+    if [ -x "`which xml2`" ] ; then
+	cat "$CAPABILITIES_FILE" | xml2 | \
+	  grep "Layer/SRS=\|Layer/Name=\|Layer/Title=\|Style/Name=\|Style/Title=\|Layer/Abstract=" | \
+	  sed -e "s/.*Layer\\/Name=/LAYER: /" \
+	      -e "s/.*Layer\\/SRS=/  SRS: /" \
+	      -e "s/.*Layer\\/Title=/  Title: /" \
+	      -e "s/.*Layer\\/Abstract=/  |/" \
+	      -e "s/.*Layer\\/Style\\/Name=/  STYLE: /" \
+	      -e "s/.*Layer\\/Style\\/Title=/	 Style title: /" \
+	  > "${TMP}capabilities.txt"
+
+    else : # xml2 is not available
+	g.message -w "The 'xml2' program was not found. Proceeding with internal XML parsing which may not be as reliable."
+	sed -e "s/<Layer/\n<Layer/g" \
+	    -e "s/<Name>/\n<Name>/g" \
+	    -e "s/<Style>/\n<Style>/g" \
+	    -e "s/<Title>/\n<Title>/g" \
+	    "$CAPABILITIES_FILE" | \
+	    grep "Name\|Title\|Style\|Layer" | \
+	    sed -e "s/<Layer .*>/LAYER:/" \
+	      -e "s/<\/Layer.*>//" \
+	      -e "s/<Name>\s*\([^<]*\)/~\1~/g" \
+	      -e "s/<\/Name>\n\?//g" \
+	      -e "s/<Style>\n*\s*\(\w*\)/Style: \1/" \
+	      -e "s/<\/Style>//" \
+	      -e "s/<Title>\(.*\)<\/Title>/\t --\1 /" \
+	      -e "s/<\/Title>//" \
+	      -e "s/<\/*.*\/*\/*>//" \
+	    > "${TMP}capabilities.txt"
+    fi
+
+    if [ `wc -l < "${TMP}capabilities.txt"` -eq 0 ] ; then
+	if [ $GIS_FLAG_G -eq 0 ] ; then
+	    g.message -w "Problem parsing XML file using POST-data method"
+	    GIS_FLAG_G=1
+	fi
+    fi
+}
+
+
+######################
+# name: dl_cap_post
+# purpose: attempt capabilities download using POST-data
+dl_cap_post() {
+    ### wget or curl the XML and grep the lines with layer names
+    g.message message="List of layers for server <$GIS_OPT_MAPSERVER>:"
+    g.message -v message=""
+    if [ "$USE_WGET" ] ; then
+	wget $GIS_OPT_WGETOPTIONS --post-data="${STRING}" "$GIS_OPT_MAPSERVER" -O "$CAPABILITIES_FILE";
+	message 1 "wget $GIS_OPT_WGETOPTIONS --post-data=\"${STRING}\" \"$GIS_OPT_MAPSERVER\" -O \"$CAPABILITIES_FILE\";"
+    else
+	 curl $GIS_OPT_CURLOPTIONS -o "$CAPABILITIES_FILE" -d "${STRING}" "$GIS_OPT_MAPSERVER"
+	 message 1 "curl $GIS_OPT_CURLOPTIONS -o \"$CAPABILITIES_FILE\" -d \"${STRING}\" \"$GIS_OPT_MAPSERVER\""
+    fi
+    g.message -v message=""
+
+    if [ ! -e "$CAPABILITIES_FILE" ] ; then
+	g.message -e "Downloading XML file"
+	\rm "$TMP"
+	exit 1
+    fi
+    if [ `wc -c < "$CAPABILITIES_FILE"` -eq 0 ] ; then
+	g.message -e "Downloaded XML file was empty"
+	\rm -f "$CAPABILITIES_FILE" "$TMP"
+	GIS_FLAG_G=1
+    fi
+}
+
+
+######################
+# name: dl_cap_get
+# purpose: attempt capabilities download using a single GET
+dl_cap_get() {
+    g.message -v message=""
+    if [ "$USE_WGET" ] ; then
+	wget $GIS_OPT_WGETOPTIONS "${GIS_OPT_MAPSERVER}${STRING}" -O "$CAPABILITIES_FILE";
+	message 1 "wget $GIS_OPT_WGETOPTIONS \"${GIS_OPT_MAPSERVER}${STRING}\" -O \"$CAPABILITIES_FILE\";"
+    else
+	curl $GIS_OPT_CURLOPTIONS -o "$CAPABILITIES_FILE" "${GIS_OPT_MAPSERVER}${STRING}"
+	message 1 "curl $GIS_OPT_CURLOPTIONS -o \"$CAPABILITIES_FILE\" \"${GIS_OPT_MAPSERVER}${STRING}\""
+    fi
+    g.message -v message=""
+
+    if [ ! -e "$CAPABILITIES_FILE" ] ; then
+	g.message -e "Downloading XML file"
+	\rm "$TMP"
+	exit 1
+    fi
+    if [ `wc -c < "$CAPABILITIES_FILE"` -eq 0 ] ; then
+	g.message -e "Downloaded XML file was empty"
+	\rm -f "$CAPABILITIES_FILE" "$TMP"
+	exit 1
+    fi
+}
+
+
+######################
 # name: listLayers
 # purpose: list layers available from this server
 
 listLayers() {
 
 	CAPABILITIES_FILE="${TMP}capabilities.xml"
-    
+
 	STRING="service=WMS&request=GetCapabilities&${GIS_OPT_WMSQUERY}";
+	g.message -d message="POST-data: [$STRING]"
 
-	### wget or curl the XML and grep the lines with layer names
-	g.message message="List of layers for server ${GIS_OPT_MAPSERVER}:"
-	if [ "$USE_WGET" ] ; then
-	    wget ${GIS_OPT_WGETOPTIONS} --post-data=${STRING} $GIS_OPT_MAPSERVER -O $CAPABILITIES_FILE;
-	    message 1 wget ${GIS_OPT_WGETOPTIONS} --post-data=${STRING} $GIS_OPT_MAPSERVER -O $CAPABILITIES_FILE;
-	else
-	    curl ${GIS_OPT_CURLOPTIONS} -o $CAPABILITIES_FILE -d ${STRING} $GIS_OPT_MAPSERVER
-	    message 1 curl ${GIS_OPT_CURLOPTIONS} -o $CAPABILITIES_FILE -d ${STRING} $GIS_OPT_MAPSERVER
+	if [ $GIS_FLAG_G -eq 0 ] ; then
+	    dl_cap_post
+	    parse_capabilities
+
+	    if [ $GIS_FLAG_G -eq 1 ] ; then
+		g.message -w "Problem downloading XML -- trying all-in-one GET method"
+	    fi
 	fi
 
-	# use xml2
-	if [ -x "`which xml2`" ] ; then
-		cat "$CAPABILITIES_FILE" | xml2 | \
-		  grep "Layer/SRS=\|Layer/Name=\|Layer/Title=\|Style/Name=\|Style/Title=\|Layer/Abstract=" | \
-		  sed -e "s/.*Layer\\/Name=/LAYER: /" \
-		      -e "s/.*Layer\\/SRS=/  SRS: /" \
-		      -e "s/.*Layer\\/Title=/  Title: /" \
-		      -e "s/.*Layer\\/Abstract=/  |/" \
-		      -e "s/.*Layer\\/Style\\/Name=/  STYLE: /" \
-		      -e "s/.*Layer\\/Style\\/Title=/    Style title: /" \
-		  > "${TMP}capabilities.txt"
-
-	else : # xml2 is not available
-		g.message -w "The 'xml2' program was not found. Proceeding with internal XML parsing which may not be as reliable."
-		grep "Name\|Title\|Style\|Layer" "$CAPABILITIES_FILE" | \
-		  sed -e "s/<Layer .*>/LAYER:/" \
-		      -e "s/<\/Layer.*>//" \
-		      -e "s/<Name>\s*\(\w*\)/~\1~/g" \
-		      -e "s/<\/Name>\n//g" \
-		      -e "s/<Style>\n*\s*\(\w*\)/Style: \1/" \
-		      -e "s/<\/Style>//" \
-		      -e "s/<Title>\(.*\)<\/Title>/\t --\1 /" \
-		      -e "s/<\/Title>//" \
-		      -e "s/<\/*.*\/*\/*>//" \
-		    > "${TMP}capabilities.txt"
+	# work-around for brain-dead WMSs which want POST-data as part of the GET URL
+	#   (that is allowed by OGC WMS def Sec 6.3.4)
+	if [ $GIS_FLAG_G -eq 1 ] ; then
+	    dl_cap_get
+	    parse_capabilities
 	fi
 
 	if [ `wc -l < "${TMP}capabilities.txt"` -eq 0 ] ; then
@@ -342,15 +448,19 @@
 	    g.message -e "Parsing XML file"
 	    g.message message="------------------------"
 	    cat "$CAPABILITIES_FILE"
+	    EXITVAL=1
 	else
+	    # we think it worked, output capabilities
 	    cat "${TMP}capabilities.txt"
+	    EXITVAL=0
 	fi
 	g.message message=""
 
 	if [ -e "$CAPABILITIES_FILE" ] ; then
 	    \rm -f "$CAPABILITIES_FILE" "${TMP}capabilities.txt" "$TMP"
         fi
-        exit 1;
+
+        exit $EXITVAL
 }
 
 
@@ -394,20 +504,25 @@
    fi
 fi
 
+# URL-proof spaces in layer names. (convert other chars to hex too?)
+#    HTTP reserved are: ";", "/", "?", ":", "@", "=", "#" and "&"
+LAYERS=`echo ${GIS_OPT_LAYERS} | sed -e 's/ /%20/g'`
+
+
 addflag REQUEST o ${GIS_FLAG_O}
 addflag REQUEST c ${GIS_FLAG_C}
 addflag REQUEST p ${GIS_FLAG_P}
 addargument REQUEST folder "${GIS_OPT_FOLDER}"
-addargument REQUEST prefix ${PREFIX}
-addargument REQUEST region ${GIS_OPT_REGION}
-addargument REQUEST mapserver ${GIS_OPT_MAPSERVER}
-addargument REQUEST layers ${GIS_OPT_LAYERS}
-addargument REQUEST styles ${GIS_OPT_STYLES}
-addargument REQUEST srs ${GIS_OPT_SRS}
-addargument REQUEST format ${GIS_OPT_FORMAT}
-addargument REQUEST wmsquery ${GIS_OPT_WMSQUERY}
-addargument REQUEST maxcols ${GIS_OPT_MAXCOLS}
-addargument REQUEST maxrows ${GIS_OPT_MAXROWS}
+addargument REQUEST prefix "${PREFIX}"
+addargument REQUEST region "${GIS_OPT_REGION}"
+addargument REQUEST mapserver "${GIS_OPT_MAPSERVER}"
+addargument REQUEST layers "${LAYERS}"
+addargument REQUEST styles "${GIS_OPT_STYLES}"
+addargument REQUEST srs "${GIS_OPT_SRS}"
+addargument REQUEST format "${GIS_OPT_FORMAT}"
+addargument REQUEST wmsquery "${GIS_OPT_WMSQUERY}"
+addargument REQUEST maxcols "${GIS_OPT_MAXCOLS}"
+addargument REQUEST maxrows "${GIS_OPT_MAXROWS}"
 addargument REQUEST tileoptions "${GIS_OPT_TILEOPTIONS}"
 
 if [ $GIS_FLAG_D -eq 0 ] ; then
@@ -419,8 +534,10 @@
 	fi
 fi
 
+addflag DOWNLOAD g ${GIS_FLAG_G}
 addargument DOWNLOAD requestfile "${REQUESTFILE}"
 addargument DOWNLOAD wgetoptions "${GIS_OPT_WGETOPTIONS}"
+addargument DOWNLOAD curloptions "${GIS_OPT_CURLOPTIONS}"
 
 message 1 "wms.download $DOWNLOAD"
 eval "wms.download $DOWNLOAD"
@@ -445,9 +562,9 @@
 addflag GDALWARP k ${GIS_FLAG_K}
 addflag GDALWARP p ${GIS_FLAG_P}
 addargument GDALWARP input "$FILES"
-addargument GDALWARP output ${GIS_OPT_OUTPUT}
-addargument GDALWARP method ${GIS_OPT_METHOD}
-addargument GDALWARP s_srs ${GIS_OPT_SRS}
+addargument GDALWARP output "${GIS_OPT_OUTPUT}"
+addargument GDALWARP method "${GIS_OPT_METHOD}"
+addargument GDALWARP s_srs "${GIS_OPT_SRS}"
 
 # check for error like 'Service Exception Report'
 if [ `file -b "$FILES" | grep -c "^HTML\|^XML"` -eq 1 ] ; then
@@ -461,6 +578,27 @@
 message 1 "r.in.gdalwarp $GDALWARP"
 eval "r.in.gdalwarp $GDALWARP"
 
+r.support map="$GIS_OPT_OUTPUT" title="WMS Download" history="" \
+  source1=`echo "$GIS_OPT_MAPSERVER" | cut -c-70` \
+  source2=`echo "$GIS_OPT_MAPSERVER" | cut -c71-140`
+  
+LAY_LEN="echo "$GIS_OPT_LAYERS" | wc -c"
+
+r.support map="$GIS_OPT_OUTPUT" history="layers: $GIS_OPT_LAYERS"
+if [ -n "$GIS_OPT_STYLES" ] ; then
+   r.support map="$GIS_OPT_OUTPUT" history="styles: $GIS_OPT_STYLES"
+fi
+r.support map="$GIS_OPT_OUTPUT" history=""
+r.support map="$GIS_OPT_OUTPUT" history="Source projection: $GIS_OPT_SRS"
+if [ "$GIS_FLAG_P" -eq 1 ] ; then
+   r.support map="$GIS_OPT_OUTPUT" history="  (imported without reprojection)"
+fi
+r.support map="$GIS_OPT_OUTPUT" description="generated by r.in.wms" history=""
+r.support map="$GIS_OPT_OUTPUT" history="$CMDLINE"
+
+g.message -v message="Map <${GIS_OPT_OUTPUT}> written"
+
 # Clean up:
 \rm -f "${TMP}"*
 
+exit

Modified: grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.download
===================================================================
--- grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.download	2008-03-19 10:51:18 UTC (rev 30635)
+++ grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.download	2008-03-19 10:58:26 UTC (rev 30636)
@@ -29,7 +29,7 @@
 #% key: wgetoptions
 #% type: string
 #% description: Additional options for wget
-#% answer: -c -t 5 --user-agent=MSIE5.5 
+#% answer: -c -t 5
 #% required : no
 #%end
 #%option
@@ -46,6 +46,10 @@
 #% description: Verbosity level
 #% answer: 1
 #%end
+#%Flag
+#% key: g
+#% description: Use alternative GET method (try if you have download problems)
+#%end
 
 if  [ -z "$GISBASE" ] ; then
     echo "You must be in GRASS GIS to run this program." 1>&2
@@ -108,22 +112,30 @@
 # Download the tiles!!
 GetTiles() {
   g.message "Downloading tiles"
-  
 
+  # init POST-data vs. GET URL method variable
+  if [ $GIS_FLAG_G -eq 0 ] ; then
+      POST_DATA_OK=1
+  else
+      POST_DATA_OK=0
+  fi
+
   CONTENTS=`cat "${REQUESTFILE}"`
 
   NUMBER_OF_TILES=0
   for line in $CONTENTS ; do
-	g.message -d message="$line"
+	g.message -d message="wget command: [$line]"
 	eval "$line"
+
 	emptyness=`file "$OUTPUT_FILE" | grep empty$`
+
 	if [ -f "$OUTPUT_FILE" ] && [ -z "$emptyness" ] ; then
-	  g.message "Tile already downloaded"
+	   g.message "Tile already downloaded"
 	else
-	  GetData
-	  if [ $? -ne 0 ] ; then
-            NUMBER_OF_TILES=`expr $NUMBER_OF_TILES + 1`
-          fi
+	   GetData
+	   if [ $? -ne 0 ] ; then
+              NUMBER_OF_TILES=`expr $NUMBER_OF_TILES + 1`
+           fi
 	fi
   done
 
@@ -140,24 +152,50 @@
 #Get the data from the WMS server
 GetData() {
    g.message "Downloading data"
-   g.message message="Requesting Data from ${SERVER}:"
-   g.message message="$STRING"
-   #download the File from the Server
-   if [ "$USE_WGET" ] ; then
-     wget ${WGET_OPTIONS} --post-data="${STRING}" "${SERVER}" -O "${OUTPUT_FILE}"
-   else
-	 curl ${CURL_OPTIONS} -o "${OUTPUT_FILE}" -d "${STRING}" "${SERVER}"
+   g.message -v message="Requesting Data from ${SERVER}:"
+   g.message -v message="$STRING"
+
+   if [ "$POST_DATA_OK" -eq 1 ] ; then
+      #download the File from the Server
+      if [ "$USE_WGET" ] ; then
+         wget ${WGET_OPTIONS} --post-data="${STRING}" "${SERVER}" -O "${OUTPUT_FILE}"
+      else
+         curl ${CURL_OPTIONS} -o "${OUTPUT_FILE}" -d "${STRING}" "${SERVER}"
+      fi
+      if [ $? -ne 0 ] ; then
+         g.message -e "Failed while downloading the data"
+         return 1
+      fi
+      if [ ! -f "$OUTPUT_FILE" ] ; then 
+         g.message -e "Not able to download the data"
+         return 1
+      fi
+
+       # work-around for brain-dead ArcIMS servers which want POST-data as part of the GET URL
+       #   (this is technically allowed by OGC WMS def v1.3.0 Sec6.3.4)
+      if [ `wc -c < "$OUTPUT_FILE"` -eq 0 ] ; then
+         g.message -w "Downloaded image file was empty -- trying another method"
+         POST_DATA_OK=0
+      fi
+   fi  # no else!
+
+   if [ "$POST_DATA_OK" -eq 0 ] ; then
+      g.message -v message=""
+      if [ "$USE_WGET" ] ; then
+	 wget ${WGET_OPTIONS} "${SERVER}${STRING}" -O "${OUTPUT_FILE}"
+      else
+	 curl ${CURL_OPTIONS} -o "${OUTPUT_FILE}" "${SERVER}${STRING}"
+      fi
+      if [ $? -ne 0 ] ; then
+         g.message -e "Failed while downloading the data"
+         return 1
+      fi
+      if [ ! -f "$OUTPUT_FILE" ] || [ `wc -c < "$OUTPUT_FILE"` -eq 0 ] ; then 
+         g.message -e "Not able to download the data"
+         return 1
+      fi
    fi
-   if [ $? -ne 0 ]; then
-     g.message -e "wget failed while downloading the data"
-     return 1
-   fi
-   if [ -f "${OUTPUT_FILE}" ]; then 
-     return 0
-   else
-     g.message -e "wget was not able to download the data"
-     return 1
-   fi
+
    return 0
 }
 

Modified: grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.request
===================================================================
--- grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.request	2008-03-19 10:51:18 UTC (rev 30635)
+++ grass/branches/releasebranch_6_3/scripts/r.in.wms/wms.request	2008-03-19 10:58:26 UTC (rev 30636)
@@ -208,28 +208,30 @@
 
   mkdir -p "${GIS_OPT_FOLDER}"
 
-  FOLDERPLUS=${GIS_OPT_FOLDER}/${GIS_OPT_PREFIX}_${GIS_OPT_REGION}
+  FOLDERPLUS="${GIS_OPT_FOLDER}/${GIS_OPT_PREFIX}_${GIS_OPT_REGION}"
 
   if [ $GIS_FLAG_C -eq 1 ] ; then
-    rm -f ${FOLDERPLUS}*
+    g.message -v message="Removing files <${FOLDERPLUS}*>"
+    rm -f "$FOLDERPLUS"*
   fi
 
   if [ -x "`which wget`" ] ; then
-    REQUESTFILE=${FOLDERPLUS}.wget
+    REQUESTFILE="${FOLDERPLUS}.wget"
   else
-    REQUESTFILE=${FOLDERPLUS}.curl
+    REQUESTFILE="${FOLDERPLUS}.curl"
   fi
 
   #reset the requestfile
-  echo -n "" > ${REQUESTFILE}
-  echo "$PROJ4_SRS" > ${FOLDERPLUS}.proj4
+  echo > "${REQUESTFILE}"
 
+  echo "$PROJ4_SRS" > "${FOLDERPLUS}.proj4"
+
   for i in $TILES ; do
 	eval "$i"
 	SIZE="bbox=$w,$s,$e,$n&width=$cols&height=$rows"
 	message 1 "$SIZE"
-	IMAGEFILE=${FOLDERPLUS}_${NUMBER_OF_TILES}
-	OUTPUT_FILE=${IMAGEFILE}${FILE_EXTENT}
+	IMAGEFILE="${FOLDERPLUS}_${NUMBER_OF_TILES}"
+	OUTPUT_FILE="${IMAGEFILE}${FILE_EXTENT}"
 	# We could add world files here to help out gdalwarp.
 	# And here it is:
 	# Displacement from top left cell to the one to the right of it and to the one below it:
@@ -256,8 +258,8 @@
 
 # Initialize variables:
 
-SERVER=${GIS_OPT_MAPSERVER}
-SRS=${GIS_OPT_SRS}
+SERVER="${GIS_OPT_MAPSERVER}"
+SRS="${GIS_OPT_SRS}"
 SRS_lower=`echo $SRS | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"`
 
 
@@ -272,10 +274,10 @@
 	SRS_SCALE=1
 fi
 
-WMS_QUERY=${GIS_OPT_WMSQUERY}
+WMS_QUERY="${GIS_OPT_WMSQUERY}"
 
 if [ -z "$GIS_OPT_REGION" ] ; then
-	TILESET_OPTIONS=$GIS_OPT_TILEOPTIONS
+	TILESET_OPTIONS="$GIS_OPT_TILEOPTIONS"
 else
 	TILESET_OPTIONS="region=$GIS_OPT_REGION $GIS_OPT_TILEOPTIONS"
 fi



More information about the grass-commit mailing list