From 5655cf1b2a7b12117de21abb3a5f77d6ffe9a050 Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Wed, 9 Jan 2008 19:42:23 +0000 Subject: [PATCH] gis: gdal: `GeometryCollection.add()` now accepts other collections, and associated tests. git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@7012 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/gdal/geometries.py | 8 +++++--- django/contrib/gis/tests/test_gdal_geom.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/django/contrib/gis/gdal/geometries.py b/django/contrib/gis/gdal/geometries.py index 5174ef18bb..37bb65d3fa 100644 --- a/django/contrib/gis/gdal/geometries.py +++ b/django/contrib/gis/gdal/geometries.py @@ -538,13 +538,15 @@ class GeometryCollection(OGRGeometry): def add(self, geom): "Add the geometry to this Geometry Collection." if isinstance(geom, OGRGeometry): - ptr = geom._ptr + if isinstance(geom, self.__class__): + for g in geom: add_geom(self._ptr, g._ptr) + else: + add_geom(self._ptr, geom._ptr) elif isinstance(geom, (StringType, UnicodeType)): tmp = OGRGeometry(geom) - ptr = tmp._ptr + add_geom(self._ptr, tmp._ptr) else: raise OGRException('Must add an OGRGeometry.') - add_geom(self._ptr, ptr) @property def point_count(self): diff --git a/django/contrib/gis/tests/test_gdal_geom.py b/django/contrib/gis/tests/test_gdal_geom.py index 8959d9121f..06c2eb769e 100644 --- a/django/contrib/gis/tests/test_gdal_geom.py +++ b/django/contrib/gis/tests/test_gdal_geom.py @@ -323,6 +323,27 @@ class OGRGeomTest(unittest.TestCase): a |= b # testing __ior__ self.assertEqual(u1, a) + def test14_add(self): + "Testing GeometryCollection.add()." + # Can't insert a Point into a MultiPolygon. + mp = OGRGeometry('MultiPolygon') + pnt = OGRGeometry('POINT(5 23)') + self.assertRaises(OGRException, mp.add, pnt) + + # GeometryCollection.add may take an OGRGeometry (if another collection + # of the same type all child geoms will be added individually) or WKT. + for mp in multipolygons: + mpoly = OGRGeometry(mp.wkt) + mp1 = OGRGeometry('MultiPolygon') + mp2 = OGRGeometry('MultiPolygon') + mp3 = OGRGeometry('MultiPolygon') + + for poly in mpoly: + mp1.add(poly) # Adding a geometry at a time + mp2.add(poly.wkt) # Adding WKT + mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once. + for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp) + def suite(): s = unittest.TestSuite() s.addTest(unittest.makeSuite(OGRGeomTest))