d.wms.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env python3
  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. region = os.environ["GRASS_REGION"]
  147. conv_reg_vals = {
  148. "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. }
  157. keys_to_convert = conv_reg_vals.keys()
  158. conv_region = {}
  159. region = region.split(";")
  160. for r in region:
  161. r = r.split(":")
  162. r[0] = r[0].strip()
  163. if r[0] in keys_to_convert:
  164. conv_region[conv_reg_vals[r[0]]] = float(r[1])
  165. return conv_region
  166. def main():
  167. options["region"] = GetRegion()
  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. temp_map = wms.GetMap(options, flags)
  177. shutil.move(temp_map, os.environ["GRASS_RENDER_FILE"])
  178. return 0
  179. if __name__ == "__main__":
  180. options, flags = grass.parser()
  181. sys.exit(main())