""" Utility classes for working with the Keyhole Markup Language (KML). """ import sys class KmlObject(object): """ The base class of all KML elements, according to the reference: http://code.google.com/apis/kml/documentation/kmlreference.html Every other class in this module should derive from KmlObject. This class provides a default constructor which creates some necessary attributes, and default implementations of to_kml() and render(). The to_kml() methods of our subclasses will, in general, generate an opening tag, render themselves (whatever that means), and then generate a closing tag. A call to render() generally returns the element's text, and renders its children recursively. """ OPEN_TAG = '' CLOSE_TAG = '' def __init__(self, initial_text=''): self.children = [] self.text = initial_text def to_kml(self): return self.render() def render(self): kml = self.OPEN_TAG kml += self.text for c in self.children: kml += c.to_kml() kml += self.CLOSE_TAG + "\n" return kml def print_kml(self): print self.OPEN_TAG self.render_to_stdout() print self.CLOSE_TAG def render_to_stdout(self): if not (len(self.text) == 0): print self.text for c in self.children: c.print_kml() class Color(KmlObject): OPEN_TAG = '' CLOSE_TAG = '' class Document(KmlObject): OPEN_TAG = """ """ CLOSE_TAG = """ """ def __init__(self, initial_text=''): super(Document, self).__init__(initial_text) self.styles = [] def render(self): kml = '' for s in self.styles: kml += s.to_kml() for c in self.children: kml += c.to_kml() return kml def render_to_stdout(self): for s in self.styles: s.print_kml() for c in self.children: c.print_kml() class Description(KmlObject): OPEN_TAG = '' CLOSE_TAG = '' class Name(KmlObject): OPEN_TAG = '' CLOSE_TAG = '' class Placemark(KmlObject): OPEN_TAG = '' CLOSE_TAG = '' class PolyStyle(KmlObject): OPEN_TAG = '' CLOSE_TAG = '' class Style(KmlObject): OPEN_TAG = "' def __init__(self, initial_text='', initial_id=''): super(Style, self).__init__(initial_text) self.id = initial_id def to_kml(self): kml = '' kml += (self.OPEN_TAG % self.id + "\n") kml += self.render() kml += self.CLOSE_TAG + "\n" return kml def print_kml(self): print (self.OPEN_TAG % self.id) self.render_to_stdout() print self.CLOSE_TAG class StyleUrl(KmlObject): OPEN_TAG = '' CLOSE_TAG = '' class RawText(KmlObject): pass