wms.request 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #!/bin/sh
  2. ############################################################################
  3. #
  4. # MODULE: wms.request 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: Builds requests for downloading data from web mapping 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: Builds download requests for WMS servers.
  18. #% keywords: wms
  19. #%End
  20. #%flag
  21. #% key: o
  22. #% description: Don't request transparent data
  23. #%end
  24. #%flag
  25. #% key: c
  26. #% description: Clean out existing data
  27. #%end
  28. #%flag
  29. #% key: t
  30. #% description: Use
  31. #%end
  32. #%flag
  33. #% key: p
  34. #% description: This projection is the srs projection.
  35. #%end
  36. #%option
  37. #% key: folder
  38. #% type: string
  39. #% description: Folder in which to save downloaded files
  40. #% required : yes
  41. #%end
  42. #%option
  43. #% key: prefix
  44. #% type: string
  45. #% description: Prefix file names with this
  46. #% required : yes
  47. #%end
  48. #%option
  49. #% key: region
  50. #% type: string
  51. #% description: Named region that defines the tileset.
  52. #% required : no
  53. #%end
  54. #%option
  55. #% key: mapserver
  56. #% type: string
  57. #% description: Mapserver to request data from
  58. #% required: yes
  59. #%end
  60. #%option
  61. #% key: layers
  62. #% type: string
  63. #% description: Layers to request from map server
  64. #% multiple: yes
  65. #% required: yes
  66. #%end
  67. #%option
  68. #% key: styles
  69. #% type: string
  70. #% description: Styles to request from map server
  71. #% multiple: yes
  72. #% required: no
  73. #%end
  74. #%option
  75. #% key: srs
  76. #% type: string
  77. #% description: Source projection to request from server
  78. #% answer:EPSG:4326
  79. #%end
  80. #%option
  81. #% key: format
  82. #% type: string
  83. #% description: Image format requested from the server
  84. #% options: geotiff,tiff,jpeg,gif,png
  85. #% answer: geotiff
  86. #% required: no
  87. #%end
  88. #%option
  89. #% key: maxcols
  90. #% type: integer
  91. #% description: Maximum columns to request at a time.
  92. #% answer: 1024
  93. #% required : no
  94. #%end
  95. #%option
  96. #% key: maxrows
  97. #% type: integer
  98. #% description: Maximum rows to request at a time
  99. #% answer: 1024
  100. #% required : no
  101. #%end
  102. #%option
  103. #% key: tileoptions
  104. #% type: string
  105. #% description: Additional options for r.tileset
  106. #% required : no
  107. #%end
  108. #%option
  109. #% key: wmsquery
  110. #% type:string
  111. #% description: Addition query options for server
  112. #% answer: version=1.1.1
  113. #%end
  114. # FIXME: Remove before GRASS 7 is released
  115. #%option
  116. #% key: v
  117. #% type: integer
  118. #% description: Verbosity level
  119. #% answer: 1
  120. #%end
  121. if [ -z "$GISBASE" ] ; then
  122. echo "You must be in GRASS GIS to run this program." 1>&2
  123. exit 1
  124. fi
  125. if [ "$1" != "@ARGS_PARSED@" ] ; then
  126. exec g.parser "$0" "$@"
  127. fi
  128. g.message -d "[wms.request]"
  129. SED="sed"
  130. GREP="grep"
  131. # check if we have sed
  132. if [ ! -x "`which $SED`" ] ; then
  133. g.message "$SED is required, please install it first"
  134. exit 1
  135. fi
  136. # check if we have grep
  137. if [ ! -x "`which $GREP`" ] ; then
  138. g.message "$GREP is required, please install it first"
  139. exit 1
  140. fi
  141. # Remember the intial field seperator
  142. defaultIFS="$IFS"
  143. ####################################
  144. # name: message
  145. # purpose: displays messages to the user
  146. # usage: message level text
  147. message () {
  148. if [ $1 -lt $GIS_OPT_V ] ; then
  149. shift
  150. echo "$@"
  151. fi
  152. }
  153. BC="bc"
  154. BCARGS="-l"
  155. ####################################
  156. # name: calculate
  157. # purpose: perform calculations
  158. # usage: varname "expr"
  159. calculate() {
  160. message 3 "$2"
  161. c_tmp=`echo "$2" | $BC $BCARGS`
  162. eval $1=$c_tmp
  163. }
  164. ####################################
  165. # Calculate the number of tiles!!
  166. # Download and them
  167. GetTiles() {
  168. g.message "Calculating tiles"
  169. #################################################
  170. # ############## TILE SETTINGS ################ #
  171. #################################################
  172. MAXCOLS=${GIS_OPT_MAXCOLS} #The maximum cols of the biggest tile
  173. MAXROWS=${GIS_OPT_MAXROWS} #The maximum rows of the biggest tile
  174. #Calculate the number of tiles and set up the arrays
  175. message 1 "r.tileset -g sourceproj=\"$PROJ4_SRS\" sourcescale=\"$SRS_SCALE\" overlap=2 maxcols=${MAXCOLS} maxrows=${MAXROWS} $TILESET_OPTIONS"
  176. TILES=`eval "GRASS_VERBOSE=1 r.tileset -g sourceproj=\"$PROJ4_SRS\" sourcescale=\"$SRS_SCALE\" overlap=2 maxcols=${MAXCOLS} maxrows=${MAXROWS} $TILESET_OPTIONS"`
  177. if [ $? -ne 0 ] ; then
  178. g.message -e "r.tileset failure"
  179. exit 1
  180. fi
  181. NUMBER_OF_TILES=0 #The number of the tiles
  182. #Calculate the number of tiles
  183. for i in $TILES ; do
  184. NUMBER_OF_TILES=`expr $NUMBER_OF_TILES + 1`
  185. done
  186. g.message "Requesting ${NUMBER_OF_TILES} tiles."
  187. NUMBER_OF_TILES=0 #The number of the tiles
  188. mkdir -p "${GIS_OPT_FOLDER}"
  189. FOLDERPLUS="${GIS_OPT_FOLDER}/${GIS_OPT_PREFIX}_${GIS_OPT_REGION}"
  190. if [ $GIS_FLAG_C -eq 1 ] ; then
  191. g.message -v message="Removing files <${FOLDERPLUS}*>"
  192. rm -f "$FOLDERPLUS"*
  193. fi
  194. if [ -x "`which wget`" ] ; then
  195. REQUESTFILE="${FOLDERPLUS}.wget"
  196. else
  197. REQUESTFILE="${FOLDERPLUS}.curl"
  198. fi
  199. #reset the requestfile
  200. echo > "${REQUESTFILE}"
  201. echo "$PROJ4_SRS" > "${FOLDERPLUS}.proj4"
  202. for i in $TILES ; do
  203. eval "$i"
  204. SIZE="bbox=$w,$s,$e,$n&width=$cols&height=$rows"
  205. message 1 "$SIZE"
  206. IMAGEFILE="${FOLDERPLUS}_${NUMBER_OF_TILES}"
  207. OUTPUT_FILE="${IMAGEFILE}${FILE_EXTENT}"
  208. # We could add world files here to help out gdalwarp.
  209. # And here it is:
  210. # Displacement from top left cell to the one to the right of it and to the one below it:
  211. calculate xres "($e - $w) / $cols"
  212. calculate nyres "($s - $n) / $rows"
  213. # Center of top left cell:
  214. calculate top_left_cell_center_x "$w + $xres / 2"
  215. calculate top_left_cell_center_y "$n + $nyres / 2"
  216. #Write the world file:
  217. echo "$xres" > "${IMAGEFILE}${WORLDFILE}"
  218. echo "0.0" >> "${IMAGEFILE}${WORLDFILE}"
  219. echo "0.0" >> "${IMAGEFILE}${WORLDFILE}"
  220. echo "$nyres" >> "${IMAGEFILE}${WORLDFILE}"
  221. echo "$top_left_cell_center_x" >> "${IMAGEFILE}${WORLDFILE}"
  222. echo "$top_left_cell_center_y" >> "${IMAGEFILE}${WORLDFILE}"
  223. #Make the requestt for data:
  224. STRING="request=GetMap&layers=${GIS_OPT_LAYERS}&styles=${GIS_OPT_STYLES}&srs=${SRS}&${SIZE}&format=${FORMAT}&${TRANSPARENCY}&${WMS_QUERY}"
  225. echo "OUTPUT_FILE=\"${OUTPUT_FILE}\";SERVER=\"${SERVER}\";STRING=\"${STRING}\"" >> "${REQUESTFILE}"
  226. NUMBER_OF_TILES=`expr $NUMBER_OF_TILES + 1`
  227. done
  228. }
  229. # Initialize variables:
  230. SERVER="${GIS_OPT_MAPSERVER}"
  231. SRS="${GIS_OPT_SRS}"
  232. SRS_lower=`echo $SRS | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"`
  233. # If the user asserts that this projection is the same as the source
  234. # use this projection as the source to get a trivial tiling from r.tileset
  235. if [ $GIS_FLAG_P -eq 1 ] ; then
  236. PROJ4_SRS=`g.proj -j`
  237. eval `g.proj -p | $GREP meters | $SED "s/\\s*:\\s*/=/"`
  238. SRS_SCALE=$meters;
  239. else
  240. PROJ4_SRS="+init=$SRS_lower"
  241. SRS_SCALE=1
  242. fi
  243. WMS_QUERY="${GIS_OPT_WMSQUERY}"
  244. if [ -z "$GIS_OPT_REGION" ] ; then
  245. TILESET_OPTIONS="$GIS_OPT_TILEOPTIONS"
  246. else
  247. TILESET_OPTIONS="region=$GIS_OPT_REGION $GIS_OPT_TILEOPTIONS"
  248. fi
  249. if [ $GIS_FLAG_O -eq 1 ] ; then
  250. TRANSPARENCY="transparent=FALSE"
  251. else
  252. TRANSPARENCY="transparent=TRUE"
  253. fi
  254. case "${GIS_OPT_FORMAT}" in
  255. "geotiff") FORMAT="image/geotiff"
  256. WORLDFILE=".tfw"
  257. FILE_EXTENT=".geotiff"
  258. ;;
  259. "tiff") FORMAT="image/tiff"
  260. WORLDFILE=".tfw"
  261. FILE_EXTENT=".tiff"
  262. ;;
  263. "png") FORMAT="image/png"
  264. WORLDFILE=".pgw"
  265. FILE_EXTENT=".png"
  266. ;;
  267. "jpeg") FORMAT="image/jpeg"
  268. WORLDFILE=".jgw"
  269. FILE_EXTENT=".jpeg"
  270. ;;
  271. "gif") FORMAT="image/gif"
  272. WORLDFILE=".gfw"
  273. FILE_EXTENT=".gif"
  274. ;;
  275. esac
  276. GetTiles
  277. exit