r.in.wms.py 7.3 KB

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