r.in.wms.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #!/usr/bin/env python3
  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-2021 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 OGC WMS and OGC WMTS web mapping servers.
  12. # % keyword: raster
  13. # % keyword: import
  14. # % keyword: OGC web services
  15. # % keyword: OGC WMS
  16. # % keyword: OGC WMTS
  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,png8
  46. # % answer: png
  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 for communication with the 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.0,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. # % required: no
  112. # %end
  113. # %option
  114. # % key: gdal_createopt
  115. # % type: string
  116. # % required: no
  117. # % multiple: yes
  118. # % label: GDAL creation option(s) to pass to the output format driver
  119. # % description: In the form of "NAME=VALUE", separate multiple entries with a comma
  120. # % guisection: Request
  121. # %end
  122. # %option
  123. # % key: region
  124. # % type: string
  125. # % description: Request data for this named region instead of the current region bounds
  126. # % guisection: Request
  127. # %end
  128. # %option
  129. # % key: bgcolor
  130. # % type: string
  131. # % label: Background color
  132. # % description: Format: 0xRRGGBB
  133. # % guisection: Map style
  134. # %end
  135. # %option
  136. # % key: proxy
  137. # % label: HTTP proxy only GDAL driver (GDAL_HTTP_PROXY)
  138. # % type: string
  139. # % description: HTTP proxy
  140. # %end
  141. # %option
  142. # % key: proxy_user_pw
  143. # % label: User and password for HTTP proxy only for GDAL driver (GDAL_HTTP_PROXYUSERPWD). Must be in the form of [user name]:[password].
  144. # % type: string
  145. # % description: User and password for HTTP proxy
  146. # %end
  147. # %option G_OPT_F_BIN_INPUT
  148. # % key: capfile
  149. # % required: no
  150. # % description: Capabilities file to parse (input). It is relevant for WMTS_GRASS and OnEarth_GRASS drivers
  151. # %end
  152. # %option G_OPT_F_OUTPUT
  153. # % key: capfile_output
  154. # % required: no
  155. # % description: File where the server capabilities will be saved ('c' flag)
  156. # %end
  157. # %flag
  158. # % key: c
  159. # % description: Get the server capabilities, print them out, then exit
  160. # % guisection: Request
  161. # % suppress_required: yes
  162. # %end
  163. # %flag
  164. # % key: o
  165. # % description: Do not request transparent data
  166. # % guisection: Map style
  167. # %end
  168. # %flag
  169. # % key: b
  170. # % description: Keep original bands (default: create composite)
  171. # % guisection: Map style
  172. # %end
  173. # %rules
  174. # % exclusive: capfile_output, capfile
  175. # %end
  176. import os
  177. import sys
  178. import grass.script as grass
  179. from grass.script.utils import decode
  180. def GetRegionParams(opt_region):
  181. # set region
  182. if opt_region:
  183. reg_spl = opt_region.strip().split("@", 1)
  184. reg_mapset = "."
  185. if len(reg_spl) > 1:
  186. reg_mapset = reg_spl[1]
  187. if not grass.find_file(name=reg_spl[0], element="windows", mapset=reg_mapset)[
  188. "name"
  189. ]:
  190. grass.fatal(_("Region <%s> not found") % opt_region)
  191. if opt_region:
  192. s = grass.read_command("g.region", quiet=True, flags="ug", region=opt_region)
  193. region_params = grass.parse_key_val(decode(s), val_type=float)
  194. else:
  195. region_params = grass.region()
  196. return region_params
  197. def main():
  198. sys.path.insert(1, os.path.join(os.path.dirname(sys.path[0]), "etc", "r.in.wms"))
  199. if "GRASS" in options["driver"]:
  200. grass.debug("Using GRASS driver")
  201. from wms_drv import WMSDrv
  202. wms = WMSDrv()
  203. elif "GDAL" in options["driver"]:
  204. grass.debug("Using GDAL WMS driver")
  205. from wms_gdal_drv import WMSGdalDrv
  206. if options["gdal_createopt"]:
  207. create_options = options["gdal_createopt"].split(",")
  208. else:
  209. create_options = None
  210. wms = WMSGdalDrv(create_options)
  211. if flags["c"]:
  212. wms.GetCapabilities(options)
  213. else:
  214. from wms_base import GRASSImporter
  215. # set proxy
  216. if options["proxy"] and options["proxy_user_pw"]:
  217. wms.setProxy(options["proxy"], options["proxy_user_pw"])
  218. if options["proxy"]:
  219. wms.setProxy(options["proxy"])
  220. if "GRASS" in options["driver"]:
  221. grass.warning(
  222. _(
  223. "The proxy will be ignored by the chosen GRASS driver. It is only used with the GDAL driver."
  224. )
  225. )
  226. options["region"] = GetRegionParams(options["region"])
  227. fetched_map = wms.GetMap(options, flags)
  228. grass.message(_("Importing raster map into GRASS..."))
  229. if not fetched_map:
  230. grass.warning(
  231. _("Nothing to import.\nNo data has been downloaded from wms server.")
  232. )
  233. return
  234. importer = GRASSImporter(options["output"], (not flags["b"]))
  235. importer.ImportMapIntoGRASS(fetched_map)
  236. return 0
  237. if __name__ == "__main__":
  238. options, flags = grass.parser()
  239. sys.exit(main())