Shapely Integration

This module provides utility functions for integrating with Shapely.

Note

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=-1, extended=False)[source]

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

Parameters:
  • 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.

Example:

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)[source]

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

Parameters:element – The element to convert into a Shapely object.

Example:

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