Skip to main content

Python tools for PostGIS

Project description

plpygis

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/.

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

Uploaded Source

Built Distribution

plpygis-0.4.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plpygis-0.4.0.tar.gz
  • Upload date:
  • Size: 27.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.4.0.tar.gz
Algorithm Hash digest
SHA256 f63baf26f38b2d674a37402b4f69a4a6d1e896416016f4b1714f96950c03ed86
MD5 db9ce96a948d742e1aa79165e3f0f7b7
BLAKE2b-256 c1e4542b72de91b19fa6757bf19a942f1c82e0b7c17de1e081bb67ce108c331c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plpygis-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for plpygis-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ec6609bf707cf30eddce500dcb520fadc178f8d4b2553239e2ff67cd021feb6
MD5 ee183b05339ab07ed5bdf80dab10e61b
BLAKE2b-256 36f52ddb15ed0b4f718953bd8dddbebb24ac128693b385c82c9d5682445539dd

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