Basic usage =========== ``plpygis`` is a Python conveter to and from the PostGIS `geometry `_ type, WKB, EWKB, GeoJSON, Shapely geometries and any object that supports ``__geo_interface__``. ``plpygis`` is intended for use in PL/Python, allowing procedural Python code to complement PostGIS types and functions. :class:`Geometry ` --------------------------------------------- New :class:`Geometry ` instances can be created using a `Well-Known Binary (WKB) `_ representation of the geometry. .. code-block:: python >>> from plpygis import Geometry >>> geom = Geometry("01010000000000000000004AC00000000000000000") Creation ~~~~~~~~ :class:`Geometry ` instances may also be created from different representations of a geometry. :class:`Geometry ` instances can be converted using the following methods: * :meth:`from_geojson() ` * :meth:`from_shapely() ` .. code-block:: python >>> from plpygis import Geometry >>> geom = Geometry.from_geojson({'type': 'Point', 'coordinates': [-52.0, 0.0]}) The :meth:`shape() ` method can convert from any instance that provides ``__geo_interface__`` (see `A Python Protocol for Geospatial Data `_). An optional ``srid`` keyword argument may be used with any of the above to set the geometry's SRID. If the representation already provides an SRID (such as with some Shapely geometries) or implies a particular SRID (GeoJSON), it will be overridden by the user-specified value. Geometry types ~~~~~~~~~~~~~~ Every :class:`Geometry ` has a type that can be accessed using the instance's :meth:`type ` property. The following geometry types are supported: * Point * LineString * Polygon * MultiPoint * MultiLineString * MultiPolygon * GeometryCollection The following EWKB types are not supported: * Unknown * CircularString * CompoundCurve * CurvePolygon * MultiCurve * MultiSurface * PolyhedralSurface * Triangle * Tin Conversion ~~~~~~~~~~ :class:`Geometry ` instances can also be converted to other representations using the following properties: * :meth:`geojson ` * :meth:`shapely ` * :meth:`wkb ` .. code-block:: python >>> from plpygis import Geometry >>> geom = Geometry("01010000000000000000004AC00000000000000000") >>> print(geom.shapely) POINT (-52 0) :class:`Geometry ` also implements :attr:`__geo_interface__ `. Conversion to GeoJSON or Shapely will result in the M dimension being lost as these representation only support X, Y and Z coordinates (see `RFC 7946 `_). Exceptions ---------- All ``plpygis`` exceptions inherit from the :class:`PlpygisError ` class. The specific exceptions that may be raised are: * :class:`DependencyError `: missing dependency required for an optional feature, such as :meth:`shapely ` * :class:`CoordinateError `: error in the coordinates used to create a :class:`Geometry ` * :class:`DimensionalityError `: error pertaining to the Z or M coordinates of a :class:`Geometry ` * :class:`GeojsonError `: error reading a GeoJSON * :class:`SridError `: error pertaining to a :class:`Geometry `'s SRIDs * :class:`WkbError `: error reading or writing a WKB