]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Updated the KML classes with a print_kml() method.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 19 Oct 2009 14:17:02 +0000 (10:17 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 19 Oct 2009 14:17:02 +0000 (10:17 -0400)
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().

bin/pg2kml
src/KML.py

index 68d5e6fafb27c9a008a26e65dd88089510906fd4..81afecaa63bc98a7fbed36739360dfd86b7f4628 100755 (executable)
@@ -170,6 +170,6 @@ for row in rows:
     
     doc.children.append(placemark)
 
-print doc.to_kml()
+doc.print_kml()
 
 conn.close()
index 890021da4167d13b0e9f7f56ae984a3c76f50f51..4b6d12f3fdcfcb47eace8548ea3566b1d7e7b999 100644 (file)
@@ -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 = '<color>'
-        kml += self.render()
-        kml += "</color>\n"
-        return kml
+    OPEN_TAG = '<color>'
+    CLOSE_TAG = '</color>'
 
-    
         
 class Document(KmlObject):
+
+    OPEN_TAG = """<?xml version=\"1.0\" encoding=\"UTF-8\"?>
+<kml xmlns=\"http://www.opengis.net/kml/2.2\">
+<Document>"""
+
+    CLOSE_TAG = """</Document>
+    </kml>"""
     
     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 = ''
         
@@ -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 = '<description>'
-        kml += self.render()
-        kml += "</description>\n"
-        return kml
-    
+    OPEN_TAG = '<description>'
+    CLOSE_TAG = '</description>'
+
 
 
 class Name(KmlObject):
     
-    def to_kml(self):
-        kml = '<name>'
-        kml += self.render()
-        kml += "</name>\n"
-        return kml
-
+    OPEN_TAG = '<name>'
+    CLOSE_TAG = '</name>'
+    
 
 
 class Placemark(KmlObject):
-
-    def to_kml(self):
-        kml = "<Placemark>\n"
-        kml += self.render()
-        kml += "</Placemark>\n"
-        return kml
-
-
+    
+    OPEN_TAG = '<Placemark>'
+    CLOSE_TAG = '</Placemark>'
+    
+    
 
 class PolyStyle(KmlObject):
-
-    def to_kml(self):
-        kml = "<PolyStyle>\n"
-        kml += self.render()
-        kml += "</PolyStyle>\n"
-        return kml
-
+    
+    OPEN_TAG = '<PolyStyle>'
+    CLOSE_TAG = '</PolyStyle>'
+    
 
         
 class Style(KmlObject):
-
-    def __init__(self, initial_text='', initial_id=None):
+    
+    OPEN_TAG = "<Style id=\"%s\">"
+    CLOSE_TAG = '</Style>'
+    
+    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 += "<Style>\n"
-        else:
-            kml += ("<Style id=\"%s\">\n" % self.id)
-
+        kml += (self.OPEN_TAG % self.id + "\n")
         kml += self.render()
-        kml += "</Style>\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 = '<styleUrl>'
-        kml += self.render()
-        kml += "</styleUrl>\n"
-        return kml
+    
 
+class StyleUrl(KmlObject):
+    
+    OPEN_TAG = '<styleUrl>'
+    CLOSE_TAG = '</styleUrl>'
 
+    
         
 class RawText(KmlObject):
     pass