r.in.wms.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 WMS/WMTS/NASA OnEarth server.
  12. #% keywords: raster
  13. #% keywords: import
  14. #% keywords: WMS
  15. #% keywords: WMTS
  16. #% keywords: OnEarth
  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
  46. #% answer: geotiff
  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. #% guisection: Request
  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 G_OPT_F_INPUT
  127. #% key: capfile
  128. #% required: no
  129. #% gisprompt: old,bin,file
  130. #% description: Capabilities file to parse (input). It is relevant for WMTS_GRASS and OnEarth_GRASS drivers
  131. #%end
  132. #%option G_OPT_F_OUTPUT
  133. #% key: capfile_output
  134. #% required: no
  135. #% description: File in which the server capabilities will be saved ('c' flag)
  136. #%end
  137. #%flag
  138. #% key: c
  139. #% description: Get the server capabilities then exit
  140. #% guisection: Request
  141. #% suppress_required: yes
  142. #%end
  143. #%flag
  144. #% key: o
  145. #% description: Don't request transparent data
  146. #% guisection: Map style
  147. #%end
  148. import os
  149. import sys
  150. sys.path.insert(1, os.path.join(os.path.dirname(sys.path[0]), 'etc', 'r.in.wms'))
  151. import grass.script as grass
  152. def GetRegionParams(opt_region):
  153. # set region
  154. if opt_region:
  155. reg_spl = opt_region.strip().split('@', 1)
  156. reg_mapset = '.'
  157. if len(reg_spl) > 1:
  158. reg_mapset = reg_spl[1]
  159. if not grass.find_file(name = reg_spl[0], element = 'windows', mapset = reg_mapset)['name']:
  160. grass.fatal(_("Region <%s> not found") % opt_region)
  161. if opt_region:
  162. s = grass.read_command('g.region',
  163. quiet = True,
  164. flags = 'ug',
  165. region = opt_region)
  166. region_params = grass.parse_key_val(s, val_type = float)
  167. else:
  168. region_params = grass.region()
  169. return region_params
  170. def main():
  171. if 'GRASS' in options['driver']:
  172. grass.debug("Using GRASS driver")
  173. from wms_drv import WMSDrv
  174. wms = WMSDrv()
  175. elif 'GDAL' in options['driver']:
  176. grass.debug("Using GDAL WMS driver")
  177. from wms_gdal_drv import WMSGdalDrv
  178. wms = WMSGdalDrv()
  179. if flags['c']:
  180. wms.GetCapabilities(options)
  181. else:
  182. from wms_base import GRASSImporter
  183. options['region'] = GetRegionParams(options['region'])
  184. fetched_map = wms.GetMap(options, flags)
  185. grass.message(_("Importing raster map into GRASS..."))
  186. if not fetched_map:
  187. grass.warning(_("Nothing to import.\nNo data has been downloaded from wms server."))
  188. return
  189. importer = GRASSImporter(options['output'])
  190. importer.ImportMapIntoGRASS(fetched_map)
  191. return 0
  192. if __name__ == "__main__":
  193. options, flags = grass.parser()
  194. sys.exit(main())