r.in.wms.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #% keyword: raster
  13. #% keyword: import
  14. #% keyword: OGC web services
  15. #%end
  16. #%option
  17. #% key: url
  18. #% type: string
  19. #% description: Typically starts with "http://"
  20. #% required: yes
  21. #%end
  22. #%option G_OPT_R_OUTPUT
  23. #% description: Name for output raster map
  24. #%end
  25. #%option
  26. #% key: layers
  27. #% type: string
  28. #% description: Layer(s) to request from the map server
  29. #% multiple: yes
  30. #% required: yes
  31. #%end
  32. #%option
  33. #% key: styles
  34. #% type: string
  35. #% description: Layer style(s) to request from the map server
  36. #% multiple: yes
  37. #% guisection: Map style
  38. #%end
  39. #%option
  40. #% key: format
  41. #% type: string
  42. #% description: Image format requested from the server
  43. #% options: geotiff,tiff,jpeg,gif,png,png8
  44. #% answer: png
  45. #% guisection: Request
  46. #%end
  47. #%option
  48. #% key: srs
  49. #% type: integer
  50. #% description: EPSG code of requested source projection
  51. #% answer:4326
  52. #% guisection: Request
  53. #%end
  54. #%option
  55. #% key: driver
  56. #% type:string
  57. #% description: Driver used to communication with server
  58. #% 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;
  59. #% options:WMS_GDAL, WMS_GRASS, WMTS_GRASS, OnEarth_GRASS
  60. #% answer:WMS_GRASS
  61. #% guisection: Connection
  62. #%end
  63. #%option
  64. #% key: wms_version
  65. #% type:string
  66. #% description: WMS standard version
  67. #% options: 1.1.1,1.3.0
  68. #% answer: 1.1.1
  69. #% guisection: Request
  70. #%end
  71. #%option
  72. #% key: maxcols
  73. #% type:integer
  74. #% description: Maximum columns to request at a time
  75. #% answer:512
  76. #% guisection: Request
  77. #%end
  78. #%option
  79. #% key: maxrows
  80. #% type: integer
  81. #% description: Maximum rows to request at a time
  82. #% answer: 512
  83. #% guisection: Request
  84. #%end
  85. #%option
  86. #% key: urlparams
  87. #% type:string
  88. #% description: Additional query parameters to pass to the server
  89. #% guisection: Request
  90. #%end
  91. #%option
  92. #% key: username
  93. #% type:string
  94. #% description: Username for server connection
  95. #% guisection: Connection
  96. #%end
  97. #%option
  98. #% key: password
  99. #% type:string
  100. #% description: Password for server connection
  101. #% guisection: Connection
  102. #%end
  103. #%option
  104. #% key: method
  105. #% type: string
  106. #% description: Interpolation method to use in reprojection
  107. #% options:nearest,linear,cubic,cubicspline
  108. #% answer:nearest
  109. #% required: no
  110. #%end
  111. #%option
  112. #% key: region
  113. #% type: string
  114. #% description: Request data for this named region instead of the current region bounds
  115. #% guisection: Request
  116. #%end
  117. #%option
  118. #% key: bgcolor
  119. #% type: string
  120. #% label: Background color
  121. #% description: Format: 0xRRGGBB
  122. #% guisection: Map style
  123. #%end
  124. #%option G_OPT_F_BIN_INPUT
  125. #% key: capfile
  126. #% required: no
  127. #% description: Capabilities file to parse (input). It is relevant for WMTS_GRASS and OnEarth_GRASS drivers
  128. #%end
  129. #%option G_OPT_F_OUTPUT
  130. #% key: capfile_output
  131. #% required: no
  132. #% description: File where the server capabilities will be saved ('c' flag)
  133. #%end
  134. #%flag
  135. #% key: c
  136. #% description: Get the server capabilities then exit
  137. #% guisection: Request
  138. #% suppress_required: yes
  139. #%end
  140. #%flag
  141. #% key: o
  142. #% description: Do not request transparent data
  143. #% guisection: Map style
  144. #%end
  145. #%rules
  146. #% exclusive: capfile_output, capfile
  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())