| 12 | |
| 13 | |
| 14 | class GeometryCollection(GEOSGeometry): |
| 15 | _typeid = 7 |
| 16 | |
| 17 | def __init__(self, *args, **kwargs): |
| 18 | "Initialize a Geometry Collection from a sequence of Geometry objects." |
| 19 | # Checking the arguments |
| 20 | if len(args) == 1: |
| 21 | # If only one geometry provided or a list of geometries is provided |
| 22 | # in the first argument. |
| 23 | if isinstance(args[0], (tuple, list)): |
| 24 | init_geoms = args[0] |
| 25 | else: |
| 26 | init_geoms = args |
| 27 | else: |
| 28 | init_geoms = args |
| 29 | |
| 30 | # Ensuring that only the permitted geometries are allowed in this |
| 31 | # collection this is moved to list mixin super class |
| 32 | self._check_allowed(init_geoms) |
| 33 | |
| 34 | # Creating the geometry pointer array. |
| 35 | collection = self._create_collection(len(init_geoms), init_geoms) |
| 36 | super().__init__(collection, **kwargs) |
| 37 | |
| 38 | def __iter__(self): |
| 39 | "Iterate over each Geometry in the Collection." |
| 40 | for i in range(len(self)): |
| 41 | yield self[i] |
| 42 | |
| 43 | def __len__(self): |
| 44 | "Return the number of geometries in this Collection." |
| 45 | return self.num_geom |
| 46 | |
| 47 | # ### Methods for compatibility with ListMixin ### |
| 48 | def _create_collection(self, length, items): |
| 49 | # Creating the geometry pointer array. |
| 50 | geoms = (GEOM_PTR * length)( |
| 51 | *[ |
| 52 | # this is a little sloppy, but makes life easier |
| 53 | # allow GEOSGeometry types (python wrappers) or pointer types |
| 54 | capi.geom_clone(getattr(g, "ptr", g)) |
| 55 | for g in items |
| 56 | ] |
| 57 | ) |
| 58 | return capi.create_collection(self._typeid, geoms, length) |
| 59 | |
| 60 | def _get_single_internal(self, index): |
| 61 | return capi.get_geomn(self.ptr, index) |
| 62 | |
| 63 | def _get_single_external(self, index): |
| 64 | """ |
| 65 | Return the Geometry from this Collection at the given index (0-based). |
| 66 | """ |
| 67 | # Checking the index and returning the corresponding GEOS geometry. |
| 68 | return GEOSGeometry( |
| 69 | capi.geom_clone(self._get_single_internal(index)), srid=self.srid |
| 70 | ) |
| 71 |
no outgoing calls