[GRASS-SVN] r52055 - grass-addons/grass6/raster/r.in.onearth

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jun 12 20:15:14 PDT 2012


Author: hamish
Date: 2012-06-12 20:15:14 -0700 (Tue, 12 Jun 2012)
New Revision: 52055

Added:
   grass-addons/grass6/raster/r.in.onearth/r.in.onearth.twms
Log:
clone main script while experiementing with pre-tiled WMS support

Copied: grass-addons/grass6/raster/r.in.onearth/r.in.onearth.twms (from rev 52054, grass-addons/grass6/raster/r.in.onearth/r.in.onearth)
===================================================================
--- grass-addons/grass6/raster/r.in.onearth/r.in.onearth.twms	                        (rev 0)
+++ grass-addons/grass6/raster/r.in.onearth/r.in.onearth.twms	2012-06-13 03:15:14 UTC (rev 52055)
@@ -0,0 +1,399 @@
+#!/bin/sh
+#############################################################################
+# Download and import satellite images direct from the                      #
+# NASA onearth WMS server into GRASS.                                       #
+# written by Soeren Gebbert 11/2005 soerengebbert AT gmx de                 #
+# and Markus Neteler                                                        #
+#                                                                           #
+# COPYRIGHT:	(C) 2005 by the GRASS Development Team                      #
+#                                                                           #
+#		This program is free software under the GNU General Public  #
+#		License (>=v2). Read the file COPYING that comes with GRASS #
+#		for details.                                                #
+#                                                                           #
+#############################################################################
+
+#%Module
+#% description: Download and import satellite images direct from the NASA onearth WMS server into GRASS or to a geo-tiff image file.
+#%End
+#%option
+#% key: output
+#% gisprompt: new,cell,raster
+#% type: string
+#% description: Output raster map name prefix
+#% required : no
+#%end
+#%option
+#% key: file
+#% gisprompt: file,file,file
+#% type: string
+#% description: Output file name prefix
+#% answer: /tmp/test
+#% required : no
+#%end
+#%flag
+#%  key: f
+#%  description: Do not import to GRASS, create a tiff file instead.
+#%end
+#%flag
+#%  key: l
+#%  description: Download and Import WMS Global Mosaic, a High Resolution, Global Coverage, Landsat TM 7 mosaic.
+#%end
+#%flag
+#%  key: s
+#%  description: Download and Import the radar reflectance images produced by the SRTM mission.
+#%end
+#%flag
+#%  key: b
+#%  description: Download and Import the Blue Marble Next Generation layer, one for each month of the year. 
+#%end
+#%flag
+#%  key: t
+#%  description: Download and Import the composite of data produced by the MODIS Rapid Response System, from data collected yesterday by the MODIS/Terra.
+#%end
+#%flag
+#%  key: a
+#%  description: Download and Import the composite of data produced by the MODIS Rapid Response System, from data collected yesterday by the MODIS/Aqua.
+#%end
+#%option
+#% key: tmband
+#% type: string
+#% description: NASA Landsat TM bands
+#% options:  Red,Green,Blue,IR1,IR2,IR3,ThL,ThH,Pan,visual,pseudo 
+#% required : no
+#%end
+#%option
+#% key: srtmband
+#% type: string
+#% description: Radar reflectance bands
+#% options:  default,ss1,ss2,ss3,ss4,all 
+#% required : no
+#%end
+#%option
+#% key: month
+#% type: string
+#% description: Blue Marble Next Generation layer
+#% options:  Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec 
+#% required : no
+#%end
+#%option
+#% key: time
+#% type: string
+#% description: The datum of creation for Aqua or Terra satellite images
+#% answer: 2005-3-24    
+#% required : no
+#%end
+#%option
+#% key: wgetopt
+#% type: string
+#% description: Options for wget
+#% answer: -c -t 5 --user-agent=MSIE5.5   
+#% required : no
+#%end
+
+#Only run if started in GRASS
+if test "$GISBASE" = ""; then
+ echo "You must be in GRASS GIS to run this program." >&2
+ exit 1
+fi
+
+#Set up important vars first
+SRC="EPSG:4326" #This is the Projection LatLong wgs84
+FORMAT="image/geotiff" #GeoTiff import for r.in.gdal
+NASASERVER="http://wms.jpl.nasa.gov/wms.cgi" #this server may change
+TYPE=""
+TIME=""
+IMPORT=0 #0 is true
+GLOBAL_MOSAIC_LAYER="global_mosaic_base" #Thats the NASA WMS Global Mosaic database
+SRTM_MAG_LAYER="srtm_mag" #Thats the NASA srtm_mag database
+BMNG_LAYER="BMNG" #Thats the Blue Marble Next Generation database
+DAILY_TERRA_LAYER="daily_terra" #Thats the NASA daily terra database
+DAILY_AQUA_LAYER="daily_aqua" #Thats the NASA daily aqua database
+USEGDALWARP=0 #if we dont have gdalwarp, only LatLong projection is supported, 0 is true
+FILE_EXTENT=".tif"
+
+# check if we have wget
+if [ ! -x "`which wget`" ] ; then
+    echo "wget required, please install first" 2>&1
+    exit 1
+fi
+
+# check if we have gdalwarp
+if [ ! -x "`which gdalwarp`" ] ; then
+    echo "gdalwarp is recommended, please install first (script still works in LatLong locations)" 2>&1
+    USEGDALWARP=1 #use only LatLong
+fi
+
+#Some functions
+#Get the data from the NASA server
+GetData() {
+   IMPORT=0 #default
+   local STRING="request=GetMap&layers=${LAYER}&srs=${SRC}&width=${WIDTH}&height=${HEIGHT}&bbox=${w},${s},${e},${n}&format=${FORMAT}&version=1.1.0&styles=${STYLE}${TIME}"
+   #echo $STRING
+   #Create thefilename
+   IMAGEFILE="${TMPDIR}/Image_${LAYER}_${STYLE}_${HEIGHT}_${WIDTH}"
+   echo " "
+   echo " "
+   echo "************** DOWNLOAD DATA ****************"
+   echo "Requesting Data from ${NASASERVER}"
+   #download the File from the Server
+   wget ${WGET_OPTIONS}  --post-data=${STRING} ${NASASERVER} -O ${IMAGEFILE} 
+   if [ $? -ne 0 ]; then
+     echo " "
+     echo "!--------------ERROR-------------------!"
+     echo "wget was not able to download the data"
+     echo " "
+     IMPORT=1
+     return 1
+   fi
+   if [ -f "${IMAGEFILE}" ]; then 
+     IMPORT=0
+   else
+     echo " "
+     echo "!--------------ERROR-------------------!"
+     echo "wget was not able to download the data"
+     echo " "
+     IMPORT=1
+     return 1
+   fi
+return 0
+}
+
+#warp the data to the current grass locationa via gdalwarp
+WarpData() {
+   if [ ${USEGDALWARP} -eq 0 ]; then 
+        echo "************* CONVERT DATA  ***************"
+        #create the new imagename
+	IMAGEFILE_GDALWARP="${TMPDIR}/Image_${LAYER}_${STYLE}_${HEIGHT}_${WIDTH}_gdalwarp"
+   
+   	#convert the data to the current location, create Erdas Imagine Images (HFA)
+	gdalwarp -s_srs $SRC -t_srs "`g.proj -wf`" -of HFA ${IMAGEFILE} ${IMAGEFILE_GDALWARP}
+	if [ $? -ne 0 ]; then
+          echo "!-------- CAN NOT CONVERT DATA --------!"
+          echo "!------------ WILL BREAK --------------!"
+	  exitprocedure
+	fi
+        echo "************ DATA CONVERTED *************"
+	#remove the old image and convert the name
+	rm -f ${IMAGEFILE}
+	IMAGEFILE=${IMAGEFILE_GDALWARP}
+	return 0
+   fi
+return 1
+}
+
+#Import the Data with r.in.gdal
+ImportData() {
+   if [ ${IMPORT} -eq 0 ]; then 
+    #Check if Tiff file
+    FILETYPE=`file  ${IMAGEFILE} | cut --fields=2 --delimiter=:`
+    echo ${FILETYPE} | grep TIFF > /dev/null
+    if [ $? -ne 0 ]; then
+    	echo "Downloaded file is not a GeoTiff file, but will try to import"
+    fi
+     echo "************** CHECK DATA *****************"
+     gdalinfo ${IMAGEFILE} | grep "GDALOpen failed" > /dev/null
+     local ReturnValueGdalBug=$?
+     
+     gdalinfo ${IMAGEFILE} 
+     local ReturnValueGdal=$?
+
+     if [ ${ReturnValueGdal} -eq 0 ] && [ ${ReturnValueGdalBug} -ne 0 ]; then
+       echo "************* DATA CHECK OK ***************"
+       #Copy or import
+       if [ ${GIS_FLAG_f} -eq 1 ] ; then
+         #Copy the data to the outputfile
+	 echo "Creating output file ${GIS_OPT_file}${TYPE}${STYLE}${FILE_EXTENT}"
+         cp ${IMAGEFILE} ${GIS_OPT_file}${TYPE}${STYLE}${FILE_EXTENT}
+       else
+         #Warp the data!
+         WarpData
+         echo "************** IMPORT DATA ****************"
+         r.in.gdal -o input=${IMAGEFILE} output="${GIS_OPT_output}${TYPE}_${STYLE}"
+       fi
+     else
+        echo "!-------------------BREAK---------------------!"
+     	echo "Downloaded file is not supported by gdal, or cannot be imported"
+	if [ ${ReturnValueGdalBug} -eq 0 ]; then
+     	  echo "There was a problem while downloading the file, maybe you should try it again."
+	fi
+	echo "File of Type: ${FILETYPE}"
+	#If the File is XML, then cat the contents to stdout
+	echo ${FILETYPE} | grep XML > /dev/null
+        if [ $? -eq 0 ]; then
+	  echo " "
+	  echo "Message from Server ${NASASERVER}"
+	  echo " "
+          echo "!------------BEGIN-ERROR-MESSGAE--------------!"
+	  cat ${IMAGEFILE}
+          echo "!-------------END-ERROR-MESSGAE---------------!"
+	  echo " "
+	fi
+     fi
+     rm -rf ${IMAGEFILE}
+   fi
+
+   return 0
+}
+
+# what to do in case of user break:
+exitprocedure()
+{
+ echo "User break!"
+ rm -rf "$TMPDIR"
+ exit 1
+}
+trap "exitprocedure" 2 3 15
+
+#Parse the arguments
+if [ "$1" != "@ARGS_PARSED@" ] ; then
+  exec g.parser "$0" "$@"
+fi
+
+#At least one flag should be set
+if [ $GIS_FLAG_l -eq 0 -a $GIS_FLAG_s -eq 0 -a $GIS_FLAG_b -eq 0 -a $GIS_FLAG_t -eq 0 -a $GIS_FLAG_a -eq 0 ] ; then
+    echo "ERROR: Select a flag to specify map type"
+    exit 1
+fi
+
+#Check if a file or a map should be created
+if [ ${GIS_FLAG_f} -eq 1 ] ; then
+  if [ ${GIS_OPT_file} == ""] ; then
+  	echo "Please specify the output filename"
+	exit 1
+  fi
+fi
+
+#Some mapset informations 
+eval `g.gisenv`
+: ${GISBASE?} ${GISDBASE?} ${LOCATION_NAME?} ${MAPSET?}
+LOCATION="$GISDBASE/$LOCATION_NAME/$MAPSET"
+PERM="$GISDBASE/$LOCATION_NAME/PERMANENT"
+
+#wget has many options
+WGET_OPTIONS=${GIS_OPT_wgetopt}
+
+#Get the region data 
+eval `g.region -g`
+WIDTH=${cols}
+HEIGHT=${rows}
+eval `g.region -gb`
+#Now get the LatLong Boundingbox
+grep -i 'proj: ll' $PERM/PROJ_INFO > /dev/null
+if [ $? -ne 0 ] && [ ${USEGDALWARP} -eq 0 ]; then
+  n=$ll_n
+  s=$ll_s
+  e=$ll_e
+  w=$ll_w
+  echo "LatLong wgs84 bounding box = N $n S $s W $w E $e"
+else
+  #We have LatLong projection, no warp is needed!
+  USEGDALWARP=1 
+  #There is a bug in nasa WMS service, it provides images which are lager then
+  #the world :(, we have to crop the images
+  if [ "$n" == "90" -a "$s" == "-90" -a "$w" == "-180" -a "$e" == "180" ] ; then
+
+  # check if we have bc
+  if [ ! -x "`which bc`" ] ; then
+    echo "bc required, please install first" 2>&1
+    exit 1
+  fi
+   #We request a smaller image from the wms server
+   n=`echo "$n - 0.001" | bc`
+   s=`echo "$s + 0.001" | bc`
+   e=`echo "$e - 0.001" | bc`
+   w=`echo "$w + 0.001" | bc`
+  fi
+fi
+
+#Break If we have no warp and no LatLong
+grep -i 'proj: ll' $PERM/PROJ_INFO > /dev/null
+if [ $? -ne 0 ] && [ ${USEGDALWARP} -eq 1 ] ; then
+  echo "NASA onearth data are in Latitude/Longitude. The current"
+  echo "location projection differs and you dont have gdalwarp! STOP."
+  exit 1
+fi
+
+
+#make a temporary directory
+TMPDIR="`g.tempfile pid=$$`"
+if [ $? -ne 0 ] || [ -z "$TMPDIR" ] ; then
+    echo "ERROR: unable to create temporary files" 1>&2
+    exit 1
+fi
+rm -f "$TMPDIR"
+mkdir "$TMPDIR"
+
+#Get the Data and import them
+#import every choice that can be made
+
+if [ $GIS_FLAG_l -eq 1 ] ; then
+LAYER=${GLOBAL_MOSAIC_LAYER}
+STYLE=${GIS_OPT_tmband}
+TYPE="LandsatTM"
+echo " "
+echo "======================================================="
+echo "Will download and import ${TYPE} Data with band ${STYLE}"
+echo "======================================================="
+GetData
+ImportData
+fi
+
+if [ $GIS_FLAG_s -eq 1 ] ; then
+LAYER=${SRTM_MAG_LAYER}
+STYLE=${GIS_OPT_srtmband}
+TYPE="SRTM"
+echo " "
+echo "======================================================="
+echo "Will download and import ${TYPE} Data with band ${STYLE}"
+echo "======================================================="
+GetData
+ImportData
+fi
+
+if [ $GIS_FLAG_b -eq 1 ] ; then
+LAYER=${BMNG_LAYER}
+STYLE=${GIS_OPT_month}
+TYPE="BMNG"
+echo " "
+echo "======================================================="
+echo "Will download and import ${TYPE} Data of month ${STYLE}"
+echo "======================================================="
+GetData
+ImportData
+fi
+
+if [ $GIS_FLAG_t -eq 1 ] ; then
+LAYER=${DAILY_TERRA_LAYER}
+TIME="&time=${GIS_OPT_time}"
+STYLE=""
+TYPE="Daily_Terra"
+echo " "
+echo "======================================================="
+echo "Will download and import ${TYPE} Data"
+echo "======================================================="
+GetData
+ImportData
+fi
+
+if [ $GIS_FLAG_a -eq 1 ] ; then
+LAYER=${DAILY_AQUA_LAYER}
+TIME="&time=${GIS_OPT_time}"
+STYLE=""
+TYPE="Daily_Aqua"
+echo " "
+echo "======================================================="
+echo "Will download and import ${TYPE} Data"
+echo "======================================================="
+GetData
+ImportData
+fi
+
+#remove the temp dir
+rm -rf "$TMPDIR"
+echo " "
+echo "========"
+echo "Finished"
+echo "========"
+exit 0
+



More information about the grass-commit mailing list