plpygis: Python tools for PostGIS

0.6.1 · active · verified Wed Apr 15

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.

Warnings

Install

Imports

Quickstart

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.

from plpygis import Point, LineString
from plpygis.wkt import from_wkt

# Create a Point geometry with an SRID
point = Point(10, 20, srid=4326)
print(f"Point WKT: {point.wkt}")
print(f"Point EWKT: {point.ewkt}")
print(f"Point WKB (hex): {point.wkb.hex()}")

# Create a LineString from EWKT, specifying SRID during parsing
line_ewkt = "SRID=4326;LINESTRING (30 10, 40 20)"
line = from_wkt(line_ewkt, srid=4326) # srid is optional if present in EWKT, but good practice
print(f"LineString GeoJSON: {line.geojson}")
print(f"LineString length: {len(line)}")

# Access properties
print(f"First coordinate of line: {line.geometries[0]}")

view raw JSON →