Skip to main content

PostGIS Python tools

Project description

plpygis is a Python tool that can convert a PostGIS geometry into an equivalent WKB, EWKB, GeoJSON or Shapely geometry. plpygis is intended for use in PostgreSQL PL/Python functions.

Basic usage

The Geometry class and its subclasses can be used to convert to and from PostGIS geometries. The following example will take a PostGIS multipolygon geometry named geom and find its largest component polygon.

Geometry() can convert a PostGIS geometry that has been passed as a parameter to a PL/Python function. A Geometry that is returned from the PL/Python function will automatically be converted back to a PostGIS geometry.

CREATE OR REPLACE FUNCTION largest_poly(geom geometry)
  RETURNS geometry
AS $$
  from plpygis import Geometry
  polygons = Geometry(geom)
  if polygons.type == "Polygon":
      return polygons
  elif polygons.type == "MultiPolygon":
      largest = max(polygons.shapely, key=lambda polygon: polygon.area)
      return Geometry.from_shapely(largest)
  else:
      return None
$$ LANGUAGE plpython3u;

This can then be called as part of an SQL query:

SELECT largest_poly(geom) FROM countries;

Documentation

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

Documentation Status Continuous Integration

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.3.0.tar.gz (38.2 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: plpygis-0.3.0.tar.gz
  • Upload date:
  • Size: 38.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for plpygis-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2fc4dd0d8fab4868e799e74b15e991ed7bec5f198292343ef602839169e90b76
MD5 dca9e3e97f544d1be6ff33c63d49344a
BLAKE2b-256 8e05c929d3b367032812787e84644ce7902c0a8e70f2d9d6cf7def76012c46dd

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