wms.download 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/bin/sh
  2. ############################################################################
  3. #
  4. # MODULE: wms.download for GRASS 6
  5. # AUTHOR(S): Cedric Shock (cedric AT shockfamily.net)
  6. # Based on r.in.onearth by Soeren Gebbert and Markus Neteler
  7. # And on r.in.wms by Jachym Cepicky
  8. # PURPOSE: Downloads data from servers
  9. # COPYRIGHT: (C) 2005, 2006 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (>=v2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. #%Module
  17. #% description: Downloads data from servers.
  18. #% keywords: wms
  19. #%End
  20. #%flag
  21. #% key: g
  22. #% label: Use GET method instead of POST data method
  23. #% description: This may be needed to connect to servers which lack POST capability
  24. #%end
  25. #%option
  26. #% key: requestfile
  27. #% type: string
  28. #% description: File that lists the tiles to download
  29. #% required : yes
  30. #%end
  31. #%option
  32. #% key: wgetoptions
  33. #% type: string
  34. #% description: Additional options for wget
  35. #% answer: -c -t 5
  36. #% required : no
  37. #%end
  38. #%option
  39. #% key: curloptions
  40. #% type: string
  41. #% description: Additional options for curl
  42. #% answer: -C - --retry 5
  43. #% required : no
  44. #%end
  45. #%option
  46. # FIXME: Remove before GRASS 7 is released
  47. #% key: v
  48. #% type: integer
  49. #% description: Verbosity level
  50. #% answer: 1
  51. #%end
  52. if [ -z "$GISBASE" ] ; then
  53. echo "You must be in GRASS GIS to run this program." 1>&2
  54. exit 1
  55. fi
  56. if [ "$1" != "@ARGS_PARSED@" ] ; then
  57. exec g.parser "$0" "$@"
  58. fi
  59. g.message -d "[wms.download]"
  60. #### setup temporary file
  61. TMP="`g.tempfile pid=$$`"
  62. if [ $? -ne 0 ] || [ -z "$TMP" ] ; then
  63. g.message -e "Unable to create temporary files"
  64. exit 1
  65. fi
  66. # check if we have wget or curl
  67. if [ ! -x "`which wget`" ] ; then
  68. if [ ! -x "`which curl`" ] ; then
  69. g.message -e "one of wget or curl is required, please install either first"
  70. exit 1
  71. else
  72. USE_CURL=1
  73. fi
  74. else
  75. USE_WGET=1
  76. fi
  77. # Remember the intial field seperator
  78. defaultIFS=$IFS
  79. #######################################################################
  80. # name: exitprocedure
  81. # purpose: removes all temporary files
  82. #
  83. exitprocedure()
  84. {
  85. g.message -e 'User break!'
  86. rm -f "${TMP}"*
  87. exit 1
  88. }
  89. trap "exitprocedure" 2 3 15
  90. BC="bc"
  91. BCARGS="-l"
  92. #####################
  93. # name: calculate
  94. # purpose: perform calculations
  95. # usage: varname "expr"
  96. calculate() {
  97. g.message message="$2"
  98. c_tmp=`echo "$2" | $BC $BCARGS`
  99. eval $1=$c_tmp
  100. }
  101. ################################################################
  102. # Download the tiles!!
  103. GetTiles() {
  104. g.message "Downloading tiles"
  105. # init POST-data vs. GET URL method variable
  106. if [ "$GIS_FLAG_G" -eq 0 ] ; then
  107. POST_DATA_OK=1
  108. else
  109. POST_DATA_OK=0
  110. fi
  111. CONTENTS=`cat "${REQUESTFILE}"`
  112. NUMBER_OF_TILES=0
  113. for line in $CONTENTS ; do
  114. g.message -d message="wget command: [$line]" debug=2
  115. eval "$line"
  116. emptyness=`file "$OUTPUT_FILE" | grep empty$`
  117. if [ -f "$OUTPUT_FILE" ] && [ -z "$emptyness" ] ; then
  118. g.message "Tile already downloaded"
  119. else
  120. GetData
  121. if [ $? -ne 0 ] ; then
  122. NUMBER_OF_TILES=`expr $NUMBER_OF_TILES + 1`
  123. fi
  124. fi
  125. done
  126. if [ $NUMBER_OF_TILES -ne 0 ] ; then
  127. g.message -w "$NUMBER_OF_TILES failed to download"
  128. return 1
  129. else
  130. g.message "All tiles downloaded successfully"
  131. return 0
  132. fi
  133. }
  134. ##################################
  135. #Get the data from the WMS server
  136. GetData() {
  137. g.message "Downloading data"
  138. g.message -v message="Requesting Data from <${SERVER}:>"
  139. g.message -v message="$STRING"
  140. if [ "$POST_DATA_OK" -eq 1 ] ; then
  141. #download the File from the Server
  142. if [ "$USE_WGET" ] ; then
  143. wget ${WGET_OPTIONS} --post-data="${STRING}" "${SERVER}" -O "${OUTPUT_FILE}"
  144. else
  145. curl ${CURL_OPTIONS} -o "${OUTPUT_FILE}" -d "${STRING}" "${SERVER}"
  146. fi
  147. if [ $? -ne 0 ] ; then
  148. g.message -e "Failed while downloading the data"
  149. return 1
  150. fi
  151. if [ ! -f "$OUTPUT_FILE" ] ; then
  152. g.message -e "Not able to download the data"
  153. return 1
  154. fi
  155. # work-around for brain-dead ArcIMS servers which want POST-data as part of the GET URL
  156. # (this is technically allowed by OGC WMS def v1.3.0 Sec6.3.4)
  157. if [ `wc -c < "$OUTPUT_FILE"` -eq 0 ] ; then
  158. g.message -w "Downloaded image file was empty -- trying another method"
  159. POST_DATA_OK=0
  160. fi
  161. fi # no else!
  162. if [ "$POST_DATA_OK" -eq 0 ] ; then
  163. g.message -v message=""
  164. if [ "$USE_WGET" ] ; then
  165. wget ${WGET_OPTIONS} "${SERVER}?${STRING}" -O "${OUTPUT_FILE}"
  166. else
  167. curl ${CURL_OPTIONS} -o "${OUTPUT_FILE}" "${SERVER}?${STRING}"
  168. fi
  169. if [ $? -ne 0 ] ; then
  170. g.message -e "Failed while downloading the data"
  171. return 1
  172. fi
  173. if [ ! -f "$OUTPUT_FILE" ] || [ `wc -c < "$OUTPUT_FILE"` -eq 0 ] ; then
  174. g.message -e "Not able to download the data"
  175. return 1
  176. fi
  177. fi
  178. return 0
  179. }
  180. # Initialize variables:
  181. #wget has many options
  182. WGET_OPTIONS="${GIS_OPT_WGETOPTIONS}"
  183. CURL_OPTIONS="${GIS_OPT_CURLOPTIONS}"
  184. REQUESTFILE="${GIS_OPT_REQUESTFILE}"
  185. #Get all the data
  186. GetTiles
  187. # Clean up:
  188. rm -f "${TMP}"*