collection.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Multi-part collections of geometries."""
  2. import shapely
  3. from shapely.geometry.base import BaseGeometry, BaseMultipartGeometry
  4. class GeometryCollection(BaseMultipartGeometry):
  5. """Collection of one or more geometries that can be of different types.
  6. Parameters
  7. ----------
  8. geoms : list
  9. A list of shapely geometry instances, which may be of varying geometry
  10. types.
  11. Attributes
  12. ----------
  13. geoms : sequence
  14. A sequence of Shapely geometry instances
  15. Examples
  16. --------
  17. Create a GeometryCollection with a Point and a LineString
  18. >>> from shapely import GeometryCollection, LineString, Point
  19. >>> p = Point(51, -1)
  20. >>> l = LineString([(52, -1), (49, 2)])
  21. >>> gc = GeometryCollection([p, l])
  22. """
  23. __slots__ = []
  24. def __new__(self, geoms=None):
  25. """Create a new GeometryCollection."""
  26. if isinstance(geoms, BaseGeometry):
  27. # TODO(shapely-2.0) do we actually want to split Multi-part geometries?
  28. # this is needed for the split() tests
  29. if hasattr(geoms, "geoms"):
  30. geoms = geoms.geoms
  31. else:
  32. geoms = [geoms]
  33. elif geoms is None or len(geoms) == 0:
  34. # TODO better empty constructor
  35. return shapely.from_wkt("GEOMETRYCOLLECTION EMPTY")
  36. return shapely.geometrycollections(geoms)
  37. @property
  38. def __geo_interface__(self):
  39. """Return a GeoJSON-like mapping of the geometry collection."""
  40. geometries = []
  41. for geom in self.geoms:
  42. geometries.append(geom.__geo_interface__)
  43. return dict(type="GeometryCollection", geometries=geometries)
  44. shapely.lib.registry[7] = GeometryCollection