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

Uploaded Source

Built Distribution

plpygis-0.2.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: plpygis-0.2.1.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for plpygis-0.2.1.tar.gz
Algorithm Hash digest
SHA256 62b3ef9ee81293e4f8ca423066cea5940542521cd16c4a08852bf73305da33c8
MD5 04a2516920eb954082604aa406985541
BLAKE2b-256 1d88d268adf8b7592d070679ee4f72061ae77c3c8368c97b8f220efd00dbb4ce

See more details on using hashes here.

File details

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

File metadata

  • Download URL: plpygis-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for plpygis-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1504cc08a032aa6585914b84d1c714a91d07c2fc5bce278c122f9457d575f2d
MD5 44ceed2505b20429a7cb155e2235eaff
BLAKE2b-256 14626998f561ee8b06085e356d205839c4e15fc9139cc5d1af5fa20f679bbc43

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