bosth / plpygis

PL/Python for PostGIS

Home Page:https://vimeo.com/248099711

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

Documentation Status

Continuous Integration

About

PL/Python for PostGIS

https://vimeo.com/248099711

License:GNU General Public License v3.0


Languages

Language:Python 99.6%Language:Makefile 0.4%