plpygis package

class plpygis.geometry.Geometry(wkb, srid=None, dimz=False, dimm=False)

Bases: object

A representation of a PostGIS geometry.

PostGIS geometries are either an OpenGIS Consortium Simple Features for SQL specification type or a PostGIS extended type. The object’s canonical form is stored in WKB or EWKB format along with an SRID and flags indicating whether the coordinates are 3DZ, 3DM or 4D.

Geometry objects can be created in a number of ways. In all cases, a subclass for the particular geometry type will be instantiated.

From an (E)WKB:

>>> Geometry(b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
<Point: 'geometry(Point)'>

From the hexadecimal string representation of an (E)WKB:

>>> Geometry("0101000080000000000000000000000000000000000000000000000000")
<Point: 'geometry(PointZ)'>

The response above indicates an instance of the Point class has been created and that it represents a PostGIS geometry(PointZ) type.

From a GeoJSON:

>>> Geometry.from_geojson({'type': 'Point', 'coordinates': (0.0, 0.0)})
<Point: 'geometry(Point,4326)'>

From a Shapely object:

>>> from shapely import Point
>>> point = Point(0, 0)
>>> Geometry.from_shapely(point, 3857)
<Point: 'geometry(Point,3857)'>

From any object supporting __geo_interface__:

>>> from shapefile import Reader
>>> feature = Reader("test/multipoint.shp").shape(0)
>>> Geometry.shape(feature)
<MultiPoint: 'geometry(MultiPoint)'>

A Geometry can be read as long as it is one of the following types: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon or GeometryCollection. The M dimension will be preserved.

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property type

The geometry type.

property srid

The geometry SRID.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property wkb

Get the geometry as an (E)WKB.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

class plpygis.geometry.Point(wkb, srid=None, dimz=False, dimm=False)

Bases: Geometry

A representation of a PostGIS Point.

Point objects can be created directly.

>>> Point((0, -52, 5), dimm=True, srid=4326)
<Point: 'geometry(PointM,4326)'>

The dimz and dimm parameters will indicate how to interpret the coordinates that have been passed as the first argument. By default, the third coordinate will be interpreted as representing the Z dimension.

property x

X coordinate.

property y

M coordinate.

property z

Z coordinate.

property m

M coordinate.

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension.

Return type:

bool

property dimm

Whether the geometry has an M dimension.

Getter:

True if the geometry has an M dimension.

Setter:

Add or remove the M dimension.

Return type:

bool

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

class plpygis.geometry.LineString(wkb, srid=None, dimz=False, dimm=False)

Bases: Geometry

A representation of a PostGIS Line.

LineString objects can be created directly.

>>> LineString([(0, 0, 0, 0), (1, 1, 0, 0), (2, 2, 0, 0)])
<LineString: 'geometry(LineStringZM)'>

The dimz and dimm parameters will indicate how to interpret the coordinates that have been passed as the first argument. By default, the third coordinate will be interpreted as representing the Z dimension.

property vertices

List of vertices that comprise the line.

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension from this and all vertices in the line.

Return type:

bool

property dimm

Whether the geometry has a M dimension.

Getter:

True if the geometry has a M dimension.

Setter:

Add or remove the M dimension from this and all vertices in the line.

Return type:

bool

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

class plpygis.geometry.Polygon(wkb, srid=None, dimz=False, dimm=False)

Bases: Geometry

A representation of a PostGIS Polygon.

Polygon objects can be created directly.

>>> Polygon([[(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 0, 0)]])
<Polygon: 'geometry(PolygonZ)'>

The first polygon in the list of linear rings is the exterior ring, while any subsequent rings are interior boundaries.

The dimz and dimm parameters will indicate how to interpret the coordinates that have been passed as the first argument. By default, the third coordinate will be interpreted as representing the Z dimension.

property rings

List of linearrings that comprise the polygon.

property exterior

The exterior ring of the polygon.

property interior

A list of interior rings of the polygon.

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension from this and all linear rings in the polygon.

Return type:

bool

property dimm

Whether the geometry has a M dimension.

Getter:

True if the geometry has a M dimension.

Setter:

Add or remove the M dimension from this and all linear rings in the polygon.

Return type:

bool

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

class plpygis.geometry.MultiPoint(wkb, srid=None, dimz=False, dimm=False)

Bases: _MultiGeometry

A representation of a PostGIS MultiPoint.

MultiPoint objects can be created directly from a list of Point objects.

>>> p1 = Point((0, 0, 0))
>>> p2 = Point((1, 1, 0))
>>> MultiPoint([p1, p2])
<MultiPoint: 'geometry(MultiPointZ)'>

The SRID and dimensionality of all geometries in the collection must be identical.

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

property dimm

Whether the geometry has an M dimension.

Getter:

True if the geometry has an M dimension.

Setter:

Add or remove the M dimension from this and all geometries in the collection.

Return type:

bool

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension from this and all geometries in the collection.

Return type:

bool

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

property points

List of all component points.

property geometries

List of all component points.

class plpygis.geometry.MultiLineString(wkb, srid=None, dimz=False, dimm=False)

Bases: _MultiGeometry

A representation of a PostGIS MultiLineString

MultiLineString objects can be created directly from a list of LineString objects.

>>> l1 = LineString([(1, 1, 0), (2, 2, 0)], dimm=True)
>>> l2 = LineString([(0, 0, 0), (0, 1, 0)], dimm=True)
>>> MultiLineString([l1, l2])
<MultiLineString: 'geometry(MultiLineStringM)'>

The SRID and dimensionality of all geometries in the collection must be identical.

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

property dimm

Whether the geometry has an M dimension.

Getter:

True if the geometry has an M dimension.

Setter:

Add or remove the M dimension from this and all geometries in the collection.

Return type:

bool

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension from this and all geometries in the collection.

Return type:

bool

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

property linestrings

List of all component lines.

property geometries

List of all component lines.

class plpygis.geometry.MultiPolygon(wkb, srid=None, dimz=False, dimm=False)

Bases: _MultiGeometry

A representation of a PostGIS MultiPolygon.

MultiPolygon objects can be created directly from a list of Polygon objects.

>>> p1 = Polygon([[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]])
>>> p2 = Polygon([[(2, 2), (3, 2), (3, 3), (2, 3), (2, 2)]])
>>> MultiPolygon([p1, p2], srid=4326)
<MultiPolygon: 'geometry(MultiPolygon,4326)'>

The SRID and dimensionality of all geometries in the collection must be identical.

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

property dimm

Whether the geometry has an M dimension.

Getter:

True if the geometry has an M dimension.

Setter:

Add or remove the M dimension from this and all geometries in the collection.

Return type:

bool

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension from this and all geometries in the collection.

Return type:

bool

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

property polygons

List of all component polygons.

property geometries

List of all component polygons.

class plpygis.geometry.GeometryCollection(wkb, srid=None, dimz=False, dimm=False)

Bases: _MultiGeometry

A representation of a PostGIS GeometryCollection.

GeometryCollection objects can be created directly from a list of geometries, including other collections.

>>> p = Point((0, 0, 0))
>>> l = LineString([(1, 1, 0), (2, 2, 0)])
>>> GeometryCollection([p, l])
<GeometryCollection: 'geometry(GeometryCollectionZ)'>

The SRID and dimensionality of all geometries in the collection must be identical.

property bounds

Get the minimum and maximum extents of the geometry: (minx, miny, maxx, maxy).

property dimm

Whether the geometry has an M dimension.

Getter:

True if the geometry has an M dimension.

Setter:

Add or remove the M dimension from this and all geometries in the collection.

Return type:

bool

property dimz

Whether the geometry has a Z dimension.

Getter:

True if the geometry has a Z dimension.

Setter:

Add or remove the Z dimension from this and all geometries in the collection.

Return type:

bool

static from_geojson(geojson, srid=4326)

Create a Geometry from a GeoJSON. The SRID can be overridden from the expected 4326.

static from_shapely(sgeom, srid=None)

Create a Geometry from a Shapely geometry and the specified SRID.

The Shapely geometry will not be modified.

property geojson

Get the geometry as a GeoJSON dict. There is no check that the GeoJSON is using an SRID of 4326.

property postgis_type

Get the type of the geometry in PostGIS format, including additional dimensions and SRID if they exist.

static shape(shape, srid=None)

Create a Geometry using __geo_interface__ and the specified SRID.

property shapely

Get the geometry as a Shapely geometry. If the geometry has an SRID, the Shapely object will be created with it set.

property srid

The geometry SRID.

property type

The geometry type.

property wkb

Get the geometry as an (E)WKB.

property geometries

List of all component geometries.

exception plpygis.exceptions.PlpygisError(msg)

Bases: Exception

Basic exception for plpygis.

exception plpygis.exceptions.DependencyError(dep, msg=None)

Bases: PlpygisError, ImportError

Exception for a missing dependency.

exception plpygis.exceptions.WkbError(msg=None)

Bases: PlpygisError

Exception for problems in parsing WKBs.

exception plpygis.exceptions.DimensionalityError(msg=None)

Bases: PlpygisError

Exception for problems in dimensionality of geometries.

exception plpygis.exceptions.SridError(msg=None)

Bases: PlpygisError

Exception for problems in dimensionality of geometries.