1#!/usr/local/bin/python3.8
2"""
3MODULE:    r.in.wms
4
5AUTHOR(S): Stepan Turek <stepan.turek AT seznam.cz>
6
7PURPOSE:   Downloads and imports data from WMS/WMTS/NASA OnEarth server.
8
9COPYRIGHT: (C) 2012-2021 Stepan Turek, and by the GRASS Development Team
10
11This program is free software under the GNU General Public License
12(>=v2). Read the file COPYING that comes with GRASS for details.
13"""
14
15#%module
16#% description: Downloads and imports data from OGC WMS and OGC WMTS web mapping servers.
17#% keyword: raster
18#% keyword: import
19#% keyword: OGC web services
20#% keyword: OGC WMS
21#% keyword: OGC WMTS
22#%end
23
24#%option
25#% key: url
26#% type: string
27#% description: Typically starts with "http://"
28#% required: yes
29#%end
30
31#%option G_OPT_R_OUTPUT
32#% description: Name for output raster map
33#%end
34
35#%option
36#% key: layers
37#% type: string
38#% description: Layer(s) to request from the map server
39#% multiple: yes
40#% required: yes
41#%end
42
43#%option
44#% key: styles
45#% type: string
46#% description: Layer style(s) to request from the map server
47#% multiple: yes
48#% guisection: Map style
49#%end
50
51#%option
52#% key: format
53#% type: string
54#% description: Image format requested from the server
55#% options: geotiff,tiff,jpeg,gif,png,png8
56#% answer: png
57#% guisection: Request
58#%end
59
60#%option
61#% key: srs
62#% type: integer
63#% description: EPSG code of requested source projection
64#% answer:4326
65#% guisection: Request
66#%end
67
68#%option
69#% key: driver
70#% type:string
71#% description: Driver used for communication with the server
72#% descriptions: WMS_GDAL;Download data using GDAL WMS driver;WMS_GRASS;Download data using native GRASS-WMS driver;WMTS_GRASS;Download data using native GRASS-WMTS driver;OnEarth_GRASS;Download data using native GRASS-OnEarth driver;
73#% options:WMS_GDAL, WMS_GRASS, WMTS_GRASS, OnEarth_GRASS
74#% answer:WMS_GRASS
75#% guisection: Connection
76#%end
77
78#%option
79#% key: wms_version
80#% type:string
81#% description: WMS standard version
82#% options: 1.1.0,1.1.1,1.3.0
83#% answer: 1.1.1
84#% guisection: Request
85#%end
86
87#%option
88#% key: maxcols
89#% type:integer
90#% description: Maximum columns to request at a time
91#% answer:512
92#% guisection: Request
93#%end
94
95#%option
96#% key: maxrows
97#% type: integer
98#% description: Maximum rows to request at a time
99#% answer: 512
100#% guisection: Request
101#%end
102
103#%option
104#% key: urlparams
105#% type:string
106#% description: Additional query parameters to pass to the server
107#% guisection: Request
108#%end
109
110#%option
111#% key: username
112#% type:string
113#% description: Username for server connection
114#% guisection: Connection
115#%end
116
117#%option
118#% key: password
119#% type:string
120#% description: Password for server connection
121#% guisection: Connection
122#%end
123
124#%option
125#% key: method
126#% type: string
127#% description: Interpolation method to use in reprojection
128#% options:nearest,linear,cubic,cubicspline
129#% answer:nearest
130#% required: no
131#%end
132
133#%option
134#% key: gdal_createopt
135#% type: string
136#% required: no
137#% multiple: yes
138#% label: GDAL creation option(s) to pass to the output format driver
139#% description: In the form of "NAME=VALUE", separate multiple entries with a comma
140#% guisection: Request
141#%end
142
143#%option
144#% key: region
145#% type: string
146#% description: Request data for this named region instead of the current region bounds
147#% guisection: Request
148#%end
149
150#%option
151#% key: bgcolor
152#% type: string
153#% label: Background color
154#% description: Format: 0xRRGGBB
155#% guisection: Map style
156#%end
157
158#%option
159#% key: proxy
160#% label: HTTP proxy only GDAL driver (GDAL_HTTP_PROXY)
161#% type: string
162#% description: HTTP proxy
163#%end
164
165#%option
166#% key: proxy_user_pw
167#% label: User and password for HTTP proxy only for GDAL driver (GDAL_HTTP_PROXYUSERPWD). Must be in the form of [user name]:[password].
168#% type: string
169#% description: User and password for HTTP proxy
170#%end
171
172#%option G_OPT_F_BIN_INPUT
173#% key: capfile
174#% required: no
175#% description: Capabilities file to parse (input). It is relevant for WMTS_GRASS and OnEarth_GRASS drivers
176#%end
177
178#%option G_OPT_F_OUTPUT
179#% key: capfile_output
180#% required: no
181#% description: File where the server capabilities will be saved ('c' flag)
182#%end
183
184#%flag
185#% key: c
186#% description: Get the server capabilities, print them out, then exit
187#% guisection: Request
188#% suppress_required: yes
189#%end
190
191#%flag
192#% key: o
193#% description: Do not request transparent data
194#% guisection: Map style
195#%end
196
197#%flag
198#% key: b
199#% description: Keep original bands (default: create composite)
200#% guisection: Map style
201#%end
202
203#%rules
204#% exclusive: capfile_output, capfile
205#%end
206
207
208import os
209import sys
210sys.path.insert(1, os.path.join(os.path.dirname(sys.path[0]), 'etc', 'r.in.wms'))
211
212import grass.script as grass
213from grass.script.utils import decode
214
215
216def GetRegionParams(opt_region):
217
218    # set region
219    if opt_region:
220        reg_spl = opt_region.strip().split('@', 1)
221        reg_mapset = '.'
222        if len(reg_spl) > 1:
223            reg_mapset = reg_spl[1]
224
225        if not grass.find_file(name=reg_spl[0], element='windows', mapset=reg_mapset)['name']:
226            grass.fatal(_("Region <%s> not found") % opt_region)
227
228    if opt_region:
229        s = grass.read_command('g.region',
230                               quiet=True,
231                               flags='ug',
232                               region=opt_region)
233        region_params = grass.parse_key_val(decode(s), val_type=float)
234    else:
235        region_params = grass.region()
236
237    return region_params
238
239
240def main():
241
242    if 'GRASS' in options['driver']:
243        grass.debug("Using GRASS driver")
244        from wms_drv import WMSDrv
245        wms = WMSDrv()
246    elif 'GDAL' in options['driver']:
247        grass.debug("Using GDAL WMS driver")
248        from wms_gdal_drv import WMSGdalDrv
249
250        if options['gdal_createopt']:
251            create_options = options['gdal_createopt'].split(',')
252        else:
253            create_options = None
254        wms = WMSGdalDrv(create_options)
255
256    if flags['c']:
257        wms.GetCapabilities(options)
258    else:
259        from wms_base import GRASSImporter
260
261        # set proxy
262        if options['proxy'] and options['proxy_user_pw']:
263            wms.setProxy(options['proxy'], options['proxy_user_pw'])
264        if options['proxy']:
265            wms.setProxy(options['proxy'])
266            if 'GRASS' in options['driver']:
267                grass.warning(_("The proxy will be ignored by the choosen GRASS driver. It is only used with the GDAL driver."))
268
269        options['region'] = GetRegionParams(options['region'])
270        fetched_map = wms.GetMap(options, flags)
271
272        grass.message(_("Importing raster map into GRASS..."))
273        if not fetched_map:
274            grass.warning(_("Nothing to import.\nNo data has been downloaded from wms server."))
275            return
276        importer = GRASSImporter(options['output'], (flags['b'] == False))
277        importer.ImportMapIntoGRASS(fetched_map)
278
279    return 0
280
281
282if __name__ == "__main__":
283    options, flags = grass.parser()
284    sys.exit(main())
285