d.wms.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 server.
  19. #% keywords: raster
  20. #% keywords: import
  21. #% keywords: wms
  22. #%end
  23. #%option
  24. #% key: url
  25. #% type: string
  26. #% description: URL of WMS server
  27. #% required: yes
  28. #%end
  29. #%option
  30. #% key: layers
  31. #% type: string
  32. #% description: Layers to request from WMS server
  33. #% multiple: yes
  34. #% required: yes
  35. #%end
  36. #%option
  37. #% key: map
  38. #% type: string
  39. #% description: Name for output WMS layer in the layer tree
  40. #% required : yes
  41. #%end
  42. #%option
  43. #% key: srs
  44. #% type: integer
  45. #% description: EPSG number of source projection for request
  46. #% answer:4326
  47. #% guisection: Request properties
  48. #%end
  49. #%option
  50. #% key: wms_version
  51. #% type: string
  52. #% description: WMS standard
  53. #% options: 1.1.1,1.3.0
  54. #% answer: 1.1.1
  55. #% guisection: Request properties
  56. #%end
  57. #%option
  58. #% key: format
  59. #% type: string
  60. #% description: Image format requested from the server
  61. #% options: geotiff,tiff,jpeg,gif,png
  62. #% answer: geotiff
  63. #% guisection: Request properties
  64. #%end
  65. #%option
  66. #% key: method
  67. #% type: string
  68. #% description: Reprojection method to use
  69. #% options:near,bilinear,cubic,cubicspline
  70. #% answer:near
  71. #% guisection: Request properties
  72. #%end
  73. #%option
  74. #% key: maxcols
  75. #% type:integer
  76. #% description: Maximum columns to request at a time
  77. #% answer:400
  78. #% guisection: Request properties
  79. #%end
  80. #%option
  81. #% key: maxrows
  82. #% type: integer
  83. #% description: Maximum rows to request at a time
  84. #% answer: 300
  85. #% guisection: Request properties
  86. #%end
  87. #%option
  88. #% key: urlparams
  89. #% type:string
  90. #% description: Additional query parameters for server
  91. #% guisection: Request properties
  92. #%end
  93. #%option
  94. #% key: username
  95. #% type:string
  96. #% description: Username for server connection
  97. #% guisection: Request properties
  98. #%end
  99. #%option
  100. #% key: password
  101. #% type:string
  102. #% description: Password for server connection
  103. #% guisection: Request properties
  104. #%end
  105. #%option
  106. #% key: styles
  107. #% type: string
  108. #% description: Styles to request from map server
  109. #% multiple: yes
  110. #% guisection: Map style
  111. #%end
  112. #%option
  113. #% key: bgcolor
  114. #% type: string
  115. #% description: Color of map background
  116. #% guisection: Map style
  117. #%end
  118. #%flag
  119. #% key: o
  120. #% description: Don't request transparent data
  121. #% guisection: Map style
  122. #%end
  123. #%option
  124. #% key: driver
  125. #% type:string
  126. #% description: Driver for communication with server
  127. #% 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;
  128. #% options:WMS_GDAL, WMS_GRASS, WMTS_GRASS, OnEarth_GRASS
  129. #% answer:WMS_GRASS
  130. #%end
  131. #%option G_OPT_F_INPUT
  132. #% key: capfile
  133. #% required: no
  134. #% gisprompt: old,file,bin_input
  135. #% description: Capabilities file to load
  136. import os
  137. import sys
  138. from grass.script import core as grass
  139. sys.path.append(os.path.join(os.getenv("GISBASE"), "etc", "r.in.wms"))
  140. def GetRegion():
  141. """!Parse region from GRASS_REGION env var.
  142. """
  143. region = os.environ["GRASS_REGION"]
  144. conv_reg_vals = {'east' : 'e',
  145. 'north' : 'n',
  146. 'west' : 'w',
  147. 'south' : 's',
  148. 'rows' : 'rows',
  149. 'cols' : 'cols',
  150. 'e-w resol' : 'ewres',
  151. 'n-s resol' : 'nsres'}
  152. keys_to_convert = conv_reg_vals.keys()
  153. conv_region = {}
  154. region = region.split(';')
  155. for r in region:
  156. r = r.split(':')
  157. r[0] = r[0].strip()
  158. if r[0] in keys_to_convert:
  159. conv_region[conv_reg_vals[r[0]]] = float(r[1])
  160. return conv_region
  161. def main():
  162. options['region'] = GetRegion()
  163. if 'GRASS' in options['driver']:
  164. grass.debug("Using GRASS driver")
  165. from wms_drv import WMSDrv
  166. wms = WMSDrv()
  167. elif 'GDAL' in options['driver']:
  168. grass.debug("Using GDAL WMS driver")
  169. from wms_gdal_drv import WMSGdalDrv
  170. wms = WMSGdalDrv()
  171. temp_map = wms.GetMap(options, flags)
  172. os.rename(temp_map, os.environ["GRASS_PNGFILE"])
  173. return 0
  174. if __name__ == "__main__":
  175. options, flags = grass.parser()
  176. sys.exit(main())