]> gitweb.michael.orlitzky.com - dead/census-tools.git/commitdiff
Merged branch 'master' with the to_kml() fix.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 25 Oct 2009 22:11:24 +0000 (18:11 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 25 Oct 2009 22:11:24 +0000 (18:11 -0400)
1  2 
src/KML.py

diff --combined src/KML.py
index 1f42898d6d4f48070381e93da2113a2b6af422a2,3279c858f29d42c69f7e825529031f89b685f177..26e8e71d551437caa72617c52bdfe7ce41717774
@@@ -32,17 -32,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 += escape(self.text)
+         kml = escape(self.text)
  
          for c in self.children:
              kml += c.to_kml()
-         kml += self.CLOSE_TAG + "\n"
          
          return kml
  
@@@ -75,7 -75,7 +75,7 @@@ class Document(KmlObject)
  <Document>"""
  
      CLOSE_TAG = """</Document>
    </kml>"""
+ </kml>"""
      
      def __init__(self, initial_text=''):
          super(Document, self).__init__(initial_text)
@@@ -110,69 -110,6 +110,69 @@@ class Description(KmlObject)
  
  
  
 +class LineString(KmlObject):
 +
 +    OPEN_TAG = '<LineString>'
 +    CLOSE_TAG = '</LineString>'
 +
 +    @classmethod
 +    def parse_linestrings(self, kml):
 +        """
 +        Parse each <LineString>...</LineString> 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>')
 +            c_start = c_tag_start + len('<coordinates>')
 +            c_end = ls.find('</coordinates>')
 +            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 = '<name>'