Shapely Integration

This module provides utility functions for integrating with Shapely.


As GeoAlchemy 2 itself has no dependency on Shapely, applications using functions of this module have to ensure that Shapely is available.

geoalchemy2.shape.from_shape(shape, srid: int = -1, extended: bool | None = False) WKBElement[source]

Function to convert a Shapely geometry to a geoalchemy2.types.WKBElement.

  • shape – The shape to convert.

  • srid – An integer representing the spatial reference system. E.g. 4326. Default value is -1, which means no/unknown reference system.

  • extended – A boolean to switch between WKB and EWKB. Default value is False.


from shapely.geometry import Point
wkb_element = from_shape(Point(5, 45), srid=4326)
ewkb_element = from_shape(Point(5, 45), srid=4326, extended=True)
geoalchemy2.shape.to_shape(element: WKBElement | WKTElement)[source]

Function to convert a geoalchemy2.types.SpatialElement to a Shapely geometry.


element – The element to convert into a Shapely object.


lake = Session.query(Lake).get(1)
polygon = to_shape(lake.geom)