r.in.wms.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.in.wms
  5. #
  6. # AUTHOR(S): Cedric Shock, 2006
  7. # Upgraded for GRASS 7 by Martin Landa <landa.martin gmail.com>, 2009
  8. #
  9. # PURPOSE: To import data from web mapping servers
  10. # (based on Bash script by Cedric Shock)
  11. #
  12. # COPYRIGHT: (C) 2009 Martin Landa, and GRASS development team
  13. #
  14. # This program is free software under the GNU General
  15. # Public License (>=v2). Read the file COPYING that
  16. # comes with GRASS for details.
  17. #
  18. #############################################################################
  19. #%module
  20. #% description: Downloads and imports data from WMS servers.
  21. #% keywords: raster, import, wms
  22. #%end
  23. #%flag
  24. #% key: l
  25. #% description: List available layers and exit
  26. #% guisection: Request
  27. #%end
  28. #%flag
  29. #% key: d
  30. #% description: Skip to downloading (to resume downloads faster)
  31. #% guisection: Download
  32. #%end
  33. #%flag
  34. #% key: o
  35. #% description: Don't request transparent data
  36. #% guisection: Request
  37. #%end
  38. #%flag
  39. #% key: c
  40. #% description: Clean existing data out of download directory
  41. #% guisection: Download
  42. #%end
  43. #%flag
  44. #% key: k
  45. #% description: Keep band numbers instead of using band color names
  46. #% guisection: Import
  47. #%end
  48. #%flag
  49. #% key: p
  50. #% description: Don't reproject the data, just patch it
  51. #% guisection: Import
  52. #%end
  53. #%flag
  54. #% key: g
  55. #% label: Use GET method instead of POST data method
  56. #% description: This may be needed to connect to servers which lack POST capability
  57. #% guisection: Request
  58. #%end
  59. #%option
  60. #% key: output
  61. #% type: string
  62. #% description: Name for output raster map
  63. #% gisprompt: new,cell,raster
  64. #% required : no
  65. #% guisection: Import
  66. #%end
  67. #%option
  68. #% key: mapserver
  69. #% type: string
  70. #% description: Mapserver to request data from
  71. #% required: yes
  72. #% guisection: Request
  73. #%end
  74. #%option
  75. #% key: layers
  76. #% type: string
  77. #% description: Layers to request from map server
  78. #% multiple: yes
  79. #% required: no
  80. #% guisection: Request
  81. #%end
  82. #%option
  83. #% key: styles
  84. #% type: string
  85. #% description: Styles to request from map server
  86. #% multiple: yes
  87. #% required: no
  88. #% guisection: Request
  89. #%end
  90. #%option
  91. #% key: srs
  92. #% type: string
  93. #% description: Source projection to request from server
  94. #% answer:EPSG:4326
  95. #% guisection: Request
  96. #%end
  97. #%option
  98. #% key: format
  99. #% type: string
  100. #% description: Image format requested from the server
  101. #% options: geotiff,tiff,jpeg,gif,png
  102. #% answer: geotiff
  103. #% required: yes
  104. #% guisection: Request
  105. #%end
  106. #%option
  107. #% key: wmsquery
  108. #% type:string
  109. #% description: Addition query options for server
  110. #% answer: version=1.1.1
  111. #% guisection: Request
  112. #%end
  113. #%option
  114. #% key: maxcols
  115. #% type: integer
  116. #% description: Maximum columns to request at a time
  117. #% answer: 1024
  118. #% required : yes
  119. #% guisection: Request
  120. #%end
  121. #%option
  122. #% key: maxrows
  123. #% type: integer
  124. #% description: Maximum rows to request at a time
  125. #% answer: 1024
  126. #% required : yes
  127. #% guisection: Request
  128. #%end
  129. #%option
  130. #% key: tileoptions
  131. #% type: string
  132. #% description: Additional options for r.tileset
  133. #% required : no
  134. #%end
  135. #%option
  136. #% key: region
  137. #% type: string
  138. #% description: Named region to request data for. Current region used if omitted
  139. #% required : no
  140. #% guisection: Request
  141. #%end
  142. #%option
  143. #% key: folder
  144. #% type: string
  145. #% description: Folder to save downloaded data to (default $GISDBASE/wms_download)
  146. #% required : no
  147. #% guisection: Download
  148. #%end
  149. #%option
  150. #% key: method
  151. #% type: string
  152. #% description: Reprojection method to use
  153. #% options:near,bilinear,cubic,cubicspline
  154. #% answer:near
  155. #% required: yes
  156. #% guisection: Import
  157. #%end
  158. #%option
  159. #% key: cap_file
  160. #% type: string
  161. #% label: Filename to save capabilities XML file to
  162. #% description: Requires list available layers flag
  163. #% required: no
  164. #% guisection: Request
  165. #%end
  166. import os
  167. import sys
  168. import tempfile
  169. import urllib
  170. import xml.sax
  171. import grass
  172. wmsPath = os.path.join(os.getenv('GISBASE'), 'etc', 'r.in.wms')
  173. sys.path.append(wmsPath)
  174. try:
  175. import wms_parse
  176. import wms_request
  177. import wms_download
  178. import gdalwarp
  179. except ImportError:
  180. pass
  181. def list_layers():
  182. """Get list of available layers from WMS server"""
  183. qstring = "service=WMS&request=GetCapabilities&" + options['wmsquery']
  184. grass.debug("POST-data: %s" % qstring)
  185. # download capabilities file
  186. grass.verbose("List of layers for server <%s>:" % options['mapserver'])
  187. url = options['mapserver'] + '?' + qstring
  188. try:
  189. if options['cap_file']:
  190. cap_file, headers = urllib.urlretrieve(url, options['cap_file'])
  191. else:
  192. cap_file = urllib.urlopen(url, options['mapserver'] + '?' + qstring)
  193. except IOError:
  194. grass.fatal("Unable to get capabilities of '%s'" % options['mapserver'])
  195. # check DOCTYPE first
  196. if options['cap_file']:
  197. if headers['content-type'] != 'application/vnd.ogc.wms_xml':
  198. grass.fatal("Unable to get capabilities: %s" % url)
  199. else:
  200. if cap_file.info()['content-type'] != 'application/vnd.ogc.wms_xml':
  201. grass.fatal("Unable to get capabilities: %s" % url)
  202. # parse file with sax
  203. cap_xml = wms_parse.ProcessCapFile()
  204. try:
  205. xml.sax.parse(cap_file, cap_xml)
  206. except xml.sax.SAXParseException, err:
  207. grass.fatal("Reading capabilities failed. "
  208. "Unable to parse XML document: %s" % err)
  209. cap_xml.getLayers()
  210. def main():
  211. if flags['l']:
  212. # list of available layers
  213. list_layers()
  214. return 0
  215. elif not options['output']:
  216. grass.fatal("No output map specified")
  217. if options['cap_file'] and not flags['l']:
  218. grass.warning("Option <cap_file> ignored. It requires '-l' flag.")
  219. # set directory for download
  220. if not options['folder']:
  221. options['folder'] = os.path.join(grass.gisenv()['GISDBASE'], 'wms_download')
  222. # region settings
  223. if options['region']:
  224. if not grass.find_file(name = options['region'], element = 'windows')['name']:
  225. grass.fatal("Region <%s> not found" % options['region'])
  226. request = wms_request.WMSRequest(flags, options)
  227. if not flags['d']:
  228. # request data first
  229. request.GetTiles()
  230. if not request:
  231. grass.fatal("WMS request failed")
  232. # download data
  233. download = wms_download.WMSDownload(flags, options)
  234. download.GetTiles(request.GetRequests())
  235. # list of files
  236. files = []
  237. for item in request.GetRequests():
  238. files.append(item['output'])
  239. files = ','.join(files)
  240. # add flags for r.in.gdalwarp
  241. options['input'] = files
  242. flags['e'] = False
  243. flags['c'] = True
  244. options['warpoptions'] = ''
  245. return gdalwarp.GDALWarp(flags, options).run()
  246. if __name__ == "__main__":
  247. options, flags = grass.parser()
  248. sys.exit(main())