r.in.wms.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 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 WMS server.
  12. #% keywords: raster
  13. #% keywords: import
  14. #% keywords: wms
  15. #%end
  16. #%option
  17. #% key: url
  18. #% type: string
  19. #% description: URL of WMS server
  20. #% required: yes
  21. #%end
  22. #%option
  23. #% key: layers
  24. #% type: string
  25. #% description: Layers to request from WMS server
  26. #% multiple: yes
  27. #% required: yes
  28. #%end
  29. #%option G_OPT_R_OUTPUT
  30. #% description: Name for output raster map
  31. #%end
  32. #%option
  33. #% key: srs
  34. #% type: integer
  35. #% description: EPSG number of source projection for request
  36. #% answer:4326
  37. #% guisection: Request properties
  38. #%end
  39. #%option
  40. #% key: region
  41. #% type: string
  42. #% description: Named region to request data for. Current region used if omitted
  43. #% guisection: Request properties
  44. #%end
  45. #%option
  46. #% key: wms_version
  47. #% type:string
  48. #% description: WMS standard
  49. #% options: 1.1.1,1.3.0
  50. #% answer: 1.1.1
  51. #% guisection: Request properties
  52. #%end
  53. #%option
  54. #% key: format
  55. #% type: string
  56. #% description: Image format requested from the server
  57. #% options: geotiff,tiff,jpeg,gif,png
  58. #% answer: geotiff
  59. #% guisection: Request properties
  60. #%end
  61. #%option
  62. #% key: method
  63. #% type: string
  64. #% description: Reprojection method to use
  65. #% options:near,bilinear,cubic,cubicspline
  66. #% answer:near
  67. #% guisection: Request properties
  68. #%end
  69. #%option
  70. #% key: maxcols
  71. #% type:integer
  72. #% description: Maximum columns to request at a time
  73. #% answer:400
  74. #% guisection: Request properties
  75. #%end
  76. #%option
  77. #% key: maxrows
  78. #% type: integer
  79. #% description: Maximum rows to request at a time
  80. #% answer: 300
  81. #% guisection: Request properties
  82. #%end
  83. #%option
  84. #% key: urlparams
  85. #% type:string
  86. #% description: Additional query parameters for server
  87. #% guisection: Request properties
  88. #%end
  89. #%option
  90. #% key: username
  91. #% type:string
  92. #% description: Username for server connection
  93. #% guisection: Request properties
  94. #%end
  95. #%option
  96. #% key: password
  97. #% type:string
  98. #% description: Password for server connection
  99. #% guisection: Request properties
  100. #%end
  101. #%option
  102. #% key: styles
  103. #% type: string
  104. #% description: Styles to request from map server
  105. #% multiple: yes
  106. #% guisection: Map style
  107. #%end
  108. #%option
  109. #% key: bgcolor
  110. #% type: string
  111. #% description: Color of map background
  112. #% guisection: Map style
  113. #%end
  114. #%flag
  115. #% key: o
  116. #% description: Don't request transparent data
  117. #% guisection: Map style
  118. #%end
  119. #%flag
  120. #% key: c
  121. #% description: Get capabilities
  122. #% guisection: Request properties
  123. #% suppress_required: yes
  124. #%end
  125. #%option
  126. #% key: driver
  127. #% type:string
  128. #% description: Driver for communication with server
  129. #% 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;
  130. #% options:WMS_GDAL, WMS_GRASS, WMTS_GRASS, OnEarth_GRASS
  131. #% answer:WMS_GRASS
  132. #%end
  133. #%option G_OPT_F_INPUT
  134. #% key: capfile
  135. #% required: no
  136. #% gisprompt: old,file,bin_input
  137. #% description: Capabilities file to load
  138. #%end
  139. #%option G_OPT_F_OUTPUT
  140. #% key: capfile_output
  141. #% required: no
  142. #% gisprompt: old,file,bin_input
  143. #% description: File where capabilities will be saved (only with 'c' flag).
  144. #%end
  145. import os
  146. import sys
  147. sys.path.insert(1, os.path.join(os.path.dirname(sys.path[0]), 'etc', 'r.in.wms'))
  148. import grass.script as grass
  149. def GetRegionParams(opt_region):
  150. # set region
  151. if opt_region:
  152. reg_spl = opt_region.strip().split('@', 1)
  153. reg_mapset = '.'
  154. if len(reg_spl) > 1:
  155. reg_mapset = reg_spl[1]
  156. if not grass.find_file(name = reg_spl[0], element = 'windows', mapset = reg_mapset)['name']:
  157. grass.fatal(_("Region <%s> not found") % opt_region)
  158. if opt_region:
  159. s = grass.read_command('g.region',
  160. quiet = True,
  161. flags = 'ug',
  162. region = opt_region)
  163. region_params = grass.parse_key_val(s, val_type = float)
  164. else:
  165. region_params = grass.region()
  166. return region_params
  167. def main():
  168. if 'GRASS' in options['driver']:
  169. grass.debug("Using GRASS driver")
  170. from wms_drv import WMSDrv
  171. wms = WMSDrv()
  172. elif 'GDAL' in options['driver']:
  173. grass.debug("Using GDAL WMS driver")
  174. from wms_gdal_drv import WMSGdalDrv
  175. wms = WMSGdalDrv()
  176. if flags['c']:
  177. wms.GetCapabilities(options)
  178. else:
  179. from wms_base import GRASSImporter
  180. options['region'] = GetRegionParams(options['region'])
  181. fetched_map = wms.GetMap(options, flags)
  182. grass.message(_("Importing raster map into GRASS..."))
  183. if not fetched_map:
  184. grass.warning(_("Nothing to import.\nNo data has been downloaded from wms server."))
  185. return
  186. importer = GRASSImporter(options['output'])
  187. importer.ImportMapIntoGRASS(fetched_map)
  188. return 0
  189. if __name__ == "__main__":
  190. options, flags = grass.parser()
  191. sys.exit(main())