r.in.wms.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.in.wms
  5. #
  6. # AUTHOR(S): Cedric Shock, 2006
  7. # Pythonized 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: wgetoptions
  151. #% type: string
  152. #% description: Additional options for wget
  153. #% answer: -c -t 5 -nv
  154. #% required : no
  155. #% guisection: Download
  156. #%end
  157. #%option
  158. #% key: curloptions
  159. #% type: string
  160. #% description: Additional options for curl
  161. #% answer: -C - --retry 5 -s -S
  162. #% required : no
  163. #% guisection: Download
  164. #%end
  165. #%option
  166. #% key: method
  167. #% type: string
  168. #% description: Reprojection method to use
  169. #% options:nearest,bilinear,cubic,cubicspline
  170. #% answer:nearest
  171. #% required: yes
  172. #% guisection: Import
  173. #%end
  174. #%option
  175. #% key: cap_file
  176. #% type: string
  177. #% label: Filename to save capabilities XML file to
  178. #% description: Requires list available layers flag
  179. #% required: no
  180. #% guisection: Request
  181. #%end
  182. import os
  183. import sys
  184. import tempfile
  185. import urllib
  186. import xml.sax
  187. import xml.sax.handler
  188. HandlerBase=xml.sax.handler.ContentHandler
  189. from xml.sax import make_parser
  190. import grass
  191. import wms_request
  192. import wms_download
  193. class ProcessCapFile(HandlerBase):
  194. """
  195. A SAX handler for the capabilities file
  196. """
  197. def __init__(self):
  198. self.inTag = {}
  199. for tag in ('layer', 'name', 'style',
  200. 'title', 'srs'):
  201. self.inTag[tag] = False
  202. self.value = ''
  203. self.layers = []
  204. def startElement(self, name, attrs):
  205. if self.inTag.has_key(name.lower()):
  206. self.inTag[name.lower()] = True
  207. if name.lower() == 'layer':
  208. self.layers.append({})
  209. def endElement(self, name):
  210. if self.inTag.has_key(name.lower()):
  211. self.inTag[name.lower()] = False
  212. for tag in ('name', 'title', 'srs'):
  213. if name.lower() != tag:
  214. continue
  215. if self.inTag['style']:
  216. if not self.layers[-1].has_key('style'):
  217. self.layers[-1]['style'] = {}
  218. if not self.layers[-1]['style'].has_key(tag):
  219. self.layers[-1]['style'][tag] = []
  220. self.layers[-1]['style'][tag].append(self.value)
  221. elif self.inTag['layer']:
  222. self.layers[-1][tag] = self.value
  223. if name.lower() in ('name', 'title', 'srs'):
  224. self.value = ''
  225. def characters(self, ch):
  226. if self.inTag['name'] or \
  227. self.inTag['title'] or \
  228. self.inTag['srs']:
  229. self.value += ch
  230. def getLayers(self):
  231. """Print list of layers"""
  232. for ly in self.layers:
  233. print "LAYER: " + ly['name']
  234. if ly.has_key('title'):
  235. print " Title: " + ly['title']
  236. if ly.has_key('srs'):
  237. print " SRS: " + ly['srs']
  238. if ly.has_key('style'):
  239. for idx in range(len(ly['style']['name'])):
  240. print " STYLE: " + ly['style']['name'][idx]
  241. print " Style title: " + ly['style']['title'][idx]
  242. def list_layers():
  243. """Get available layers from WMS server"""
  244. qstring = "service=WMS&request=GetCapabilities&" + options['wmsquery']
  245. grass.debug("POST-data: %s" % qstring)
  246. # download capabilities file
  247. grass.verbose("List of layers for server <%s>:" % options['mapserver'])
  248. cap_file = urllib.urlopen(options['mapserver'] + qstring)
  249. if not cap_file:
  250. grass.fatal("Unable to get capabilities of <%s>" % options['mapserver'])
  251. # parse file with sax
  252. cap_xml = ProcessCapFile()
  253. try:
  254. xml.sax.parse(cap_file, cap_xml)
  255. except xml.sax.SAXParseException, err:
  256. grass.fatal("Reading capabilities failed. "
  257. "Unable to parse XML document: %s" % err)
  258. cap_xml.getLayers()
  259. def main():
  260. if flags['l']:
  261. # list of available layers
  262. list_layers()
  263. return 0
  264. elif not options['output']:
  265. grass.fatal("No output map specified")
  266. # set directory for download
  267. if not options['folder']:
  268. options['folder'] = os.path.join(grass.gisenv()['GISDBASE'], 'wms_download')
  269. # region settings
  270. if options['region']:
  271. if not grass.find_file(name = options['region'], element = 'windows')['name']:
  272. grass.fatal("Region <%s> not found" % options['region'])
  273. if not flags['d']:
  274. # request data first
  275. request = wms_request.WMSRequest(flags, options)
  276. request.GetTiles()
  277. if not request:
  278. grass.fatal("WMS request failed")
  279. # download data
  280. download = wms_download.WMSDownload(flags, options)
  281. download.GetTiles(request.GetRequests()) ## ??
  282. # list of files
  283. files = []
  284. for item in request.GetRequests():
  285. files.append(item['output'])
  286. files = ','.join(files)
  287. gdalwarp.GDALWarp(flags, options)
  288. return 0
  289. if __name__ == "__main__":
  290. options, flags = grass.parser()
  291. sys.exit(main())