Basic usage =========== ``plpygis`` is a Python converter to and from the PostGIS `geometry `_ type, WKB, EWKB, WKT, EWKT, 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 in hexadecimal form. .. 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_wkt() ` * :meth:`from_geojson() ` * :meth:`from_shapely() ` .. code-block:: python >>> from plpygis import Geometry >>> point = Geometry.from_geojson({'type': 'Point', 'coordinates': [-52.0, 0.0]}) >>> polygon = Geometry.from_wkt("POLYGON ((0 0, 1 0, 1 1, 0 1, 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 ` * :meth:`ewkb ` * :meth:`wkt ` * :meth:`ewkt ` .. code-block:: python >>> from plpygis import Geometry >>> geom = Geometry("01010000000000000000004AC00000000000000000") >>> print(geom.wkt) 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 `_). The precision of coordinates in WKT/EWKT can be controlled by setting :attr:`plpygis.wkt.PRECISION `; by default, this value is 6. Exceptions ---------- All ``plpygis`` exceptions inherit from the :class:`PlpygisError ` class. The specific exceptions that may be raised are: * :py:exc:`DependencyError `: missing dependency required for an optional feature, such as :meth:`shapely ` * :py:exc:`CollectionError `: error when attempting to create a multigeometry or geometry collection * :py:exc:`CoordinateError `: error in the coordinates used to create a :class:`Geometry ` * :py:exc:`DimensionalityError `: error pertaining to the Z or M coordinates of a :class:`Geometry ` * :py:exc:`GeojsonError `: error reading a GeoJSON * :py:exc:`SridError `: error pertaining to a :class:`Geometry `'s SRIDs * :py:exc:`WkbError `: error reading or writing a WKB * :py:exc:`WktError `: error reading or writing a WKT