--- /dev/null
+"""
+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.
+ """
+
+ def __init__(self, initial_text=''):
+ self.children = []
+ self.text = initial_text
+
+
+ def to_kml(self):
+ return self.render()
+
+
+ def render(self):
+ kml = self.text
+
+ for c in self.children:
+ kml += c.to_kml()
+
+ return kml
+
+
+
+class Color(KmlObject):
+
+ def to_kml(self):
+ kml = '<color>'
+ kml += self.render()
+ kml += "</color>\n"
+ return kml
+
+
+
+class Document(KmlObject):
+
+ def __init__(self, initial_text=''):
+ super(Document, self).__init__(initial_text)
+ self.styles = []
+
+
+ def to_kml(self):
+ kml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ kml += "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
+ kml += "<Document>\n"
+
+ kml += self.render()
+
+ kml += "</Document>\n"
+ kml += "</kml>\n"
+
+ return kml
+
+
+ def render(self):
+ kml = ''
+
+ for s in self.styles:
+ kml += s.to_kml()
+
+ for c in self.children:
+ kml += c.to_kml()
+
+ return kml
+
+
+
+class Description(KmlObject):
+
+ def to_kml(self):
+ kml = '<description>'
+ kml += self.render()
+ kml += "</description>\n"
+ return kml
+
+
+
+class Name(KmlObject):
+
+ def to_kml(self):
+ kml = '<name>'
+ kml += self.render()
+ kml += "</name>\n"
+ return kml
+
+
+
+class Placemark(KmlObject):
+
+ def to_kml(self):
+ kml = "<Placemark>\n"
+ kml += self.render()
+ kml += "</Placemark>\n"
+ return kml
+
+
+
+class PolyStyle(KmlObject):
+
+ def to_kml(self):
+ kml = "<PolyStyle>\n"
+ kml += self.render()
+ kml += "</PolyStyle>\n"
+ return kml
+
+
+
+class Style(KmlObject):
+
+ def __init__(self, initial_text='', initial_id=None):
+ super(Style, self).__init__(initial_text)
+ self.id = initial_id
+
+
+ def to_kml(self):
+ kml = ''
+
+ if (self.id == None):
+ kml += "<Style>\n"
+ else:
+ kml += ("<Style id=\"%s\">\n" % self.id)
+
+ kml += self.render()
+ kml += "</Style>\n"
+
+ return kml
+
+
+
+class StyleUrl(KmlObject):
+
+ def to_kml(self):
+ kml = '<styleUrl>'
+ kml += self.render()
+ kml += "</styleUrl>\n"
+ return kml
+
+
+
+class RawText(KmlObject):
+ pass