wms_download.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. ############################################################################
  2. #
  3. # MODULE: r.in.wms / wms_download.py
  4. #
  5. # AUTHOR(S): Cedric Shock, 2006
  6. # Updated for GRASS 7 by Martin Landa <landa.martin gmail.com>, 2009
  7. #
  8. # PURPOSE: To import data from web mapping servers
  9. # (based on Bash script by Cedric Shock)
  10. #
  11. # COPYRIGHT: (C) 2009 Martin Landa, and GRASS development team
  12. #
  13. # This program is free software under the GNU General
  14. # Public License (>=v2). Read the file COPYING that
  15. # comes with GRASS for details.
  16. #
  17. #############################################################################
  18. import os
  19. import urllib
  20. from grass.script import core as grass
  21. class WMSDownload:
  22. def __init__(self, flags, options):
  23. self.flags = flags
  24. self.options = options
  25. def GetTiles(self, requests):
  26. grass.message("Downloading tiles...")
  27. i = 0
  28. for item in requests:
  29. if os.path.exists(item['output']) and \
  30. os.path.getsize(item['output']) > 0:
  31. grass.verbose("Tile already downloaded")
  32. else:
  33. self.GetData(i, item['server'], item['string'], item['output'])
  34. i += 1
  35. def GetData(self, idx, server, query, output):
  36. """Download data"""
  37. grass.message("Downloading data (tile %d)..." % idx)
  38. grass.verbose("Requesting data: %s" % self.options['mapserver'])
  39. if not self.flags['g']: # -> post
  40. try:
  41. urllib.urlretrieve(server, output, data = query)
  42. except IOError:
  43. grass.fatal("Failed while downloading the data")
  44. if not os.path.exists(output):
  45. grass.fatal("Failed while downloading the data")
  46. # work-around for brain-dead ArcIMS servers which want POST-data as part of the GET URL
  47. # (this is technically allowed by OGC WMS def v1.3.0 Sec6.3.4)
  48. if os.path.getsize(output) == 0:
  49. grass.warning("Downloaded image file is empty -- trying another method")
  50. self.flags['g'] = True
  51. if self.flags['g']: # -> get
  52. try:
  53. urllib.urlretrieve(server + '?' + query, output, data = None)
  54. except IOError:
  55. grass.fatal("Failed while downloading the data")
  56. if not os.path.exists(output) or os.path.getsize(output) == 0:
  57. grass.fatal("Failed while downloading the data")