Commit 4bb70a19 authored by Christian Ledermann's avatar Christian Ledermann

make sure that the output (to_string) can be fed back into from_string and...

make sure that the output (to_string) can be fed back into from_string and produces the same document
parent 43e1d83b
...@@ -23,13 +23,6 @@ class KML(object): ...@@ -23,13 +23,6 @@ class KML(object):
def __init__(self): def __init__(self):
self._features =[] self._features =[]
def from_file(self, filename):
""" read a KML File and parse into a KML Object"""
f = open(filename, 'r')
self.from_string(f.read())
f.close()
def from_string(self, xml_string): def from_string(self, xml_string):
""" create a KML object from a xml string""" """ create a KML object from a xml string"""
element = etree.XML(xml_string) element = etree.XML(xml_string)
...@@ -120,18 +113,18 @@ class _Feature(object): ...@@ -120,18 +113,18 @@ class _Feature(object):
def etree_element(self): def etree_element(self):
if self.__name__: if self.__name__:
element = etree.Element(self.__name__) element = etree.Element(self.ns + self.__name__)
if self.id: if self.id:
element.set('id', self.id) element.set('id', self.id)
if self.name: if self.name:
name = etree.SubElement(element, "name") name = etree.SubElement(element, "%sname" %self.ns)
name.text = self.name name.text = self.name
if self.description: if self.description:
description =etree.SubElement(element, "description") description =etree.SubElement(element, "%sdescription" %self.ns)
description.text = self.description description.text = self.description
visibility = etree.SubElement(element, "visibility") visibility = etree.SubElement(element, "%svisibility" %self.ns)
visibility.text = str(self.visibility) visibility.text = str(self.visibility)
isopen = etree.SubElement(element, "open") isopen = etree.SubElement(element, "%sopen" %self.ns)
isopen.text = str(self.isopen) isopen.text = str(self.isopen)
else: else:
raise NotImplementedError raise NotImplementedError
...@@ -326,7 +319,7 @@ class Placemark(_Feature): ...@@ -326,7 +319,7 @@ class Placemark(_Feature):
def _etree_coordinates(self, coordinates): def _etree_coordinates(self, coordinates):
element = etree.Element("coordinates") element = etree.Element("%scoordinates" %self.ns)
if len(coordinates[0]) == 2: if len(coordinates[0]) == 2:
tuples = ('%f,%f,0.0' % tuple(c) for c in coordinates) tuples = ('%f,%f,0.0' % tuple(c) for c in coordinates)
elif len(coordinates[0]) == 3: elif len(coordinates[0]) == 3:
...@@ -337,46 +330,46 @@ class Placemark(_Feature): ...@@ -337,46 +330,46 @@ class Placemark(_Feature):
return element return element
def _etree_point(self, point): def _etree_point(self, point):
element = etree.Element("Point") element = etree.Element("%sPoint" %self.ns)
coords = list(point.coords) coords = list(point.coords)
element.append(self._etree_coordinates(coords)) element.append(self._etree_coordinates(coords))
return element return element
def _etree_linestring(self, linestring): def _etree_linestring(self, linestring):
element = etree.Element("LineString") element = etree.Element("%sLineString" %self.ns)
coords = list(linestring.coords) coords = list(linestring.coords)
element.append(self._etree_coordinates(coords)) element.append(self._etree_coordinates(coords))
return element return element
def _etree_linearring(self, linearring): def _etree_linearring(self, linearring):
element = etree.Element("LinearRing") element = etree.Element("%sLinearRing" %self.ns)
coords = list(linearring.coords) coords = list(linearring.coords)
element.append(self._etree_coordinates(coords)) element.append(self._etree_coordinates(coords))
return element return element
def _etree_polygon(self, polygon): def _etree_polygon(self, polygon):
element = etree.Element("Polygon") element = etree.Element("%sPolygon" %self.ns)
outer_boundary = etree.SubElement(element, "outerBoundaryIs") outer_boundary = etree.SubElement(element, "%souterBoundaryIs" %self.ns)
outer_boundary.append(self._etree_linearring(polygon.exterior)) outer_boundary.append(self._etree_linearring(polygon.exterior))
for ib in polygon.interiors: for ib in polygon.interiors:
inner_boundary = etree.SubElement(element, "innerBoundaryIs") inner_boundary = etree.SubElement(element, "%sinnerBoundaryIs" %self.ns)
inner_boundary.append(self._etree_linearring(ib)) inner_boundary.append(self._etree_linearring(ib))
return element return element
def _etree_multipoint(self, points): def _etree_multipoint(self, points):
element = etree.Element("MultiGeometry") element = etree.Element("%sMultiGeometry" %self.ns)
for point in points.geoms: for point in points.geoms:
element.append(self._etree_point(point)) element.append(self._etree_point(point))
return element return element
def _etree_multilinestring(self, linestrings): def _etree_multilinestring(self, linestrings):
element = etree.Element("MultiGeometry") element = etree.Element("%sMultiGeometry" %self.ns)
for linestring in linestrings.geoms: for linestring in linestrings.geoms:
element.append(self._etree_linestring(linestring)) element.append(self._etree_linestring(linestring))
return element return element
def _etree_multipolygon(self, polygons): def _etree_multipolygon(self, polygons):
element = etree.Element("MultiGeometry") element = etree.Element("%sMultiGeometry" %self.ns)
for polygon in polygons.geoms: for polygon in polygons.geoms:
element.append(self._etree_polygon(polygon)) element.append(self._etree_polygon(polygon))
return element return element
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment