1"""Multi-part collections of geometries 2""" 3 4from ctypes import c_void_p 5 6from shapely.geos import lgeos 7from shapely.geometry.base import BaseGeometry 8from shapely.geometry.base import BaseMultipartGeometry 9from shapely.geometry.base import HeterogeneousGeometrySequence 10from shapely.geometry.base import geos_geom_from_py 11 12 13class GeometryCollection(BaseMultipartGeometry): 14 15 """A heterogenous collection of geometries 16 17 Attributes 18 ---------- 19 geoms : sequence 20 A sequence of Shapely geometry instances 21 """ 22 23 def __init__(self, geoms=None): 24 """ 25 Parameters 26 ---------- 27 geoms : list 28 A list of shapely geometry instances, which may be heterogenous. 29 30 Example 31 ------- 32 Create a GeometryCollection with a Point and a LineString 33 34 >>> from shapely.geometry import LineString, Point 35 >>> p = Point(51, -1) 36 >>> l = LineString([(52, -1), (49, 2)]) 37 >>> gc = GeometryCollection([p, l]) 38 """ 39 BaseMultipartGeometry.__init__(self) 40 if not geoms: 41 pass 42 else: 43 geom, n = geos_geometrycollection_from_py(geoms) 44 self._set_geom(geom) 45 self._ndim = n 46 47 @property 48 def __geo_interface__(self): 49 geometries = [] 50 for geom in self.geoms: 51 geometries.append(geom.__geo_interface__) 52 return dict(type='GeometryCollection', geometries=geometries) 53 54 @property 55 def geoms(self): 56 if self.is_empty: 57 return [] 58 return HeterogeneousGeometrySequence(self) 59 60def geos_geometrycollection_from_py(ob): 61 """Creates a GEOS GeometryCollection from a list of geometries""" 62 if isinstance(ob, BaseMultipartGeometry): 63 ob = ob.geoms 64 L = len(ob) 65 N = 2 66 subs = (c_void_p * L)() 67 for l in range(L): 68 assert(isinstance(ob[l], BaseGeometry)) 69 if ob[l].has_z: 70 N = 3 71 geom, n = geos_geom_from_py(ob[l]) 72 subs[l] = geom 73 74 return (lgeos.GEOSGeom_createCollection(7, subs, L), N) 75