wms_download.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. import os
  20. import urllib
  21. import grass
  22. class WMSDownload:
  23. def __init__(self, flags, options):
  24. self.flags = flags
  25. self.options = options
  26. def GetTiles(self, requests):
  27. grass.message("Downloading tiles...")
  28. i = 0
  29. for item in requests:
  30. if os.path.exists(item['output']) and \
  31. os.path.getsize(item['output']) > 0:
  32. grass.message("Tile already downloaded")
  33. else:
  34. self.GetData(i, item['server'] + item['string'], item['output'])
  35. i += 1
  36. def GetData(self, idx, url, output):
  37. """Download data"""
  38. grass.message("Downloading data (tile %d)..." % idx)
  39. grass.verbose("Requesting data: %s" % self.options['mapserver'])
  40. grass.verbose(url)
  41. if not self.flags['g']: # -> post
  42. try:
  43. urllib.urlretrieve(url, output, data="POST")
  44. except IOError:
  45. grass.fatal("Failed while downloading the data")
  46. if not os.path.exists(output):
  47. grass.fatal("Failed while downloading the data")
  48. # work-around for brain-dead ArcIMS servers which want POST-data as part of the GET URL
  49. # (this is technically allowed by OGC WMS def v1.3.0 Sec6.3.4)
  50. if os.path.getsize(output) == 0:
  51. grass.warning("Downloaded image file is empty -- trying another method")
  52. self.flags['g'] = True
  53. if self.flags['g']: # -> get
  54. try:
  55. urllib.urlretrieve(url, output, data="GET")
  56. except IOError:
  57. grass.fatal("Failed while downloading the data")
  58. if not os.path.exists(output) or os.path.getsize(output) == 0:
  59. grass.fatal("Failed while downloading the data")