r.in.wms.py 7.2 KB

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