[GRASS-SVN] r30460 - grass/trunk/scripts/r.in.wms

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Mar 4 07:25:15 EST 2008


Author: hamish
Date: 2008-03-04 07:25:15 -0500 (Tue, 04 Mar 2008)
New Revision: 30460

Modified:
   grass/trunk/scripts/r.in.wms/r.in.gdalwarp
   grass/trunk/scripts/r.in.wms/r.in.wms
   grass/trunk/scripts/r.in.wms/wms.download
Log:
fixes: 
* r.in.wms: protect spaces in layer names
* r.in.gdalwarp: patch together single band maps correctly
* wms.download: work with brain-dead ArcIMS servers which don't support POST-data
* add some debug messages
* quieten chatter
* r.in.wms: write out metadata


Modified: grass/trunk/scripts/r.in.wms/r.in.gdalwarp
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.gdalwarp	2008-03-04 09:59:09 UTC (rev 30459)
+++ grass/trunk/scripts/r.in.wms/r.in.gdalwarp	2008-03-04 12:25:15 UTC (rev 30460)
@@ -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,23 @@
 		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}"
+	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 +226,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 +245,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 +289,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 +302,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"
@@ -292,17 +326,30 @@
 	done
 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"
+	    g.message -d message="patch list = [$MAPLIST]"
+	    r.patch input="$MAPLIST" output="${GIS_OPT_OUTPUT}"
+	    if [ $? -eq 0 ] ; then
+		g.remove rast="$MAPLIST" --quiet
+	    fi
+	fi
 fi
 
 # Set up color counter

Modified: grass/trunk/scripts/r.in.wms/r.in.wms
===================================================================
--- grass/trunk/scripts/r.in.wms/r.in.wms	2008-03-04 09:59:09 UTC (rev 30459)
+++ grass/trunk/scripts/r.in.wms/r.in.wms	2008-03-04 12:25:15 UTC (rev 30460)
@@ -174,8 +174,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
@@ -313,7 +319,7 @@
 	CAPABILITIES_FILE="${TMP}capabilities.xml"
     
 	STRING="service=WMS&request=GetCapabilities&${GIS_OPT_WMSQUERY}";
-	g.message -d message="POST: [$STRING]"
+	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>:"
@@ -333,7 +339,7 @@
 	    exit 1
 	fi
 
-	# work-around for broken WMS which wants post-data in the URL line
+	# work-around for brain-dead WMSs which want POST-data as part of the GET URL (allowed by OGC WMS def S6.3.4)
 	if [ `wc -c < "$CAPABILITIES_FILE"` -eq 0 ] ; then
 	    g.message -w "Downloaded XML file was empty -- trying another method"
 	    g.message -v message=""
@@ -443,6 +449,11 @@
    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}
@@ -450,7 +461,7 @@
 addargument REQUEST prefix "${PREFIX}"
 addargument REQUEST region "${GIS_OPT_REGION}"
 addargument REQUEST mapserver "${GIS_OPT_MAPSERVER}"
-addargument REQUEST layers "${GIS_OPT_LAYERS}"
+addargument REQUEST layers "${LAYERS}"
 addargument REQUEST styles "${GIS_OPT_STYLES}"
 addargument REQUEST srs "${GIS_OPT_SRS}"
 addargument REQUEST format "${GIS_OPT_FORMAT}"
@@ -511,6 +522,26 @@
 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"
+
+
 # Clean up:
 \rm -f "${TMP}"*
 
+exit

Modified: grass/trunk/scripts/r.in.wms/wms.download
===================================================================
--- grass/trunk/scripts/r.in.wms/wms.download	2008-03-04 09:59:09 UTC (rev 30459)
+++ grass/trunk/scripts/r.in.wms/wms.download	2008-03-04 12:25:15 UTC (rev 30460)
@@ -108,22 +108,26 @@
 # Download the tiles!!
 GetTiles() {
   g.message "Downloading tiles"
-  
 
+  # init POST-data vs. GET URL method variable
+  POST_DATA_OK=1
+
   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 +144,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
 }
 



More information about the grass-commit mailing list