plpygis provides Python tools for working with PostGIS geometries, enabling easy conversion between common formats like WKT, EWKT, WKB, EWKB, and GeoJSON. It focuses on parsing and generating these formats directly in Python, making it suitable for applications interacting with PostGIS. The current version is 0.6.1, and the library maintains an active release cadence with minor updates and bug fixes.
pip install plpygisVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates creating Point and LineString geometries, converting them to WKT, EWKT, and GeoJSON, and parsing a geometry from an EWKT string using `from_wkt`. It also shows how to access basic geometry properties.
Update existing code: change `geometry.wkb` to `geometry.ewkb` if you require SRID information embedded in the binary representation, or ensure your consuming system correctly handles standard WKB.
Ensure your project or environment uses Python 3.9 or newer. Upgrade your Python interpreter if necessary.
If your code directly manipulates the output of `__geo_interface__` and expects mutable lists, you will need to convert tuples to lists (e.g., `list(coords)`) or adapt your logic to work with immutable tuples.
To configure the precision, set `plpygis.wkt.PRECISION` to your desired number of decimal places (e.g., `plpygis.wkt.PRECISION = 10`) or `None` for full precision before generating WKTs.
Ensure that your WKT strings for LineString (at least 2 points) and Polygon (at least 3 points, first and last equal for rings) are valid. Handle `plpygis.exceptions.WktError` for malformed inputs.
Install the library using pip: `pip install plpygis`. For PL/Python, ensure it's installed in the Python environment accessible by PostgreSQL.
Install the Shapely library: `pip install shapely`. Also, ensure your `plpygis` version is compatible with your `shapely` version (e.g., `plpygis` 0.6.1 supports `shapely` 2.x).
Review and correct the WKT string to ensure it is syntactically valid and represents a proper geometry. Example: `Geometry.from_wkt('POINT (10 20)')`Ensure the GeoJSON input is a valid GeoJSON structure. Example: `Geometry.from_geojson({'type': 'Point', 'coordinates': [10.0, 20.0]})`Ensure that all component geometries within a collection have consistent SRIDs or no SRID if the collection is being assigned one. Verify SRID values are valid. Example: `Point((0, 0), srid=4326)`
No dependency data recorded yet.