r.in.wms.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #%flag
  60. #% key: a
  61. #% description: Use GDAL WMS driver
  62. #% guisection: Request
  63. #%end
  64. #%option
  65. #% key: output
  66. #% type: string
  67. #% description: Name for output raster map
  68. #% gisprompt: new,cell,raster
  69. #% required : no
  70. #% guisection: Import
  71. #%end
  72. #%option
  73. #% key: mapserver
  74. #% type: string
  75. #% description: Mapserver to request data from
  76. #% required: yes
  77. #% guisection: Request
  78. #%end
  79. #%option
  80. #% key: layers
  81. #% type: string
  82. #% description: Layers to request from map server
  83. #% multiple: yes
  84. #% required: no
  85. #% guisection: Request
  86. #%end
  87. #%option
  88. #% key: styles
  89. #% type: string
  90. #% description: Styles to request from map server
  91. #% multiple: yes
  92. #% required: no
  93. #% guisection: Request
  94. #%end
  95. #%option
  96. #% key: srs
  97. #% type: string
  98. #% description: Source projection to request from server
  99. #% answer:EPSG:4326
  100. #% guisection: Request
  101. #%end
  102. #%option
  103. #% key: format
  104. #% type: string
  105. #% description: Image format requested from the server
  106. #% options: geotiff,tiff,jpeg,gif,png
  107. #% answer: geotiff
  108. #% required: yes
  109. #% guisection: Request
  110. #%end
  111. #%option
  112. #% key: wmsquery
  113. #% type:string
  114. #% description: Addition query options for server
  115. #% answer: version=1.1.1
  116. #% guisection: Request
  117. #%end
  118. #%option
  119. #% key: maxcols
  120. #% type: integer
  121. #% description: Maximum columns to request at a time
  122. #% answer: 1024
  123. #% required : yes
  124. #% guisection: Request
  125. #%end
  126. #%option
  127. #% key: maxrows
  128. #% type: integer
  129. #% description: Maximum rows to request at a time
  130. #% answer: 1024
  131. #% required : yes
  132. #% guisection: Request
  133. #%end
  134. #%option
  135. #% key: tileoptions
  136. #% type: string
  137. #% description: Additional options for r.tileset
  138. #% required : no
  139. #%end
  140. #%option
  141. #% key: region
  142. #% type: string
  143. #% description: Named region to request data for. Current region used if omitted
  144. #% required : no
  145. #% guisection: Request
  146. #%end
  147. #%option
  148. #% key: folder
  149. #% type: string
  150. #% description: Folder to save downloaded data to (default $GISDBASE/wms_download)
  151. #% required : no
  152. #% guisection: Download
  153. #%end
  154. #%option
  155. #% key: method
  156. #% type: string
  157. #% description: Reprojection method to use
  158. #% options:near,bilinear,cubic,cubicspline
  159. #% answer:near
  160. #% required: yes
  161. #% guisection: Import
  162. #%end
  163. #%option
  164. #% key: cap_file
  165. #% type: string
  166. #% label: Filename to save capabilities XML file to
  167. #% description: Requires list available layers flag
  168. #% required: no
  169. #% guisection: Request
  170. #%end
  171. import os
  172. import sys
  173. import tempfile
  174. import urllib
  175. import xml.sax
  176. from grass.script import core, raster as grass
  177. wmsPath = os.path.join(os.getenv('GISBASE'), 'etc', 'r.in.wms')
  178. sys.path.append(wmsPath)
  179. try:
  180. import wms_parse
  181. import wms_request
  182. import wms_download
  183. import gdalwarp
  184. import wms_gdal
  185. except ImportError:
  186. pass
  187. def list_layers():
  188. """Get list of available layers from WMS server"""
  189. qstring = "service=WMS&request=GetCapabilities&" + options['wmsquery']
  190. grass.debug("POST-data: %s" % qstring)
  191. # download capabilities file
  192. grass.verbose("List of layers for server <%s>:" % options['mapserver'])
  193. url = options['mapserver'] + '?' + qstring
  194. try:
  195. if options['cap_file']:
  196. cap_file, headers = urllib.urlretrieve(url, options['cap_file'])
  197. else:
  198. cap_file = urllib.urlopen(url, options['mapserver'] + '?' + qstring)
  199. except IOError:
  200. grass.fatal("Unable to get capabilities of '%s'" % options['mapserver'])
  201. # check DOCTYPE first
  202. if options['cap_file']:
  203. if headers['content-type'] != 'application/vnd.ogc.wms_xml':
  204. grass.fatal("Unable to get capabilities: %s" % url)
  205. else:
  206. if cap_file.info()['content-type'] != 'application/vnd.ogc.wms_xml':
  207. grass.fatal("Unable to get capabilities: %s" % url)
  208. # parse file with sax
  209. cap_xml = wms_parse.ProcessCapFile()
  210. try:
  211. xml.sax.parse(cap_file, cap_xml)
  212. except xml.sax.SAXParseException, err:
  213. grass.fatal("Reading capabilities failed. "
  214. "Unable to parse XML document: %s" % err)
  215. cap_xml.getLayers()
  216. def main():
  217. if flags['l']:
  218. # list of available layers
  219. list_layers()
  220. return 0
  221. elif not options['output']:
  222. grass.fatal("No output map specified")
  223. if options['cap_file'] and not flags['l']:
  224. grass.warning("Option <cap_file> ignored. It requires '-l' flag.")
  225. # set directory for download
  226. if not options['folder']:
  227. options['folder'] = os.path.join(grass.gisenv()['GISDBASE'], 'wms_download')
  228. # region settings
  229. if options['region']:
  230. if not grass.find_file(name = options['region'], element = 'windows')['name']:
  231. grass.fatal("Region <%s> not found" % options['region'])
  232. request = wms_request.WMSRequest(flags, options)
  233. if not flags['d']:
  234. # request data first
  235. request.GetTiles()
  236. if not request:
  237. grass.fatal("WMS request failed")
  238. if flags['a']:
  239. # use GDAL WMS driver
  240. ### TODO: use GDAL Python bindings instead
  241. if not wms_gdal.checkGdalWms():
  242. grass.fatal("GDAL WMS driver is not available")
  243. # create local service description XML file
  244. gdalWms = wms_gdal.GdalWms(options, request)
  245. options['input'] = gdalWms.GetFile()
  246. else:
  247. # download data
  248. download = wms_download.WMSDownload(flags, options)
  249. download.GetTiles(request.GetRequests())
  250. # list of files
  251. files = []
  252. for item in request.GetRequests():
  253. files.append(item['output'])
  254. files = ','.join(files)
  255. options['input'] = files
  256. # add flags for r.in.gdalwarp
  257. flags['e'] = False
  258. flags['c'] = True
  259. options['warpoptions'] = ''
  260. return gdalwarp.GDALWarp(flags, options).run()
  261. return 0
  262. if __name__ == "__main__":
  263. options, flags = grass.parser()
  264. sys.exit(main())