Skip to main content

Python tools for PostGIS

Project description

plpygis

plpygis is a pure Python module with no dependencies that can convert geometries between Well-known binary (WKB/EWKB), Well-known Text (WKT/EWKT) and GeoJSON representations. plpygis is mainly intended for use in PostgreSQL PL/Python functions to augment PostGIS's native capabilities.

Basic usage

plpygis implements several subclasses of the Geometry class, such as Point, LineString, MultiPolygon and so on:

>>> from plpygis import Point
>>> p = Point((-124.005, 49.005), srid=4326)
>>> print(p.ewkb)
0101000020e6100000b81e85eb51005fc0713d0ad7a3804840
>>> print(p.geojson)
{'type': 'Point', 'coordinates': [-124.005, 49.005]}
>>> p.z = 1
>>> print(p.wkt)
POINT Z (-124.005 49.005 1)

Usage with PostGIS

plpygis is designed to provide an easy way to implement PL/Python functions that accept geometry arguments or return geometry results. The following example will take a PostGIS geometry(Point) and use an external service to create a geometry(PointZ).

CREATE OR REPLACE FUNCTION add_elevation(geom geometry(POINT))
  RETURNS geometry(POINTZ)
AS $$
  from plpygis import Geometry
  from requests import get
  p = Geometry(geom)

  response = get(f'https://api.open-meteo.com/v1/elevation?longitude={p.x}&latitude={p.y}')
  if response.status_code == 200:
      content = response.json()
      p.z = content['elevation'][0]
      return p
  else:
      return None
$$ LANGUAGE plpython3u;

The Geometry() constructor will convert a PostGIS geometry that has been passed as a parameter to the PL/Python function into one of its plpygis subclasses. A Geometry that is returned from the PL/Python function will automatically be converted back to a PostGIS geometry.

The function above can be called as part of an SQL query:

SELECT
    name,
    add_elevation(geom)
FROM
    city;

Documentation

Full plpygis documentation is available at http://plpygis.readthedocs.io/.

Continuous Integration Documentation Status

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plpygis-0.5.3.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

plpygis-0.5.3-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file plpygis-0.5.3.tar.gz.

File metadata

  • Download URL: plpygis-0.5.3.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.4

File hashes

Hashes for plpygis-0.5.3.tar.gz
Algorithm Hash digest
SHA256 4a4ebb36f3f2fec4d50f1b99b7d1ae2fb7991b153028dacb2455499ee9201169
MD5 551653519265a6c5aa14b4cc330d1bda
BLAKE2b-256 9f9fa0d0cb4e4bf755ad198f73955713c3dcb4734d226baf8e979cccab1be990

See more details on using hashes here.

File details

Details for the file plpygis-0.5.3-py3-none-any.whl.

File metadata

  • Download URL: plpygis-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.1.dev0+g94f810c.d20240510 CPython/3.12.4

File hashes

Hashes for plpygis-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f2f1428e446f644c880c5c4c65a26de1cf2ea2bdf907bc72293d503b1e81397c
MD5 ca677e4b1db14f9b6064f3795078e040
BLAKE2b-256 08c31fd7069d12215ec239da2f4c69658f3884c907c3193a5ed237f049308d04

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page