d.wms.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env python
  2. #
  3. ############################################################################
  4. #
  5. # MODULE: d.wms
  6. #
  7. # AUTHOR(S): Stepan Turek <stepan.turek seznam.cz> (mentor: Martin Landa)
  8. #
  9. # PURPOSE: Wrapper for wxGUI to add WMS into layer tree
  10. #
  11. # COPYRIGHT: (C) 2012 by Stepan Turek, and the GRASS Development Team
  12. #
  13. # This program is free software under the GNU General Public License
  14. # (>=v2). Read the file COPYING that comes with GRASS for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Downloads and displays data from WMS/WMTS/NASA OnEarth server.
  19. #% keyword: raster
  20. #% keyword: import
  21. #% keyword: WMS
  22. #% keyword: WMTS
  23. #% keyword: OnEarth
  24. #%end
  25. #%option
  26. #% key: url
  27. #% type: string
  28. #% description: Typically starts with "http://"
  29. #% required: yes
  30. #%end
  31. #%option
  32. #% key: map
  33. #% type: string
  34. #% description: Name for output WMS layer in the layer tree
  35. #% required : yes
  36. #%end
  37. #%option
  38. #% key: layers
  39. #% type: string
  40. #% description: Layer(s) to request from the map server
  41. #% multiple: yes
  42. #% required: yes
  43. #%end
  44. #%option
  45. #% key: styles
  46. #% type: string
  47. #% description: Layer style(s) to request from the map server
  48. #% multiple: yes
  49. #% guisection: Map style
  50. #%end
  51. #%option
  52. #% key: format
  53. #% type: string
  54. #% description: Image format requested from the server
  55. #% options: geotiff,tiff,jpeg,gif,png,png8
  56. #% answer: png
  57. #% guisection: Request
  58. #%end
  59. #%option
  60. #% key: srs
  61. #% type: integer
  62. #% description: EPSG code of requested source projection
  63. #% answer:4326
  64. #% guisection: Request
  65. #%end
  66. #%option
  67. #% key: driver
  68. #% type:string
  69. #% description: Driver used to communication with server
  70. #% 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;
  71. #% options:WMS_GDAL, WMS_GRASS, WMTS_GRASS, OnEarth_GRASS
  72. #% answer:WMS_GRASS
  73. #% guisection: Connection
  74. #%end
  75. #%option
  76. #% key: wms_version
  77. #% type:string
  78. #% description: WMS standard version
  79. #% options: 1.1.1,1.3.0
  80. #% answer: 1.1.1
  81. #% guisection: Request
  82. #%end
  83. #%option
  84. #% key: maxcols
  85. #% type:integer
  86. #% description: Maximum columns to request at a time
  87. #% answer:512
  88. #% guisection: Request
  89. #%end
  90. #%option
  91. #% key: maxrows
  92. #% type: integer
  93. #% description: Maximum rows to request at a time
  94. #% answer: 512
  95. #% guisection: Request
  96. #%end
  97. #%option
  98. #% key: urlparams
  99. #% type:string
  100. #% description: Additional query parameters to pass to the server
  101. #% guisection: Request
  102. #%end
  103. #%option
  104. #% key: username
  105. #% type:string
  106. #% description: Username for server connection
  107. #% guisection: Connection
  108. #%end
  109. #%option
  110. #% key: password
  111. #% type:string
  112. #% description: Password for server connection
  113. #% guisection: Connection
  114. #%end
  115. #%option
  116. #% key: method
  117. #% type: string
  118. #% description: Interpolation method to use in reprojection
  119. #% options:nearest,linear,cubic,cubicspline
  120. #% answer:nearest
  121. #% required: no
  122. #%end
  123. #%option
  124. #% key: bgcolor
  125. #% type: string
  126. #% description: Background color
  127. #% guisection: Map style
  128. #%end
  129. #%option G_OPT_F_BIN_INPUT
  130. #% key: capfile
  131. #% required: no
  132. #% description: Capabilities file to parse (input). It is relevant for WMTS_GRASS and OnEarth_GRASS drivers
  133. #%end
  134. #%flag
  135. #% key: o
  136. #% description: Don't request transparent data
  137. #% guisection: Map style
  138. #%end
  139. import os
  140. import sys
  141. import shutil
  142. from grass.script import core as grass
  143. sys.path.append(os.path.join(os.getenv("GISBASE"), "etc", "r.in.wms"))
  144. def GetRegion():
  145. """!Parse region from GRASS_REGION env var.
  146. """
  147. region = os.environ["GRASS_REGION"]
  148. conv_reg_vals = {'east': 'e',
  149. 'north': 'n',
  150. 'west': 'w',
  151. 'south': 's',
  152. 'rows': 'rows',
  153. 'cols': 'cols',
  154. 'e-w resol': 'ewres',
  155. 'n-s resol': 'nsres'}
  156. keys_to_convert = conv_reg_vals.keys()
  157. conv_region = {}
  158. region = region.split(';')
  159. for r in region:
  160. r = r.split(':')
  161. r[0] = r[0].strip()
  162. if r[0] in keys_to_convert:
  163. conv_region[conv_reg_vals[r[0]]] = float(r[1])
  164. return conv_region
  165. def main():
  166. options['region'] = GetRegion()
  167. if 'GRASS' in options['driver']:
  168. grass.debug("Using GRASS driver")
  169. from wms_drv import WMSDrv
  170. wms = WMSDrv()
  171. elif 'GDAL' in options['driver']:
  172. grass.debug("Using GDAL WMS driver")
  173. from wms_gdal_drv import WMSGdalDrv
  174. wms = WMSGdalDrv()
  175. temp_map = wms.GetMap(options, flags)
  176. shutil.move(temp_map, os.environ["GRASS_RENDER_FILE"])
  177. return 0
  178. if __name__ == "__main__":
  179. options, flags = grass.parser()
  180. sys.exit(main())