~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to scripts/r.in.wms/wms.request

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
 
3
 
############################################################################
4
 
#
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
11
 
#
12
 
#               This program is free software under the GNU General Public
13
 
#               License (>=v2). Read the file COPYING that comes with GRASS
14
 
#               for details.
15
 
#
16
 
#############################################################################
17
 
 
18
 
#%Module
19
 
#%  description: Builds download requests for WMS servers.
20
 
#%  keywords: wms
21
 
#%End
22
 
#%flag
23
 
#% key: o
24
 
#% description: Don't request transparent data
25
 
#%end
26
 
#%flag
27
 
#% key: c
28
 
#% description: Clean out existing data
29
 
#%end
30
 
#%flag
31
 
#% key: t
32
 
#% description: Use 
33
 
#%end
34
 
#%flag
35
 
#% key: p
36
 
#% description: This projection is the srs projection.
37
 
#%end
38
 
#%option
39
 
#% key: folder
40
 
#% type: string
41
 
#% description: Folder in which to save downloaded files
42
 
#% required : yes
43
 
#%end
44
 
#%option
45
 
#% key: prefix
46
 
#% type: string
47
 
#% description: Prefix file names with this
48
 
#% required : yes
49
 
#%end
50
 
#%option
51
 
#% key: region
52
 
#% type: string
53
 
#% description: Named region that defines the tileset.
54
 
#% required : no
55
 
#%end
56
 
#%option
57
 
#% key: mapserver
58
 
#% type: string
59
 
#% description: Mapserver to request data from
60
 
#% required: yes
61
 
#%end
62
 
#%option
63
 
#% key: layers
64
 
#% type: string
65
 
#% description: Layers to request from map server
66
 
#% multiple: yes
67
 
#% required: yes
68
 
#%end
69
 
#%option
70
 
#% key: styles
71
 
#% type: string
72
 
#% description: Styles to request from map server
73
 
#% multiple: yes
74
 
#% required: no
75
 
#%end
76
 
#%option
77
 
#% key: srs
78
 
#% type: string
79
 
#% description: Source projection to request from server
80
 
#% answer:EPSG:4326
81
 
#%end
82
 
#%option
83
 
#% key: format
84
 
#% type: string
85
 
#% description: Image format requested from the server
86
 
#% options: geotiff,tiff,jpeg,gif,png
87
 
#% answer: geotiff
88
 
#% required: no
89
 
#%end
90
 
#%option
91
 
#% key: maxcols
92
 
#% type: integer
93
 
#% description: Maximum columns to request at a time. 
94
 
#% answer: 1024
95
 
#% required : no
96
 
#%end
97
 
#%option
98
 
#% key: maxrows
99
 
#% type: integer
100
 
#% description: Maximum rows to request at a time
101
 
#% answer: 1024
102
 
#% required : no
103
 
#%end
104
 
#%option
105
 
#% key: tileoptions
106
 
#% type: string
107
 
#% description: Additional options for r.tileset
108
 
#% required : no
109
 
#%end
110
 
#%option
111
 
#% key: wmsquery
112
 
#% type:string
113
 
#% description: Addition query options for server
114
 
#% answer: version=1.1.1
115
 
#%end
116
 
# FIXME: Remove before GRASS 7 is released
117
 
#%option
118
 
#% key: v
119
 
#% type: integer
120
 
#% description: Verbosity level
121
 
#% answer: 1
122
 
#%end
123
 
 
124
 
if  [ -z "$GISBASE" ] ; then
125
 
    echo "You must be in GRASS GIS to run this program." 1>&2
126
 
    exit 1
127
 
fi
128
 
 
129
 
if [ "$1" != "@ARGS_PARSED@" ] ; then
130
 
  exec g.parser "$0" "$@"
131
 
fi
132
 
 
133
 
g.message -d "[wms.request]"
134
 
 
135
 
 
136
 
# Get the system name
137
 
SYSTEM=`uname -s`
138
 
case $SYSTEM in
139
 
MINGW* | MSYS*)
140
 
        MINGW=1
141
 
        ;;
142
 
CYGWIN*)
143
 
        CYGWIN=1
144
 
        ;;
145
 
Darwin*)
146
 
        MACOSX=1
147
 
        ;;
148
 
esac
149
 
 
150
 
# in case of fire, break glass
151
 
dos2unix_path()
152
 
{
153
 
   echo "$1" | sed -e 's|^\([A-Za-z]\):|/\1|' -e 's|\\|/|g'
154
 
}
155
 
 
156
 
# Remember the intial field seperator
157
 
defaultIFS="$IFS"
158
 
 
159
 
 
160
 
####################################
161
 
# name:     message
162
 
# purpose:  displays messages to the user
163
 
# usage: message level text
164
 
 
165
 
message () {
166
 
    if [ "$1" -lt "$GIS_OPT_V" ] ; then
167
 
        shift
168
 
        echo "$@"  
169
 
    fi
170
 
}
171
 
 
172
 
 
173
 
BC="bc"
174
 
BCARGS="-l"
175
 
 
176
 
####################################
177
 
# name: calculate
178
 
# purpose: perform calculations
179
 
# usage: varname "expr"
180
 
 
181
 
calculate() {
182
 
    message 3 "$2"
183
 
    c_tmp=`echo "$2" | "$BC" "$BCARGS"`
184
 
    eval $1=$c_tmp
185
 
}
186
 
 
187
 
