1.1 --- a/django/contrib/gis/geos/collections.py Thu Jan 29 16:59:00 2009 -0600
1.2 +++ b/django/contrib/gis/geos/collections.py Sat Jan 31 15:18:50 2009 -0600
1.3 @@ -32,39 +32,40 @@
1.4 init_geoms = args
1.5
1.6 # Ensuring that only the permitted geometries are allowed in this collection
1.7 - if False in [isinstance(geom, self._allowed) for geom in init_geoms]:
1.8 - raise TypeError('Invalid Geometry type encountered in the arguments.')
1.9 + self._checkAllowedTypes(init_geoms)
1.10
1.11 # Creating the geometry pointer array.
1.12 - ngeoms = len(init_geoms)
1.13 - geoms = get_pointer_arr(ngeoms)
1.14 - for i in xrange(ngeoms): geoms[i] = capi.geom_clone(init_geoms[i].ptr)
1.15 - super(GeometryCollection, self).__init__(capi.create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms)), **kwargs)
1.16 + collection = self._createCollection(len(init_geoms), iter(init_geoms))
1.17 + super(GeometryCollection, self).__init__(collection, **kwargs)
1.18
1.19 - def __getitem__(self, index):
1.20 + @classmethod
1.21 + def _createCollection(cls, length, items):
1.22 + # Creating the geometry pointer array.
1.23 + geoms = get_pointer_arr(length)
1.24 + for i, g in enumerate(items):
1.25 + # this is a little sloppy, but makes life easier
1.26 + # allow GEOSGeometry types (python wrappers) or pointer types
1.27 + if hasattr(g, 'ptr'):
1.28 + geoms[i] = capi.geom_clone(g.ptr)
1.29 + else:
1.30 + geoms[i] = capi.geom_clone(g)
1.31 +
1.32 + return capi.create_collection(c_int(cls._typeid), byref(geoms), c_uint(length))
1.33 +
1.34 + def _getItemInternal(self, index):
1.35 + return capi.get_geomn(self.ptr, index)
1.36 +
1.37 + def _getItemExternal(self, index):
1.38 "Returns the Geometry from this Collection at the given index (0-based)."
1.39 # Checking the index and returning the corresponding GEOS geometry.
1.40 self._checkindex(index)
1.41 - return GEOSGeometry(capi.geom_clone(capi.get_geomn(self.ptr, index)), srid=self.srid)
1.42 + return GEOSGeometry(capi.geom_clone(self._getItemInternal(index)), srid=self.srid)
1.43
1.44 - def __setitem__(self, index, geom):
1.45 - "Sets the Geometry at the specified index."
1.46 - self._checkindex(index)
1.47 - if not isinstance(geom, self._allowed):
1.48 - raise TypeError('Incompatible Geometry for collection.')
1.49 -
1.50 - ngeoms = len(self)
1.51 - geoms = get_pointer_arr(ngeoms)
1.52 - for i in xrange(ngeoms):
1.53 - if i == index:
1.54 - geoms[i] = capi.geom_clone(geom.ptr)
1.55 - else:
1.56 - geoms[i] = capi.geom_clone(capi.get_geomn(self.ptr, i))
1.57 -
1.58 - # Creating a new collection, and destroying the contents of the previous poiner.
1.59 + def _setCollection(self, length, items):
1.60 + "Create a new collection, and destroy the contents of the previous pointer."
1.61 prev_ptr = self.ptr
1.62 srid = self.srid
1.63 - self.ptr = capi.create_collection(c_int(self._typeid), byref(geoms), c_uint(ngeoms))
1.64 + self.ptr = self._createCollection(length, items)
1.65 if srid: self.srid = srid
1.66 capi.destroy_geom(prev_ptr)
1.67
1.68 @@ -77,11 +78,6 @@
1.69 "Returns the number of geometries in this Collection."
1.70 return self.num_geom
1.71
1.72 - def _checkindex(self, index):
1.73 - "Checks the given geometry index."
1.74 - if index < 0 or index >= self.num_geom:
1.75 - raise GEOSIndexError('invalid GEOS Geometry index: %s' % str(index))
1.76 -
1.77 @property
1.78 def kml(self):
1.79 "Returns the KML for this Geometry Collection."