wms_parse.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ############################################################################
  2. #
  3. # MODULE: r.in.wms / wms_parse
  4. #
  5. # AUTHOR(S): Cedric Shock, 2006
  6. # Upgraded 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 xml.sax
  19. import xml.sax.handler
  20. HandlerBase=xml.sax.handler.ContentHandler
  21. from xml.sax import make_parser
  22. class ProcessCapFile(HandlerBase):
  23. """
  24. A SAX handler for the capabilities file
  25. """
  26. def __init__(self):
  27. self.inTag = {}
  28. for tag in ('layer', 'name', 'style',
  29. 'title', 'srs'):
  30. self.inTag[tag] = False
  31. self.value = ''
  32. self.layers = []
  33. def startElement(self, name, attrs):
  34. if self.inTag.has_key(name.lower()):
  35. self.inTag[name.lower()] = True
  36. if name.lower() == 'layer':
  37. self.layers.append({})
  38. def endElement(self, name):
  39. if self.inTag.has_key(name.lower()):
  40. self.inTag[name.lower()] = False
  41. for tag in ('name', 'title', 'srs'):
  42. if name.lower() != tag:
  43. continue
  44. if self.inTag['style']:
  45. if not self.layers[-1].has_key('style'):
  46. self.layers[-1]['style'] = {}
  47. if not self.layers[-1]['style'].has_key(tag):
  48. self.layers[-1]['style'][tag] = []
  49. self.layers[-1]['style'][tag].append(self.value)
  50. elif self.inTag['layer']:
  51. self.layers[-1][tag] = self.value
  52. if name.lower() in ('name', 'title', 'srs'):
  53. self.value = ''
  54. def characters(self, ch):
  55. if self.inTag['name'] or \
  56. self.inTag['title'] or \
  57. self.inTag['srs']:
  58. self.value += ch
  59. def getLayers(self):
  60. """Print list of layers"""
  61. for ly in self.layers:
  62. if ly.has_key('name'):
  63. print "LAYER: " + ly['name']
  64. else:
  65. print "LAYER: unknown"
  66. if ly.has_key('title'):
  67. print " Title: " + ly['title']
  68. if ly.has_key('srs'):
  69. print " SRS: " + ly['srs']
  70. if ly.has_key('style'):
  71. for idx in range(len(ly['style']['name'])):
  72. print " STYLE: " + ly['style']['name'][idx]
  73. print " Style title: " + ly['style']['title'][idx]