####################################
188
 
# Calculate the number of tiles!!
189
 
# Download and them
190
 
GetTiles() {
191
 
  g.message "Calculating tiles"
192
 
 
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 
198
 
 
199
 
 
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"
205
 
     exit 1
206
 
  fi
207
 
 
208
 
  NUMBER_OF_TILES=0 #The number of the tiles 
209
 
  #Calculate the number of tiles
210
 
  for i in $TILES ; do
211
 
        NUMBER_OF_TILES=`expr "$NUMBER_OF_TILES" + 1`
212
 
  done
213
 
 
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."
221
 
  fi
222
 
 
223
 
  NUMBER_OF_TILES=0 #The number of the tiles 
224
 
 
225
 
  mkdir -p "$GIS_OPT_FOLDER"
226
 
 
227
 
  FOLDERPLUS="$GIS_OPT_FOLDER/${GIS_OPT_PREFIX}_$GIS_OPT_REGION"
228
 
 
229
 
  if [ $GIS_FLAG_C -eq 1 ] ; then
230
 
    g.message -v message="Removing files <${FOLDERPLUS}*>"
231
 
    rm -f "$FOLDERPLUS"*
232
 
  fi
233
 
 
234
 
  if [ -x "`which wget`" ] ; then
235
 
    REQUESTFILE="$FOLDERPLUS.wget"
236
 
  else
237
 
    REQUESTFILE="$FOLDERPLUS.curl"
238
 
  fi
239
 
 
240
 
  #reset the requestfile
241
 
  echo > "$REQUESTFILE"
242
 
 
243
 
  echo "$PROJ4_SRS" > "$FOLDERPLUS.proj4"
244
 
 
245
 
  for i in $TILES ; do
246
 
        eval "$i"
247
 
        SIZE="bbox=$w,$s,$e,$n&width=$cols&height=$rows"
248
 
        message 1 "$SIZE"
249
 
        IMAGEFILE="${FOLDERPLUS}_$NUMBER_OF_TILES"
250
 
 
251
 
        if [ "$MINGW" ] ; then
252
 
            OUTPUT_FILE=`dos2unix_path "${IMAGEFILE}${FILE_EXTENT}"`
253
 
        else
254
 
            OUTPUT_FILE="${IMAGEFILE}${FILE_EXTENT}"
255
 
        fi
256
 
 
257
 
        # We could add world files here to help out gdalwarp.
258
 
        # And here it is:
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"
275
 
 
276
 
        NUMBER_OF_TILES=`expr "$NUMBER_OF_TILES" + 1`
277
 
  done
278
 
}
279
 
 
280
 
 
281
 
# Initialize variables:
282
 
 
283
 
SERVER="$GIS_OPT_MAPSERVER"
284
 
SRS="$GIS_OPT_SRS"
285
 
# use `tr '[:upper:]' '[:lower:]'` instead?
286
 
SRS_lower=`echo "$SRS" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"`
287
 
 
288
 
 
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
292
 
 
293
 
    PROJ4_SRS=`g.proj -j | (
294
 
        PROJ4_SRS=
295
 
        while read line ; do
296
 
            PROJ4_SRS="$PROJ4_SRS '$line'"
297
 
        done
298
 
        echo "$PROJ4_SRS"
299
 
    )`
300
 
 
301
 
    eval `g.proj -p | grep ^meters | sed "s/\\s*:\\s*/=/"`
302
 
    SRS_SCALE=$meters;
303
 
else
304
 
    PROJ4_SRS="+init=$SRS_lower"
305
 
    SRS_SCALE=1
306
 
fi
307
 
 
308
 
WMS_QUERY="$GIS_OPT_WMSQUERY"
309
 
 
310
 
if [ -z "$GIS_OPT_REGION" ] ; then
311
 
        TILESET_OPTIONS="$GIS_OPT_TILEOPTIONS"
312
 
else
313
 
        TILESET_OPTIONS="region=$GIS_OPT_REGION $GIS_OPT_TILEOPTIONS"
314
 
fi
315
 
 
316
 
if [ "$GIS_FLAG_O" -eq 1 ] ; then
317
 
        TRANSPARENCY="transparent=FALSE"
318
 
else
319
 
        TRANSPARENCY="transparent=TRUE"
320
 
fi
321
 
 
322
 
case "$GIS_OPT_FORMAT" in
323
 
    "geotiff") FORMAT="image/geotiff"
324
 
               WORLDFILE=".tfw"
325
 
               FILE_EXTENT=".geotiff"
326
 
               ;;
327
 
    "tiff") FORMAT="image/tiff"
328
 
               WORLDFILE=".tfw"
329
 
               FILE_EXTENT=".tiff"
330
 
               ;;
331
 
    "png") FORMAT="image/png"
332
 
               WORLDFILE=".pgw"
333
 
               FILE_EXTENT=".png"
334
 
               ;;
335
 
    "jpeg") FORMAT="image/jpeg"
336
 
               WORLDFILE=".jgw"
337
 
               FILE_EXTENT=".jpeg"
338
 
               ;;
339
 
    "gif") FORMAT="image/gif"
340
 
               WORLDFILE=".gfw"
341
 
               FILE_EXTENT=".gif"
342
 
               ;;
343
 
esac
344
 
 
345
 
GetTiles
346
 
 
347
 
exit