plpygis package

class plpygis.geometry.Geometry

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.geometry import Point
>>> Geometry.from_shapely(Point((0, 0)), 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.

type

The geometry type.

srid

The geometry SRID.

geojson

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

wkb

Get the geometry as an (E)WKB.

shapely

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

bounds

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

postgis_type

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

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

Bases: plpygis.geometry.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.

x

X coordinate.

y

M coordinate.

z

Z coordinate.

m

M coordinate.

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
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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

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

Bases: plpygis.geometry.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.

vertices

List of vertices that comprise the line.

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
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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

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

Bases: plpygis.geometry.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.

rings

List of linearrings that comprise the polygon.

exterior

The exterior ring of the polygon.

interior

A list of interior rings of the polygon.

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
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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

class plpygis.geometry.MultiPoint(points=None, srid=None)

Bases: plpygis.geometry._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 dimensionality of all geometries in the collection must be identical.

bounds

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

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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

points

List of all component points.

geometries

List of all component points.

class plpygis.geometry.MultiLineString(linestrings=None, srid=None)

Bases: plpygis.geometry._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 dimensionality of all geometries in the collection must be identical.

bounds

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

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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

linestrings

List of all component lines.

geometries

List of all component lines.

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

Bases: plpygis.geometry._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)]], srid=4326)
>>> p2 = Polygon([[(2, 2), (3, 2), (3, 3), (2, 3), (2, 2)]], srid=4326)
>>> MultiPolygon([p1, p2])
<MultiPolygon: 'geometry(MultiPolygon,4326)'>

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

bounds

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

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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

polygons

List of all component polygons.

geometries

List of all component polygons.

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

Bases: plpygis.geometry._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 dimensionality of all geometries in the collection must be identical.

bounds

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

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
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.

geojson

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

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.

shapely

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

srid

The geometry SRID.

type

The geometry type.

wkb

Get the geometry as an (E)WKB.

geometries

List of all component geometries.

exception plpygis.exceptions.PlpygisError(msg)

Bases: exceptions.Exception

Basic exception for plpygis.

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

Bases: plpygis.exceptions.PlpygisError, exceptions.ImportError

Exception for a missing dependency.

exception plpygis.exceptions.WkbError(msg=None)

Bases: plpygis.exceptions.PlpygisError

Exception for problems in parsing WKBs.

exception plpygis.exceptions.DimensionalityError(msg=None)

Bases: plpygis.exceptions.PlpygisError

Exception for problems in dimensionality of geometries.

exception plpygis.exceptions.SridError(msg=None)

Bases: plpygis.exceptions.PlpygisError

Exception for problems in dimensionality of geometries.