r.in.wms.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. wmsPath = os.path.join(os.getenv('GISBASE'), 'etc', 'r.in.wms')
  192. sys.path.append(wmsPath)
  193. print wmsPath
  194. import wms_request
  195. import wms_download
  196. class ProcessCapFile(HandlerBase):
  197. """
  198. A SAX handler for the capabilities file
  199. """
  200. def __init__(self):
  201. self.inTag = {}
  202. for tag in ('layer', 'name', 'style',
  203. 'title', 'srs'):
  204. self.inTag[tag] = False
  205. self.value = ''
  206. self.layers = []
  207. def startElement(self, name, attrs):
  208. if self.inTag.has_key(name.lower()):
  209. self.inTag[name.lower()] = True
  210. if name.lower() == 'layer':
  211. self.layers.append({})
  212. def endElement(self, name):
  213. if self.inTag.has_key(name.lower()):
  214. self.inTag[name.lower()] = False
  215. for tag in ('name', 'title', 'srs'):
  216. if name.lower() != tag:
  217. continue
  218. if self.inTag['style']:
  219. if not self.layers[-1].has_key('style'):
  220. self.layers[-1]['style'] = {}
  221. if not self.layers[-1]['style'].has_key(tag):
  222. self.layers[-1]['style'][tag] = []
  223. self.layers[-1]['style'][tag].append(self.value)
  224. elif self.inTag['layer']:
  225. self.layers[-1][tag] = self.value
  226. if name.lower() in ('name', 'title', 'srs'):
  227. self.value = ''
  228. def characters(self, ch):
  229. if self.inTag['name'] or \
  230. self.inTag['title'] or \
  231. self.inTag['srs']:
  232. self.value += ch
  233. def getLayers(self):
  234. """Print list of layers"""
  235. for ly in self.layers:
  236. print "LAYER: " + ly['name']
  237. if ly.has_key('title'):
  238. print " Title: " + ly['title']
  239. if ly.has_key('srs'):
  240. print " SRS: " + ly['srs']
  241. if ly.has_key('style'):
  242. for idx in range(len(ly['style']['name'])):
  243. print " STYLE: " + ly['style']['name'][idx]
  244. print " Style title: " + ly['style']['title'][idx]
  245. def list_layers():
  246. """Get available layers from WMS server"""
  247. qstring = "service=WMS&request=GetCapabilities&" + options['wmsquery']
  248. grass.debug("POST-data: %s" % qstring)
  249. # download capabilities file
  250. grass.verbose("List of layers for server <%s>:" % options['mapserver'])
  251. cap_file = urllib.urlopen(options['mapserver'] + qstring)
  252. if not cap_file:
  253. grass.fatal("Unable to get capabilities of <%s>" % options['mapserver'])
  254. # parse file with sax
  255. cap_xml = ProcessCapFile()
  256. try:
  257. xml.sax.parse(cap_file, cap_xml)
  258. except xml.sax.SAXParseException, err:
  259. grass.fatal("Reading capabilities failed. "
  260. "Unable to parse XML document: %s" % err)
  261. cap_xml.getLayers()
  262. def main():
  263. if flags['l']:
  264. # list of available layers
  265. list_layers()
  266. return 0
  267. elif not options['output']:
  268. grass.fatal("No output map specified")
  269. # set directory for download
  270. if not options['folder']:
  271. options['folder'] = os.path.join(grass.gisenv()['GISDBASE'], 'wms_download')
  272. # region settings
  273. if options['region']:
  274. if not grass.find_file(name = options['region'], element = 'windows')['name']:
  275. grass.fatal("Region <%s> not found" % options['region'])
  276. if not flags['d']:
  277. # request data first
  278. request = wms_request.WMSRequest(flags, options)
  279. request.GetTiles()
  280. if not request:
  281. grass.fatal("WMS request failed")
  282. # download data
  283. download = wms_download.WMSDownload(flags, options)
  284. download.GetTiles(request.GetRequests()) ## ??
  285. # list of files
  286. files = []
  287. for item in request.GetRequests():
  288. files.append(item['output'])
  289. files = ','.join(files)
  290. gdalwarp.GDALWarp(flags, options)
  291. return 0
  292. if __name__ == "__main__":
  293. options, flags = grass.parser()
  294. sys.exit(main())