"""
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 = ''
kml += self.render()
kml += "\n"
return kml
class Document(KmlObject):
def __init__(self, initial_text=''):
super(Document, self).__init__(initial_text)
self.styles = []
def to_kml(self):
kml = "\n"
kml += "\n"
kml += "\n"
kml += self.render()
kml += "\n"
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 = ''
kml += self.render()
kml += "\n"
return kml
class Name(KmlObject):
def to_kml(self):
kml = ''
kml += self.render()
kml += "\n"
return kml
class Placemark(KmlObject):
def to_kml(self):
kml = "\n"
kml += self.render()
kml += "\n"
return kml
class PolyStyle(KmlObject):
def to_kml(self):
kml = "\n"
kml += self.render()
kml += "\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 += "\n"
return kml
class StyleUrl(KmlObject):
def to_kml(self):
kml = ''
kml += self.render()
kml += "\n"
return kml
class RawText(KmlObject):
pass