r.in.wms.py 6.7 KB

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