From: Michael Orlitzky Date: Mon, 19 Oct 2009 14:17:02 +0000 (-0400) Subject: Updated the KML classes with a print_kml() method. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fcensus-tools.git;a=commitdiff_plain;h=27a538e5ff5530d0db3504d5edfcbbaa5e9b7798 Updated the KML classes with a print_kml() method. Abstracted the to_kml() and print_kml() methods out in to the KmlObject class for most subclasses. Modified pg2kml to use the print_kml() method instead of to_kml(). --- diff --git a/bin/pg2kml b/bin/pg2kml index 68d5e6f..81afeca 100755 --- a/bin/pg2kml +++ b/bin/pg2kml @@ -170,6 +170,6 @@ for row in rows: doc.children.append(placemark) -print doc.to_kml() +doc.print_kml() conn.close() diff --git a/src/KML.py b/src/KML.py index 890021d..4b6d12f 100644 --- a/src/KML.py +++ b/src/KML.py @@ -21,6 +21,9 @@ class KmlObject(object): 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 = [] @@ -32,45 +35,52 @@ class KmlObject(object): def render(self): - kml = self.text + 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): - def to_kml(self): - kml = '' - kml += self.render() - kml += "\n" - return kml + 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 to_kml(self): - kml = "\n" - kml += "\n" - kml += "\n" - - kml += self.render() - - kml += "\n" - kml += "\n" - - return kml - - def render(self): kml = '' @@ -83,78 +93,75 @@ class Document(KmlObject): 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): - def to_kml(self): - kml = '' - kml += self.render() - kml += "\n" - return kml - + OPEN_TAG = '' + CLOSE_TAG = '' + class Name(KmlObject): - def to_kml(self): - kml = '' - kml += self.render() - kml += "\n" - return kml - + OPEN_TAG = '' + CLOSE_TAG = '' + class Placemark(KmlObject): - - def to_kml(self): - kml = "\n" - kml += self.render() - kml += "\n" - return kml - - + + OPEN_TAG = '' + CLOSE_TAG = '' + + class PolyStyle(KmlObject): - - def to_kml(self): - kml = "\n" - kml += self.render() - kml += "\n" - return kml - + + OPEN_TAG = '' + CLOSE_TAG = '' + class Style(KmlObject): - - def __init__(self, initial_text='', initial_id=None): + + OPEN_TAG = "' + + def __init__(self, initial_text='', initial_id=''): super(Style, self).__init__(initial_text) self.id = initial_id def to_kml(self): kml = '' - - if (self.id == None): - kml += "\n" + 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): - - def to_kml(self): - kml = '' - kml += self.render() - kml += "\n" - return kml + +class StyleUrl(KmlObject): + + OPEN_TAG = '' + CLOSE_TAG = '' + class RawText(KmlObject): pass