Commit 1951be6b authored by Christian Ledermann's avatar Christian Ledermann

tests for geometry.Geometry

parent d2f9d77c
...@@ -54,7 +54,7 @@ class Geometry(_BaseObject): ...@@ -54,7 +54,7 @@ class Geometry(_BaseObject):
geometry: a geometry that implements the __geo_interface__ convention geometry: a geometry that implements the __geo_interface__ convention
extrude: boolean --> Specifies whether to connect the feature to extrude: boolean --> Specifies whether to connect the feature to
the ground with a line. To extrude a Point, the value for the ground with a line. To extrude a Feature, the value for
'altitudeMode' must be either relativeToGround, relativeToSeaFloor, 'altitudeMode' must be either relativeToGround, relativeToSeaFloor,
or absolute. The feature is extruded toward the center of the or absolute. The feature is extruded toward the center of the
Earth's sphere. Earth's sphere.
...@@ -107,6 +107,21 @@ class Geometry(_BaseObject): ...@@ -107,6 +107,21 @@ class Geometry(_BaseObject):
self.geometry = asShape(geometry) self.geometry = asShape(geometry)
def _set_altitude_mode(self, element):
if self.altitude_mode:
assert(self.altitude_mode in ['clampToGround',
#'relativeToSeaFloor', 'clampToSeaFloor',
'relativeToGround', 'absolute'])
if self.altitude_mode != 'clampToGround':
element.set('altitudeMode', self.altitude_mode)
def _set_extrude(self, element):
if self.extrude and self.altitude_mode in ['relativeToGround',
#'relativeToSeaFloor',
'absolute']:
element.set('extrude', '1')
def _etree_coordinates(self, coordinates): def _etree_coordinates(self, coordinates):
clampToGround = (self.altitude_mode == 'clampToGround') or (self.altitude_mode == None) clampToGround = (self.altitude_mode == 'clampToGround') or (self.altitude_mode == None)
element = etree.Element("%scoordinates" %self.ns) element = etree.Element("%scoordinates" %self.ns)
...@@ -129,24 +144,40 @@ class Geometry(_BaseObject): ...@@ -129,24 +144,40 @@ class Geometry(_BaseObject):
def _etree_point(self, point): def _etree_point(self, point):
element = etree.Element("%sPoint" %self.ns) element = etree.Element("%sPoint" %self.ns)
self._set_extrude(element)
self._set_altitude_mode(element)
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("%sLineString" %self.ns) element = etree.Element("%sLineString" %self.ns)
self._set_extrude(element)
self._set_altitude_mode(element)
if self.tessellate and self.altitude_mode in ['clampToGround',
'clampToSeaFloor']:
element.set('tessellate', '1')
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("%sLinearRing" %self.ns) element = etree.Element("%sLinearRing" %self.ns)
self._set_extrude(element)
self._set_altitude_mode(element)
# tesseleation is ignored by polygon and tesselation together with
# LinearRing without a polygon very rare Edgecase -> ignore for now
#if self.tessellate and self.altitude_mode in ['clampToGround',
# 'clampToSeaFloor']:
# element.set('tessellate', '1')
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("%sPolygon" %self.ns) element = etree.Element("%sPolygon" %self.ns)
self._set_extrude(element)
self._set_altitude_mode(element)
outer_boundary = etree.SubElement(element, "%souterBoundaryIs" %self.ns) 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:
...@@ -177,10 +208,10 @@ class Geometry(_BaseObject): ...@@ -177,10 +208,10 @@ class Geometry(_BaseObject):
for feature in features.geoms: for feature in features.geoms:
if isinstance(feature, Point): if isinstance(feature, Point):
element.append(self._etree_point(feature)) element.append(self._etree_point(feature))
elif isinstance(feature, LineString):
element.append(self._etree_linestring(feature))
elif isinstance(feature, LinearRing): elif isinstance(feature, LinearRing):
element.append(self._etree_linearring(feature)) element.append(self._etree_linearring(feature))
elif isinstance(feature, LineString):
element.append(self._etree_linestring(feature))
elif isinstance(feature, Polygon): elif isinstance(feature, Polygon):
element.append(self._etree_polygon(feature)) element.append(self._etree_polygon(feature))
else: else:
...@@ -190,10 +221,10 @@ class Geometry(_BaseObject): ...@@ -190,10 +221,10 @@ class Geometry(_BaseObject):
def etree_element(self): def etree_element(self):
if isinstance(self.geometry, Point): if isinstance(self.geometry, Point):
return self._etree_point(self.geometry) return self._etree_point(self.geometry)
elif isinstance(self.geometry, LineString):
return self._etree_linestring(self.geometry)
elif isinstance(self.geometry, LinearRing): elif isinstance(self.geometry, LinearRing):
return self._etree_linearring(self.geometry) return self._etree_linearring(self.geometry)
elif isinstance(self.geometry, LineString):
return self._etree_linestring(self.geometry)
elif isinstance(self.geometry, Polygon): elif isinstance(self.geometry, Polygon):
return self._etree_polygon(self.geometry) return self._etree_polygon(self.geometry)
elif isinstance(self.geometry, MultiPoint): elif isinstance(self.geometry, MultiPoint):
......
...@@ -640,7 +640,7 @@ class Placemark(_Feature): ...@@ -640,7 +640,7 @@ class Placemark(_Feature):
else: else:
logger.warn('No geometries found') logger.warn('No geometries found')
#XXX move to geometry.Geometry
def _etree_coordinates(self, coordinates): def _etree_coordinates(self, coordinates):
element = etree.Element("%scoordinates" %self.ns) element = etree.Element("%scoordinates" %self.ns)
if len(coordinates[0]) == 2: if len(coordinates[0]) == 2:
......
...@@ -891,7 +891,130 @@ class AtomTestCase( unittest.TestCase ): ...@@ -891,7 +891,130 @@ class AtomTestCase( unittest.TestCase ):
l.href = None l.href = None
self.assertRaises(ValueError, l.to_string) self.assertRaises(ValueError, l.to_string)
class GeometryTestCase( unittest.TestCase ): class SetGeometryTestCase( unittest.TestCase ):
def test_altitude_mode(self):
geom=Geometry()
geom.geometry = Point(0,1)
self.assertEqual(geom.altitude_mode , None)
self.assertFalse('altitudeMode' in geom.to_string())
geom.altitude_mode = 'unknown'
self.assertRaises(AssertionError, geom.to_string)
geom.altitude_mode = 'clampToSeaFloor'
self.assertRaises(AssertionError, geom.to_string)
geom.altitude_mode = 'relativeToSeaFloor'
self.assertRaises(AssertionError, geom.to_string)
geom.altitude_mode = 'clampToGround'
self.assertFalse('altitudeMode' in geom.to_string())
geom.altitude_mode = 'relativeToGround'
self.assertTrue('altitudeMode="relativeToGround"' in geom.to_string())
geom.altitude_mode = 'absolute'
self.assertTrue('altitudeMode="absolute"' in geom.to_string())
def test_extrude(self):
geom=Geometry()
self.assertEqual(geom.extrude , False)
geom.geometry = Point(0,1)
geom.extrude=False
self.assertFalse('extrude' in geom.to_string())
geom.extrude=True
geom.altitude_mode = 'clampToGround'
self.assertFalse('extrude' in geom.to_string())
geom.altitude_mode = 'relativeToGround'
self.assertTrue('extrude="1"' in geom.to_string())
geom.altitude_mode = 'absolute'
self.assertTrue('extrude="1"' in geom.to_string())
def test_tesselate(self):
geom=Geometry()
self.assertEqual(geom.tessellate , False)
geom.geometry = LineString([(0,0), (1,1)])
self.assertFalse('tessellate' in geom.to_string())
geom.altitude_mode = 'clampToGround'
self.assertFalse('tessellate' in geom.to_string())
geom.altitude_mode = 'relativeToGround'
self.assertFalse('tessellate' in geom.to_string())
geom.altitude_mode = 'absolute'
self.assertFalse('tessellate' in geom.to_string())
geom.tessellate = True
geom.altitude_mode = None
self.assertFalse('tessellate' in geom.to_string())
geom.altitude_mode = 'relativeToGround'
self.assertFalse('tessellate' in geom.to_string())
geom.altitude_mode = 'absolute'
self.assertFalse('tessellate' in geom.to_string())
geom.altitude_mode = 'clampToGround'
self.assertTrue('tessellate="1"' in geom.to_string())
#for geometries != LineString tesselate is ignored
geom.geometry = Point(0,1)
self.assertFalse('tessellate' in geom.to_string())
def testPoint(self):
p = Point(0,1)
g = Geometry(geometry=p)
self.assertEqual(g.geometry, p)
g = Geometry(geometry=p.__geo_interface__)
self.assertEqual(g.geometry.__geo_interface__, p.__geo_interface__)
self.assertTrue('Point' in g.to_string())
self.assertTrue('coordinates>0.000000,1.000000</' in g.to_string())
def testLineString(self):
l = LineString([(0,0), (1,1)])
g = Geometry(geometry=l)
self.assertEqual(g.geometry, l)
self.assertTrue('LineString' in g.to_string())
self.assertTrue('coordinates>0.000000,0.000000 1.000000,1.000000</' in g.to_string())
def testLinearRing(self):
l = LinearRing([(0,0), (1,0), (1,1), (0,0)])
g = Geometry(geometry=l)
self.assertEqual(g.geometry, l)
self.assertTrue('LinearRing' in g.to_string())
self.assertTrue(
'coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 0.000000,0.000000</'
in g.to_string())
def testPolygon(self):
#without holes
l = Polygon([(0,0), (1,0), (1,1), (0,0)])
g = Geometry(geometry=l)
self.assertEqual(g.geometry, l)
self.assertTrue('Polygon' in g.to_string())
self.assertTrue('outerBoundaryIs' in g.to_string())
self.assertFalse('innerBoundaryIs' in g.to_string())
self.assertTrue('LinearRing' in g.to_string())
self.assertTrue(
'coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 0.000000,0.000000</'
in g.to_string())
#with holes
p = Polygon([(-1,-1), (2,-1), (2,2), (-1,-1)],[[(0,0), (1,0), (1,1), (0,0)]])
g = Geometry(geometry=p)
self.assertEqual(g.geometry, p)
self.assertTrue('Polygon' in g.to_string())
self.assertTrue('outerBoundaryIs' in g.to_string())
self.assertTrue('innerBoundaryIs' in g.to_string())
self.assertTrue('LinearRing' in g.to_string())
self.assertTrue(
'coordinates>0.000000,0.000000 1.000000,0.000000 1.000000,1.000000 0.000000,0.000000</'
in g.to_string())
self.assertTrue(
'coordinates>-1.000000,-1.000000 2.000000,-1.000000 2.000000,2.000000 -1.000000,-1.000000</'
in g.to_string())
def testMultiPoint(self):
pass
def testMultiLineString(self):
pass
def testMultiPolygon(self):
pass
def testGeometryCollection(self):
pass
class GetGeometryTestCase( unittest.TestCase ):
def testPoint(self): def testPoint(self):
pass pass
...@@ -901,7 +1024,6 @@ class GeometryTestCase( unittest.TestCase ): ...@@ -901,7 +1024,6 @@ class GeometryTestCase( unittest.TestCase ):
pass pass
def testPolygon(self): def testPolygon(self):
pass pass
def testMultiPoint(self): def testMultiPoint(self):
pass pass
def testMultiLineString(self): def testMultiLineString(self):
...@@ -916,7 +1038,6 @@ class GeometryTestCase( unittest.TestCase ): ...@@ -916,7 +1038,6 @@ class GeometryTestCase( unittest.TestCase ):
class Force3DTestCase( unittest.TestCase ): class Force3DTestCase( unittest.TestCase ):
def test3d(self): def test3d(self):
#altitudeMode clampToGround indicates to ignore an altitude specification.
ns ='' ns =''
p2 = kml.Placemark(ns, 'id', 'name', 'description') p2 = kml.Placemark(ns, 'id', 'name', 'description')
p2.geometry = Polygon([(0, 0), (1, 1), (1, 0)]) p2.geometry = Polygon([(0, 0), (1, 1), (1, 0)])
...@@ -926,7 +1047,12 @@ class Force3DTestCase( unittest.TestCase ): ...@@ -926,7 +1047,12 @@ class Force3DTestCase( unittest.TestCase ):
self.assertNotEqual(p2.to_string(), p3.to_string()) self.assertNotEqual(p2.to_string(), p3.to_string())
config.FORCE3D = True config.FORCE3D = True
self.assertEqual(p2.to_string(), p3.to_string()) self.assertEqual(p2.to_string(), p3.to_string())
#XXX
#altitudeMode clampToGround indicates to ignore an altitude specification.
#p3.altitudeMode = 'clampToGround'
#self.assertEqual(p2.to_string(), p3.to_string())
#config.FORCE3D = False
#self.assertEqual(p2.to_string(), p3.to_string())
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
...@@ -936,7 +1062,8 @@ def test_suite(): ...@@ -936,7 +1062,8 @@ def test_suite():
suite.addTest(unittest.makeSuite(BaseClassesTestCase)) suite.addTest(unittest.makeSuite(BaseClassesTestCase))
suite.addTest(unittest.makeSuite(DateTimeTestCase)) suite.addTest(unittest.makeSuite(DateTimeTestCase))
suite.addTest(unittest.makeSuite(AtomTestCase)) suite.addTest(unittest.makeSuite(AtomTestCase))
suite.addTest(unittest.makeSuite(GeometryTestCase)) suite.addTest(unittest.makeSuite(SetGeometryTestCase))
suite.addTest(unittest.makeSuite(GetGeometryTestCase))
suite.addTest(unittest.makeSuite(Force3DTestCase)) suite.addTest(unittest.makeSuite(Force3DTestCase))
return suite return suite
......
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