From: Michael Orlitzky Date: Sun, 25 Oct 2009 22:11:24 +0000 (-0400) Subject: Merged branch 'master' with the to_kml() fix. X-Git-Url: http://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=2c1d45fac5f57867d6cf7f4e19628a67aae8a055;hp=b2537c605b2ad0e6b04bf1534d569f91f30f1594;p=dead%2Fcensus-tools.git Merged branch 'master' with the to_kml() fix. --- diff --git a/src/Geometry.py b/src/Geometry.py index 6c039a8..2bee60a 100644 --- a/src/Geometry.py +++ b/src/Geometry.py @@ -242,8 +242,9 @@ class Polygon: # Ex: POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2)) if (text.lower().find('polygon') == -1): return None - - return shapely.wkt.loads(text) + + sp = shapely.wkt.loads(text) + return Polygon.from_shapely(sp) def drag_rectangle(self, v): diff --git a/src/KML.py b/src/KML.py index 3279c85..26e8e71 100644 --- a/src/KML.py +++ b/src/KML.py @@ -110,6 +110,69 @@ class Description(KmlObject): +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('') + c_start = c_tag_start + len('') + c_end = ls.find('') + 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 = ''