X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FKML.py;h=9b589ad582c0ac4f11c16a775222716bf7fa1d2f;hb=9e873928a42a91b468e56bf95217f8504c12dad2;hp=4b6d12f3fdcfcb47eace8548ea3566b1d7e7b999;hpb=27a538e5ff5530d0db3504d5edfcbbaa5e9b7798;p=dead%2Fcensus-tools.git
diff --git a/src/KML.py b/src/KML.py
index 4b6d12f..9b589ad 100644
--- a/src/KML.py
+++ b/src/KML.py
@@ -3,6 +3,7 @@ Utility classes for working with the Keyhole Markup Language (KML).
"""
import sys
+from xml.sax.saxutils import escape
class KmlObject(object):
@@ -31,17 +32,17 @@ class KmlObject(object):
def to_kml(self):
- return self.render()
+ kml = self.OPEN_TAG
+ kml += self.render()
+ kml += self.CLOSE_TAG + "\n"
+ return kml
def render(self):
- kml = self.OPEN_TAG
- kml += self.text
+ kml = escape(self.text)
for c in self.children:
kml += c.to_kml()
-
- kml += self.CLOSE_TAG + "\n"
return kml
@@ -53,8 +54,8 @@ class KmlObject(object):
def render_to_stdout(self):
- if not (len(self.text) == 0):
- print self.text
+ if (len(self.text) > 0):
+ print escape(self.text)
for c in self.children:
c.print_kml()
@@ -74,7 +75,7 @@ class Document(KmlObject):
"""
CLOSE_TAG = """
- """
+"""
def __init__(self, initial_text=''):
super(Document, self).__init__(initial_text)
@@ -109,6 +110,75 @@ class Description(KmlObject):
+class Coordinates(KmlObject):
+
+ OPEN_TAG = ''
+ CLOSE_TAG = ''
+
+
+class LineString(KmlObject):
+
+ OPEN_TAG = ''
+ CLOSE_TAG = ''
+
+ @classmethod
+ def parse_linestrings(self, kml):
+ """
+ Parse each ... block from the KML.
+ """
+ linestrings = []
+
+ search_idx = kml.find(self.OPEN_TAG, 0)
+
+ while (search_idx != -1):
+ # No reason to keep the tag around.
+ ls_start = search_idx + len(self.OPEN_TAG)
+ ls_tag_end = kml.find(self.CLOSE_TAG, ls_start)
+ ls = kml[ ls_start : ls_tag_end ]
+ linestrings.append(ls)
+ search_idx = kml.find(self.OPEN_TAG, (ls_tag_end + len(self.CLOSE_TAG)))
+
+ return linestrings
+
+
+ @classmethod
+ def parse_coordinates_from_linestrings(self, linestrings):
+ coords = []
+
+ for ls in linestrings:
+ c_tag_start = ls.find(Coordinates.OPEN_TAG)
+ c_start = c_tag_start + len(Coordinates.OPEN_TAG)
+ c_end = ls.find(Coordinates.CLOSE_TAG)
+ c = ls[ c_start : c_end ]
+ coords.append(c)
+
+ return coords
+
+
+ @classmethod
+ def tuples_from_kml(self, kml):
+ """
+ Parse one or more linestrings from a KML document.
+ Return a list of tuples.
+ """
+ ls = self.parse_linestrings(kml)
+ cs = self.parse_coordinates_from_linestrings(ls)
+
+ tuples = []
+
+ for c in cs:
+ pointstrings = c.strip().split()
+ for point in pointstrings:
+ components = point.strip().split(',')
+ if (len(components) >= 2):
+ # Project the three-dimensional vector onto the
+ # x-y plane. I don't think we're going to run
+ # in to any linestrings in 3d.
+ tuples.append( (float(components[0]), float(components[1])) )
+
+ return tuples
+
+
class Name(KmlObject):
OPEN_TAG = ''
@@ -164,4 +234,10 @@ class StyleUrl(KmlObject):
class RawText(KmlObject):
- pass
+
+ def to_kml(self):
+ return self.text
+
+ def print_kml(self):
+ if (len(self.text) > 0):
+ print self.text