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))