wms_parse.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. print "\n\n-=-=-=-=-=-\n"
  63. if ly.has_key('name'):
  64. print "LAYER: " + ly['name']
  65. else:
  66. print "LAYER: unknown"
  67. if ly.has_key('title'):
  68. print " Title: " + ly['title']
  69. if ly.has_key('srs'):
  70. print " SRS: " + ly['srs']
  71. if ly.has_key('style'):
  72. for idx in range(len(ly['style']['name'])):
  73. print " STYLE: " + ly['style']['name'][idx]
  74. print " Style title: " + ly['style']['title'][idx]