3
############################################################################
5
# MODULE: wms.request for GRASS 6
6
# AUTHOR(S): Cedric Shock (cedric AT shockfamily.net)
7
# Based on r.in.onearth by Soeren Gebbert and Markus Neteler
8
# And on r.in.wms by Jachym Cepicky
9
# PURPOSE: Builds requests for downloading data from web mapping servers
10
# COPYRIGHT: (C) 2005, 2006 by the GRASS Development Team
12
# This program is free software under the GNU General Public
13
# License (>=v2). Read the file COPYING that comes with GRASS
16
#############################################################################
19
#% description: Builds download requests for WMS servers.
24
#% description: Don't request transparent data
28
#% description: Clean out existing data
36
#% description: This projection is the srs projection.
41
#% description: Folder in which to save downloaded files
47
#% description: Prefix file names with this
53
#% description: Named region that defines the tileset.
59
#% description: Mapserver to request data from
65
#% description: Layers to request from map server
72
#% description: Styles to request from map server
79
#% description: Source projection to request from server
85
#% description: Image format requested from the server
86
#% options: geotiff,tiff,jpeg,gif,png
93
#% description: Maximum columns to request at a time.
100
#% description: Maximum rows to request at a time
107
#% description: Additional options for r.tileset
113
#% description: Addition query options for server
114
#% answer: version=1.1.1
116
# FIXME: Remove before GRASS 7 is released
120
#% description: Verbosity level
124
if [ -z "$GISBASE" ] ; then
125
echo "You must be in GRASS GIS to run this program." 1>&2
129
if [ "$1" != "@ARGS_PARSED@" ] ; then
130
exec g.parser "$0" "$@"
133
g.message -d "[wms.request]"
136
# Get the system name
150
# in case of fire, break glass
153
echo "$1" | sed -e 's|^\([A-Za-z]\):|/\1|' -e 's|\\|/|g'
156
# Remember the intial field seperator
160
####################################
162
# purpose: displays messages to the user
163
# usage: message level text
166
if [ "$1" -lt "$GIS_OPT_V" ] ; then
176
####################################
178
# purpose: perform calculations
179
# usage: varname "expr"
183
c_tmp=`echo "$2" | "$BC" "$BCARGS"`
187
####################################
188
# Calculate the number of tiles!!
191
g.message "Calculating tiles"
193
#################################################
194
# ############## TILE SETTINGS ################ #
195
#################################################
196
MAXCOLS="$GIS_OPT_MAXCOLS" #The maximum cols of the biggest tile
197
MAXROWS="$GIS_OPT_MAXROWS" #The maximum rows of the biggest tile
200
#Calculate the number of tiles and set up the arrays
201
message 1 "r.tileset -g sourceproj=\"$PROJ4_SRS\" sourcescale=\"$SRS_SCALE\" overlap=2 maxcols=$MAXCOLS maxrows=$MAXROWS $TILESET_OPTIONS"
202
TILES=`eval "GRASS_VERBOSE=1 r.tileset -g sourceproj=\"$PROJ4_SRS\" sourcescale=\"$SRS_SCALE\" overlap=2 maxcols=${MAXCOLS} maxrows=${MAXROWS} $TILESET_OPTIONS"`
203
if [ $? -ne 0 ] ; then
204
g.message -e "r.tileset failure"
208
NUMBER_OF_TILES=0 #The number of the tiles
209
#Calculate the number of tiles
211
NUMBER_OF_TILES=`expr "$NUMBER_OF_TILES" + 1`
214
g.message "Requesting $NUMBER_OF_TILES tiles."
215
if [ "$NUMBER_OF_TILES" -gt 200 ] ; then
216
g.message -w "Proceed with care. This number of tiles may exceed \
217
the maximum command line argument length available from your \
218
operating system and cause an error later on in the r.in.gdalwarp \
219
step. In addition it may be considered abusive behavior by the \
220
server provider - check their terms of use."
223
NUMBER_OF_TILES=0 #The number of the tiles
225
mkdir -p "$GIS_OPT_FOLDER"
227
FOLDERPLUS="$GIS_OPT_FOLDER/${GIS_OPT_PREFIX}_$GIS_OPT_REGION"
229
if [ $GIS_FLAG_C -eq 1 ] ; then
230
g.message -v message="Removing files <${FOLDERPLUS}*>"
234
if [ -x "`which wget`" ] ; then
235
REQUESTFILE="$FOLDERPLUS.wget"
237
REQUESTFILE="$FOLDERPLUS.curl"
240
#reset the requestfile
241
echo > "$REQUESTFILE"
243
echo "$PROJ4_SRS" > "$FOLDERPLUS.proj4"
247
SIZE="bbox=$w,$s,$e,$n&width=$cols&height=$rows"
249
IMAGEFILE="${FOLDERPLUS}_$NUMBER_OF_TILES"
251
if [ "$MINGW" ] ; then
252
OUTPUT_FILE=`dos2unix_path "${IMAGEFILE}${FILE_EXTENT}"`
254
OUTPUT_FILE="${IMAGEFILE}${FILE_EXTENT}"
257
# We could add world files here to help out gdalwarp.
259
# Displacement from top left cell to the one to the right of it and to the one below it:
260
calculate xres "($e - $w) / $cols"
261
calculate nyres "($s - $n) / $rows"
262
# Center of top left cell:
263
calculate top_left_cell_center_x "$w + $xres / 2"
264
calculate top_left_cell_center_y "$n + $nyres / 2"
265
#Write the world file:
266
echo "$xres" > "${IMAGEFILE}${WORLDFILE}"
267
echo "0.0" >> "${IMAGEFILE}${WORLDFILE}"
268
echo "0.0" >> "${IMAGEFILE}${WORLDFILE}"
269
echo "$nyres" >> "${IMAGEFILE}${WORLDFILE}"
270
echo "$top_left_cell_center_x" >> "${IMAGEFILE}${WORLDFILE}"
271
echo "$top_left_cell_center_y" >> "${IMAGEFILE}${WORLDFILE}"
272
#Make the requestt for data:
273
STRING="request=GetMap&layers=${GIS_OPT_LAYERS}&styles=${GIS_OPT_STYLES}&srs=${SRS}&${SIZE}&format=${FORMAT}&${TRANSPARENCY}&${WMS_QUERY}"
274
echo "OUTPUT_FILE=\"$OUTPUT_FILE\";SERVER=\"$SERVER\";STRING=\"$STRING\"" >> "$REQUESTFILE"
276
NUMBER_OF_TILES=`expr "$NUMBER_OF_TILES" + 1`
281
# Initialize variables:
283
SERVER="$GIS_OPT_MAPSERVER"
285
# use `tr '[:upper:]' '[:lower:]'` instead?
286
SRS_lower=`echo "$SRS" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"`
289
# If the user asserts that this projection is the same as the source
290
# use this projection as the source to get a trivial tiling from r.tileset
291
if [ $GIS_FLAG_P -eq 1 ] ; then
293
PROJ4_SRS=`g.proj -j | (
296
PROJ4_SRS="$PROJ4_SRS '$line'"
301
eval `g.proj -p | grep ^meters | sed "s/\\s*:\\s*/=/"`
304
PROJ4_SRS="+init=$SRS_lower"
308
WMS_QUERY="$GIS_OPT_WMSQUERY"
310
if [ -z "$GIS_OPT_REGION" ] ; then
311
TILESET_OPTIONS="$GIS_OPT_TILEOPTIONS"
313
TILESET_OPTIONS="region=$GIS_OPT_REGION $GIS_OPT_TILEOPTIONS"
316
if [ "$GIS_FLAG_O" -eq 1 ] ; then
317
TRANSPARENCY="transparent=FALSE"
319
TRANSPARENCY="transparent=TRUE"
322
case "$GIS_OPT_FORMAT" in
323
"geotiff") FORMAT="image/geotiff"
325
FILE_EXTENT=".geotiff"
327
"tiff") FORMAT="image/tiff"
331
"png") FORMAT="image/png"
335
"jpeg") FORMAT="image/jpeg"
339
"gif") FORMAT="image/gif"