Changeset 52056


Ignore:
Timestamp:
Jun 12, 2012 11:20:37 PM (4 years ago)
Author:
hamish
Message:

adjust to modern grass coding style

File:
1 edited

Legend:

Unmodified
Added
Removed
  • grass-addons/grass6/raster/r.in.onearth/r.in.onearth.twms

    r52055 r52056  
    44# NASA onearth WMS server into GRASS.                                       #
    55# written by Soeren Gebbert 11/2005 soerengebbert AT gmx de                 #
    6 # and Markus Neteler                                                        #
     6# and Markus Neteler. Support for pre-tiled WMS server by Hamish Bowman     #
    77#                                                                           #
    8 # COPYRIGHT:    (C) 2005 by the GRASS Development Team                      #
     8# COPYRIGHT:    (C) 2005-2012 by the GRASS Development Team                 #
    99#                                                                           #
    1010#               This program is free software under the GNU General Public  #
     
    1515
    1616#%Module
    17 #% description: Download and import satellite images direct from the NASA onearth WMS server into GRASS or to a geo-tiff image file.
     17#%  description: Download and import satellite images direct from the NASA onearth WMS server into GRASS or to a geo-tiff image file.
    1818#%End
    1919#%option
    20 #% key: output
    21 #% gisprompt: new,cell,raster
    22 #% type: string
    23 #% description: Output raster map name prefix
    24 #% required : no
    25 #%end
    26 #%option
    27 #% key: file
    28 #% gisprompt: file,file,file
    29 #% type: string
    30 #% description: Output file name prefix
    31 #% answer: /tmp/test
    32 #% required : no
     20#%  key: output
     21#%  gisprompt: new,cell,raster
     22#%  type: string
     23#%  description: Output raster map name prefix
     24#%  required: no
     25#%end
     26#%option
     27#%  key: file
     28#%  gisprompt: file,file,file
     29#%  type: string
     30#%  description: Output file name prefix
     31#%  answer: /tmp/test
     32#%  required: no
    3333#%end
    3434#%flag
     
    5757#%end
    5858#%option
    59 #% key: tmband
    60 #% type: string
    61 #% description: NASA Landsat TM bands
    62 #% options:  Red,Green,Blue,IR1,IR2,IR3,ThL,ThH,Pan,visual,pseudo
    63 #% required : no
    64 #%end
    65 #%option
    66 #% key: srtmband
    67 #% type: string
    68 #% description: Radar reflectance bands
    69 #% options:  default,ss1,ss2,ss3,ss4,all
    70 #% required : no
    71 #%end
    72 #%option
    73 #% key: month
    74 #% type: string
    75 #% description: Blue Marble Next Generation layer
    76 #% options:  Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
    77 #% required : no
    78 #%end
    79 #%option
    80 #% key: time
    81 #% type: string
    82 #% description: The datum of creation for Aqua or Terra satellite images
    83 #% answer: 2005-3-24   
    84 #% required : no
    85 #%end
    86 #%option
    87 #% key: wgetopt
    88 #% type: string
    89 #% description: Options for wget
    90 #% answer: -c -t 5 --user-agent=MSIE5.5   
    91 #% required : no
    92 #%end
     59#%  key: tmband
     60#%  type: string
     61#%  description: NASA Landsat TM bands
     62#%  options: Red,Green,Blue,IR1,IR2,IR3,ThL,ThH,Pan,visual,pseudo
     63#%  required: no
     64#%end
     65#%option
     66#%  key: srtmband
     67#%  type: string
     68#%  description: Radar reflectance bands
     69#%  options:  default,ss1,ss2,ss3,ss4,all
     70#%  required: no
     71#%end
     72#%option
     73#%  key: month
     74#%  type: string
     75#%  description: Blue Marble Next Generation layer
     76#%  options: Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
     77#%  required: no
     78#%end
     79#%option
     80#%  key: time
     81#%  type: string
     82#%  description: The datum of creation for Aqua or Terra satellite images
     83#%  answer: 2005-3-24   
     84#%  required: no
     85#%end
     86#%option
     87#%  key: wgetopt
     88#%  type: string
     89#%  description: Options for wget
     90#%  answer: -c -t 5
     91#%  required: no
     92#%end
     93
    9394
    9495#Only run if started in GRASS
    95 if test "$GISBASE" = ""; then
    96  echo "You must be in GRASS GIS to run this program." >&2
    97  exit 1
    98 fi
     96if [ -z "$GISBASE" ] ; then
     97    echo "You must be in GRASS GIS to run this program." >&2
     98    exit 1
     99fi
     100
     101#Parse the arguments
     102if [ "$1" != "@ARGS_PARSED@" ] ; then
     103  exec g.parser "$0" "$@"
     104fi
     105
    99106
    100107#Set up important vars first
     
    113120FILE_EXTENT=".tif"
    114121
     122
    115123# check if we have wget
    116124if [ ! -x "`which wget`" ] ; then
    117     echo "wget required, please install first" 2>&1
     125    g.message -e "wget required, please install first"
    118126    exit 1
    119127fi
     
    121129# check if we have gdalwarp
    122130if [ ! -x "`which gdalwarp`" ] ; then
    123     echo "gdalwarp is recommended, please install first (script still works in LatLong locations)" 2>&1
     131    g.message -w "gdalwarp is recommended, please install first (script still works in LatLong locations)"
    124132    USEGDALWARP=1 #use only LatLong
    125133fi
     
    127135#Some functions
    128136#Get the data from the NASA server
    129 GetData() {
     137GetData()
     138{
    130139   IMPORT=0 #default
    131    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}"
     140   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"
    132141   #echo $STRING
    133142   #Create thefilename
    134    IMAGEFILE="${TMPDIR}/Image_${LAYER}_${STYLE}_${HEIGHT}_${WIDTH}"
    135    echo " "
    136    echo " "
    137    echo "************** DOWNLOAD DATA ****************"
    138    echo "Requesting Data from ${NASASERVER}"
     143   IMAGEFILE="$TMPDIR/Image_${LAYER}_${STYLE}_${HEIGHT}_$WIDTH"
     144   g.message -v "**** DOWNLOAD DATA ****"
     145   g.message -v "Requesting Data from $NASASERVER"
     146
    139147   #download the File from the Server
    140    wget ${WGET_OPTIONS}  --post-data=${STRING} ${NASASERVER} -O ${IMAGEFILE}
    141    if [ $? -ne 0 ]; then
    142      echo " "
    143      echo "!--------------ERROR-------------------!"
    144      echo "wget was not able to download the data"
    145      echo " "
     148   wget $WGET_OPTIONS --post-data="$STRING" "$NASASERVER" -O "$IMAGEFILE"
     149
     150   if [ $? -ne 0 ] ; then
     151     g.message -e "wget was not able to download the data"
    146152     IMPORT=1
    147153     return 1
    148154   fi
    149    if [ -f "${IMAGEFILE}" ]; then
     155
     156   if [ -f "$IMAGEFILE" ] ; then
    150157     IMPORT=0
    151158   else
    152      echo " "
    153      echo "!--------------ERROR-------------------!"
    154      echo "wget was not able to download the data"
    155      echo " "
     159     g.message -e "wget was not able to download the data"
    156160     IMPORT=1
    157161     return 1
     
    160164}
    161165
     166
    162167#warp the data to the current grass locationa via gdalwarp
    163 WarpData() {
    164    if [ ${USEGDALWARP} -eq 0 ]; then
    165         echo "************* CONVERT DATA  ***************"
     168WarpData()
     169{
     170   if [ "$USEGDALWARP" -eq 0 ] ; then
     171        g.message -v "**** CONVERT DATA  ****"
    166172        #create the new imagename
    167         IMAGEFILE_GDALWARP="${TMPDIR}/Image_${LAYER}_${STYLE}_${HEIGHT}_${WIDTH}_gdalwarp"
    168    
     173        IMAGEFILE_GDALWARP="$TMPDIR/Image_${LAYER}_${STYLE}_${HEIGHT}_${WIDTH}_gdalwarp"
     174
    169175        #convert the data to the current location, create Erdas Imagine Images (HFA)
    170         gdalwarp -s_srs $SRC -t_srs "`g.proj -wf`" -of HFA ${IMAGEFILE} ${IMAGEFILE_GDALWARP}
    171         if [ $? -ne 0 ]; then
    172           echo "!-------- CAN NOT CONVERT DATA --------!"
    173           echo "!------------ WILL BREAK --------------!"
     176        gdalwarp -s_srs "$SRC" -t_srs "`g.proj -wf`" -of HFA \
     177            "$IMAGEFILE" "$IMAGEFILE_GDALWARP"
     178        if [ $? -ne 0 ] ; then
     179          g.message -i '!-------- CAN NOT CONVERT DATA --------!'
     180          g.message -i '!------------ WILL BREAK --------------!'
    174181          exitprocedure
    175182        fi
    176         echo "************ DATA CONVERTED *************"
     183        g.message -v "**** DATA CONVERTED ****"
    177184        #remove the old image and convert the name
    178         rm -f ${IMAGEFILE}
    179         IMAGEFILE=${IMAGEFILE_GDALWARP}
     185        rm -f "$IMAGEFILE"
     186        IMAGEFILE="$IMAGEFILE_GDALWARP"
    180187        return 0
    181188   fi
     
    183190}
    184191
     192
    185193#Import the Data with r.in.gdal
    186 ImportData() {
    187    if [ ${IMPORT} -eq 0 ]; then
     194ImportData()
     195{
     196   if [ "$IMPORT" -eq 0 ] ; then
    188197    #Check if Tiff file
    189     FILETYPE=`file  ${IMAGEFILE} | cut --fields=2 --delimiter=:`
    190     echo ${FILETYPE} | grep TIFF > /dev/null
    191     if [ $? -ne 0 ]; then
    192         echo "Downloaded file is not a GeoTiff file, but will try to import"
     198    FILETYPE=`file  "$IMAGEFILE" | cut --fields=2 --delimiter=:`
     199    echo "$FILETYPE" | grep TIFF > /dev/null
     200    if [ $? -ne 0 ] ; then
     201        g.message -i "Downloaded file is not a GeoTiff file, but will try to import"
    193202    fi
    194      echo "************** CHECK DATA *****************"
    195      gdalinfo ${IMAGEFILE} | grep "GDALOpen failed" > /dev/null
     203     g.message -v "**** CHECK DATA ****"
     204     gdalinfo "$IMAGEFILE" | grep "GDALOpen failed" > /dev/null
    196205     local ReturnValueGdalBug=$?
    197206     
    198      gdalinfo ${IMAGEFILE}
     207     gdalinfo "$IMAGEFILE"
    199208     local ReturnValueGdal=$?
    200209
    201      if [ ${ReturnValueGdal} -eq 0 ] && [ ${ReturnValueGdalBug} -ne 0 ]; then
    202        echo "************* DATA CHECK OK ***************"
     210     if [ "$ReturnValueGdal" -eq 0 ] && [ "$ReturnValueGdalBug" -ne 0 ] ; then
     211       g.message -v "**** DATA CHECK OK ****"
    203212       #Copy or import
    204        if [ ${GIS_FLAG_f} -eq 1 ] ; then
     213       if [ "$GIS_FLAG_F" -eq 1 ] ; then
    205214         #Copy the data to the outputfile
    206          echo "Creating output file ${GIS_OPT_file}${TYPE}${STYLE}${FILE_EXTENT}"
    207          cp ${IMAGEFILE} ${GIS_OPT_file}${TYPE}${STYLE}${FILE_EXTENT}
     215         g.message -v "Creating output file $GIS_OPT_FILE$TYPE$STYLE$FILE_EXTENT"
     216         cp "$IMAGEFILE" "$GIS_OPT_FILE$TYPE$STYLE$FILE_EXTENT"
    208217       else
    209218         #Warp the data!
    210219         WarpData
    211          echo "************** IMPORT DATA ****************"
    212          r.in.gdal -o input=${IMAGEFILE} output="${GIS_OPT_output}${TYPE}_${STYLE}"
     220         g.message -v "**** IMPORT DATA ****"
     221         r.in.gdal -o input="$IMAGEFILE" \
     222             output="$GIS_OPT_OUTPUT${TYPE}_$STYLE"
    213223       fi
    214224     else
    215         echo "!-------------------BREAK---------------------!"
     225        echo '!-------------------BREAK---------------------!'
    216226        echo "Downloaded file is not supported by gdal, or cannot be imported"
    217         if [ ${ReturnValueGdalBug} -eq 0 ]; then
    218           echo "There was a problem while downloading the file, maybe you should try it again."
     227        if [ $ReturnValueGdalBug -eq 0 ] ; then
     228            echo "There was a problem while downloading the file, maybe you should try it again."
    219229        fi
    220         echo "File of Type: ${FILETYPE}"
     230        g.message -v "File of Type: $FILETYPE"
    221231        #If the File is XML, then cat the contents to stdout
    222         echo ${FILETYPE} | grep XML > /dev/null
    223         if [ $? -eq 0 ]; then
     232        echo "$FILETYPE" | grep XML > /dev/null
     233        if [ $? -eq 0 ] ; then
     234          g.message " "
     235          g.message "Message from Server $NASASERVER"
    224236          echo " "
    225           echo "Message from Server ${NASASERVER}"
    226           echo " "
    227           echo "!------------BEGIN-ERROR-MESSGAE--------------!"
    228           cat ${IMAGEFILE}
    229           echo "!-------------END-ERROR-MESSGAE---------------!"
    230           echo " "
     237          echo '!------------BEGIN-ERROR-MESSGAE--------------!'
     238          cat "$IMAGEFILE"
     239          echo '!-------------END-ERROR-MESSGAE---------------!'
    231240        fi
    232241     fi
    233      rm -rf ${IMAGEFILE}
     242     rm -rf "$IMAGEFILE"
    234243   fi
    235244
    236245   return 0
    237246}
     247
    238248
    239249# what to do in case of user break:
    240250exitprocedure()
    241251{
    242  echo "User break!"
    243  rm -rf "$TMPDIR"
    244  exit 1
     252    g.message 'User break!'
     253    rm -rf "$TMPDIR"
     254    exit 1
    245255}
    246256trap "exitprocedure" 2 3 15
    247257
    248 #Parse the arguments
    249 if [ "$1" != "@ARGS_PARSED@" ] ; then
    250   exec g.parser "$0" "$@"
    251 fi
    252258
    253259#At least one flag should be set
    254 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
    255     echo "ERROR: Select a flag to specify map type"
     260if [ $GIS_FLAG_L -eq 0  -a  $GIS_FLAG_S -eq 0  -a  $GIS_FLAG_B -eq 0 ] \
     261   && [ $GIS_FLAG_T -eq 0  -a  $GIS_FLAG_A -eq 0 ] ; then
     262    g.message -e "Select a flag to specify map type"
    256263    exit 1
    257264fi
    258265
    259266#Check if a file or a map should be created
    260 if [ ${GIS_FLAG_f} -eq 1 ] ; then
    261   if [ ${GIS_OPT_file} == ""] ; then
    262         echo "Please specify the output filename"
     267if [ "$GIS_FLAG_F" -eq 1 ] ; then
     268    if [ -z "$GIS_OPT_FILE"] ; then
     269        g.message -e "Please specify the output filename"
    263270        exit 1
    264   fi
     271    fi
    265272fi
    266273
     
    272279
    273280#wget has many options
    274 WGET_OPTIONS=${GIS_OPT_wgetopt}
     281WGET_OPTIONS="$GIS_OPT_WGETOPT"
    275282
    276283#Get the region data
    277284eval `g.region -g`
    278 WIDTH=${cols}
    279 HEIGHT=${rows}
     285WIDTH="$cols"
     286HEIGHT="$rows"
    280287eval `g.region -gb`
    281288#Now get the LatLong Boundingbox
    282289grep -i 'proj: ll' $PERM/PROJ_INFO > /dev/null
    283 if [ $? -ne 0 ] && [ ${USEGDALWARP} -eq 0 ]; then
    284   n=$ll_n
    285   s=$ll_s
    286   e=$ll_e
    287   w=$ll_w
    288   echo "LatLong wgs84 bounding box = N $n S $s W $w E $e"
     290if [ $? -ne 0 ] && [ "$USEGDALWARP" -eq 0 ] ; then
     291    n="$ll_n"
     292    s="$ll_s"
     293    e="$ll_e"
     294    w="$ll_w"
     295    g.message -v "LatLong wgs84 bounding box = N $n S $s W $w E $e"
    289296else
    290   #We have LatLong projection, no warp is needed!
    291   USEGDALWARP=1
    292   #There is a bug in nasa WMS service, it provides images which are lager then
    293   #the world :(, we have to crop the images
    294   if [ "$n" == "90" -a "$s" == "-90" -a "$w" == "-180" -a "$e" == "180" ] ; then
    295 
    296   # check if we have bc
    297   if [ ! -x "`which bc`" ] ; then
    298     echo "bc required, please install first" 2>&1
    299     exit 1
    300   fi
    301    #We request a smaller image from the wms server
    302    n=`echo "$n - 0.001" | bc`
    303    s=`echo "$s + 0.001" | bc`
    304    e=`echo "$e - 0.001" | bc`
    305    w=`echo "$w + 0.001" | bc`
    306   fi
     297    #We have LatLong projection, no warp is needed!
     298    USEGDALWARP=1
     299    #There is a bug in nasa WMS service, it provides images which are lager then
     300    #the world :(, we have to crop the images
     301    if [ "$n" = "90" -a "$s" = "-90" ] && \
     302       [ "$w" = "-180" -a "$e" = "180" ] ; then
     303
     304        # check if we have bc
     305        if [ ! -x "`which bc`" ] ; then
     306            g.message -e "bc required, please install first"
     307            exit 1
     308        fi
     309        #We request a smaller image from the wms server
     310        n=`echo "$n - 0.001" | bc`
     311        s=`echo "$s + 0.001" | bc`
     312        e=`echo "$e - 0.001" | bc`
     313        w=`echo "$w + 0.001" | bc`
     314    fi
    307315fi
    308316
     
    310318grep -i 'proj: ll' $PERM/PROJ_INFO > /dev/null
    311319if [ $? -ne 0 ] && [ ${USEGDALWARP} -eq 1 ] ; then
    312   echo "NASA onearth data are in Latitude/Longitude. The current"
    313   echo "location projection differs and you dont have gdalwarp! STOP."
    314   exit 1
     320    g.message -e "NASA OnEarth data are in Latitude/Longitude. The \
     321                  current location projection differs and you don't \
     322                  have gdalwarp! STOP."
     323    exit 1
    315324fi
    316325
     
    319328TMPDIR="`g.tempfile pid=$$`"
    320329if [ $? -ne 0 ] || [ -z "$TMPDIR" ] ; then
    321     echo "ERROR: unable to create temporary files" 1>&2
     330    g.message -e "Unable to create temporary files"
    322331    exit 1
    323332fi
     
    328337#import every choice that can be made
    329338
    330 if [ $GIS_FLAG_l -eq 1 ] ; then
    331 LAYER=${GLOBAL_MOSAIC_LAYER}
    332 STYLE=${GIS_OPT_tmband}
    333 TYPE="LandsatTM"
    334 echo " "
    335 echo "======================================================="
    336 echo "Will download and import ${TYPE} Data with band ${STYLE}"
    337 echo "======================================================="
    338 GetData
    339 ImportData
    340 fi
    341 
    342 if [ $GIS_FLAG_s -eq 1 ] ; then
    343 LAYER=${SRTM_MAG_LAYER}
    344 STYLE=${GIS_OPT_srtmband}
    345 TYPE="SRTM"
    346 echo " "
    347 echo "======================================================="
    348 echo "Will download and import ${TYPE} Data with band ${STYLE}"
    349 echo "======================================================="
    350 GetData
    351 ImportData
    352 fi
    353 
    354 if [ $GIS_FLAG_b -eq 1 ] ; then
    355 LAYER=${BMNG_LAYER}
    356 STYLE=${GIS_OPT_month}
    357 TYPE="BMNG"
    358 echo " "
    359 echo "======================================================="
    360 echo "Will download and import ${TYPE} Data of month ${STYLE}"
    361 echo "======================================================="
    362 GetData
    363 ImportData
    364 fi
    365 
    366 if [ $GIS_FLAG_t -eq 1 ] ; then
    367 LAYER=${DAILY_TERRA_LAYER}
    368 TIME="&time=${GIS_OPT_time}"
    369 STYLE=""
    370 TYPE="Daily_Terra"
    371 echo " "
    372 echo "======================================================="
    373 echo "Will download and import ${TYPE} Data"
    374 echo "======================================================="
    375 GetData
    376 ImportData
    377 fi
    378 
    379 if [ $GIS_FLAG_a -eq 1 ] ; then
    380 LAYER=${DAILY_AQUA_LAYER}
    381 TIME="&time=${GIS_OPT_time}"
    382 STYLE=""
    383 TYPE="Daily_Aqua"
    384 echo " "
    385 echo "======================================================="
    386 echo "Will download and import ${TYPE} Data"
    387 echo "======================================================="
    388 GetData
    389 ImportData
     339if [ $GIS_FLAG_L -eq 1 ] ; then
     340    LAYER="$GLOBAL_MOSAIC_LAYER"
     341    STYLE="$GIS_OPT_TMBAND"
     342    TYPE="LandsatTM"
     343    g.message -v "Will download and import $TYPE Data with band $STYLE"
     344    GetData
     345    ImportData
     346fi
     347
     348
     349if [ $GIS_FLAG_S -eq 1 ] ; then
     350    LAYER="$SRTM_MAG_LAYER"
     351    STYLE="$GIS_OPT_SRTMBAND"
     352    TYPE="SRTM"
     353    g.message -v "Will download and import $TYPE Data with band $STYLE"
     354    GetData
     355    ImportData
     356fi
     357
     358if [ $GIS_FLAG_B -eq 1 ] ; then
     359    LAYER="$BMNG_LAYER"
     360    STYLE="$GIS_OPT_MONTH"
     361    TYPE="BMNG"
     362    g.message -v "Will download and import $TYPE Data of month $STYLE"
     363    GetData
     364    ImportData
     365fi
     366
     367if [ $GIS_FLAG_T -eq 1 ] ; then
     368    LAYER="$DAILY_TERRA_LAYER"
     369    TIME="&time=$GIS_OPT_TIME"
     370    STYLE=""
     371    TYPE="Daily_Terra"
     372    g.message -v "Will download and import $TYPE Data"
     373    GetData
     374    ImportData
     375fi
     376
     377if [ $GIS_FLAG_A -eq 1 ] ; then
     378    LAYER="$DAILY_AQUA_LAYER"
     379    TIME="&time=$GIS_OPT_TIME"
     380    STYLE=""
     381    TYPE="Daily_Aqua"
     382    g.message -v "Will download and import $TYPE Data"
     383    GetData
     384    ImportData
    390385fi
    391386
    392387#remove the temp dir
    393388rm -rf "$TMPDIR"
    394 echo " "
    395 echo "========"
    396 echo "Finished"
    397 echo "========"
     389g.message -v "Finished"
     390
    398391exit 0
    399392
Note: See TracChangeset for help on using the changeset viewer.