r.in.wms.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!/usr/bin/env python
  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 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 to communication with 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.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: region
  115. #% type: string
  116. #% description: Request data for this named region instead of the current region bounds
  117. #% guisection: Request
  118. #%end
  119. #%option
  120. #% key: bgcolor
  121. #% type: string
  122. #% label: Background color
  123. #% description: Format: 0xRRGGBB
  124. #% guisection: Map style
  125. #%end
  126. #%option
  127. #% key: proxy
  128. #% label: HTTP proxy only GDAL driver (GDAL_HTTP_PROXY)
  129. #% type: string
  130. #% description: HTTP proxy
  131. #%end
  132. #%option
  133. #% key: proxy_user_pw
  134. #% label: User and password for HTTP proxy only for GDAL driver (GDAL_HTTP_PROXYUSERPWD). Must be in the form of [user name]:[password].
  135. #% type: string
  136. #% description: User and password for HTTP proxy
  137. #%end
  138. #%option G_OPT_F_BIN_INPUT
  139. #% key: capfile
  140. #% required: no
  141. #% description: Capabilities file to parse (input). It is relevant for WMTS_GRASS and OnEarth_GRASS drivers
  142. #%end
  143. #%option G_OPT_F_OUTPUT
  144. #% key: capfile_output
  145. #% required: no
  146. #% description: File where the server capabilities will be saved ('c' flag)
  147. #%end
  148. #%flag
  149. #% key: c
  150. #% description: Get the server capabilities then exit
  151. #% guisection: Request
  152. #% suppress_required: yes
  153. #%end
  154. #%flag
  155. #% key: o
  156. #% description: Do not request transparent data
  157. #% guisection: Map style
  158. #%end
  159. #%flag
  160. #% key: b
  161. #% description: Keep original bands (default: create composite)
  162. #% guisection: Map style
  163. #%end
  164. #%rules
  165. #% exclusive: capfile_output, capfile
  166. #%end
  167. import os
  168. import sys
  169. sys.path.insert(1, os.path.join(os.path.dirname(sys.path[0]), 'etc', 'r.in.wms'))
  170. import grass.script as grass
  171. from grass.script.utils import decode
  172. def GetRegionParams(opt_region):
  173. # set region
  174. if opt_region:
  175. reg_spl = opt_region.strip().split('@', 1)
  176. reg_mapset = '.'
  177. if len(reg_spl) > 1:
  178. reg_mapset = reg_spl[1]
  179. if not grass.find_file(name=reg_spl[0], element='windows', mapset=reg_mapset)['name']:
  180. grass.fatal(_("Region <%s> not found") % opt_region)
  181. if opt_region:
  182. s = grass.read_command('g.region',
  183. quiet=True,
  184. flags='ug',
  185. region=opt_region)
  186. region_params = grass.parse_key_val(decode(s), val_type=float)
  187. else:
  188. region_params = grass.region()
  189. return region_params
  190. def main():
  191. if 'GRASS' in options['driver']:
  192. grass.debug("Using GRASS driver")
  193. from wms_drv import WMSDrv
  194. wms = WMSDrv()
  195. elif 'GDAL' in options['driver']:
  196. grass.debug("Using GDAL WMS driver")
  197. from wms_gdal_drv import WMSGdalDrv
  198. wms = WMSGdalDrv()
  199. if flags['c']:
  200. wms.GetCapabilities(options)
  201. else:
  202. from wms_base import GRASSImporter
  203. # set proxy
  204. if options['proxy'] and options['proxy_user_pw']:
  205. wms.setProxy(options['proxy'], options['proxy_user_pw'])
  206. if options['proxy']:
  207. wms.setProxy(options['proxy'])
  208. if 'GRASS' in options['driver']:
  209. grass.warning(_("The proxy will be ignored by the choosen GRASS driver. It is only used with the GDAL driver."))
  210. options['region'] = GetRegionParams(options['region'])
  211. fetched_map = wms.GetMap(options, flags)
  212. grass.message(_("Importing raster map into GRASS..."))
  213. if not fetched_map:
  214. grass.warning(_("Nothing to import.\nNo data has been downloaded from wms server."))
  215. return
  216. importer = GRASSImporter(options['output'], (flags['b'] == False))
  217. importer.ImportMapIntoGRASS(fetched_map)
  218. return 0
  219. if __name__ == "__main__":
  220. options, flags = grass.parser()
  221. sys.exit(main())