From 18b00503e26e51db44a6b3d9b279162f8278b868 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 13 Sep 2009 21:03:20 -0400 Subject: [PATCH] Added the KML module with enough classes to export our block data as KML. --- src/KML.py | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 src/KML.py diff --git a/src/KML.py b/src/KML.py new file mode 100644 index 0000000..890021d --- /dev/null +++ b/src/KML.py @@ -0,0 +1,160 @@ +""" +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 -- 2.44.2