# plpygis
`plpygis` is a pure Python module with no dependencies that can convert geometries between [Well-known binary](https://en.wikipedia.org/wiki/Well-known_binary) (WKB/EWKB), [Well-known Text](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) (WKT/EWKT) and GeoJSON representations. `plpygis` is mainly intended for use in PostgreSQL [PL/Python](https://www.postgresql.org/docs/current/plpython.html) functions to augment [PostGIS](https://postgis.net/)'s native capabilities.
## Basic usage
`plpygis` implements several subclasses of the `Geometry` class, such as `Point`, `LineString`, `MultiPolygon` and so on:
```python
>>> 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)`.
```pgsql
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:
```pgsql
SELECT
name,
add_elevation(geom)
FROM
city;
```
## Documentation
Full `plpygis` documentation is available at <http://plpygis.readthedocs.io/>.
[![Continuous Integration](https://github.com/bosth/plpygis/workflows/tests/badge.svg)](https://github.com/bosth/plpygis/actions?query=workflow%3A%22tests%22) [![Documentation Status](https://readthedocs.org/projects/plpygis/badge/?version=latest)](http://plpygis.readthedocs.io/en/latest/?badge=latest)
Raw data
{
"_id": null,
"home_page": null,
"name": "plpygis",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "gis, geospatial, postgis, postgresql, pl/python",
"author": null,
"author_email": "Benjamin Trigona-Harany <plpygis@jaxartes.net>",
"download_url": "https://files.pythonhosted.org/packages/e1/b6/c658eb83673b14ef553a2b0f9c3a6e94edaf3b54b11cb89ba88c20b472eb/plpygis-0.5.5.tar.gz",
"platform": null,
"description": "# plpygis\n\n`plpygis` is a pure Python module with no dependencies that can convert geometries between [Well-known binary](https://en.wikipedia.org/wiki/Well-known_binary) (WKB/EWKB), [Well-known Text](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) (WKT/EWKT) and GeoJSON representations. `plpygis` is mainly intended for use in PostgreSQL [PL/Python](https://www.postgresql.org/docs/current/plpython.html) functions to augment [PostGIS](https://postgis.net/)'s native capabilities.\n\n## Basic usage\n\n`plpygis` implements several subclasses of the `Geometry` class, such as `Point`, `LineString`, `MultiPolygon` and so on:\n\n```python\n>>> from plpygis import Point\n>>> p = Point((-124.005, 49.005), srid=4326)\n>>> print(p.ewkb)\n0101000020e6100000b81e85eb51005fc0713d0ad7a3804840\n>>> print(p.geojson)\n{'type': 'Point', 'coordinates': [-124.005, 49.005]}\n>>> p.z = 1\n>>> print(p.wkt)\nPOINT Z (-124.005 49.005 1)\n```\n\n## Usage with PostGIS\n\n`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)`.\n\n```pgsql\nCREATE OR REPLACE FUNCTION add_elevation(geom geometry(POINT))\n RETURNS geometry(POINTZ)\nAS $$\n from plpygis import Geometry\n from requests import get\n p = Geometry(geom)\n\n response = get(f'https://api.open-meteo.com/v1/elevation?longitude={p.x}&latitude={p.y}')\n if response.status_code == 200:\n content = response.json()\n p.z = content['elevation'][0]\n return p\n else:\n return None\n$$ LANGUAGE plpython3u;\n```\n\nThe `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`.\n\nThe function above can be called as part of an SQL query:\n\n```pgsql\nSELECT\n name,\n add_elevation(geom)\nFROM\n city;\n```\n\n## Documentation\n\nFull `plpygis` documentation is available at <http://plpygis.readthedocs.io/>.\n\n[![Continuous Integration](https://github.com/bosth/plpygis/workflows/tests/badge.svg)](https://github.com/bosth/plpygis/actions?query=workflow%3A%22tests%22) [![Documentation Status](https://readthedocs.org/projects/plpygis/badge/?version=latest)](http://plpygis.readthedocs.io/en/latest/?badge=latest)\n",
"bugtrack_url": null,
"license": "GPL-3.0-only",
"summary": "Python tools for PostGIS",
"version": "0.5.5",
"project_urls": {
"Documentation": "https://plpygis.readthedocs.io/",
"Homepage": "https://github.com/bosth/plpygis",
"Issues": "https://github.com/bosth/plpygis/issues"
},
"split_keywords": [
"gis",
" geospatial",
" postgis",
" postgresql",
" pl/python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "373a2abfb2e649a7c20630d3c4d2d497373098e3f7052abea3048ecef9ee330f",
"md5": "6e1a414904330751be7b3795259bbde9",
"sha256": "d8317146615f017aa150723ce907571a4c40c7ebff4b8be58ad44749938f68dc"
},
"downloads": -1,
"filename": "plpygis-0.5.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6e1a414904330751be7b3795259bbde9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 25416,
"upload_time": "2024-09-08T13:49:30",
"upload_time_iso_8601": "2024-09-08T13:49:30.624344Z",
"url": "https://files.pythonhosted.org/packages/37/3a/2abfb2e649a7c20630d3c4d2d497373098e3f7052abea3048ecef9ee330f/plpygis-0.5.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e1b6c658eb83673b14ef553a2b0f9c3a6e94edaf3b54b11cb89ba88c20b472eb",
"md5": "17d1678be01fc1828446ad723742cde7",
"sha256": "153f23ef00726b389f1a49442f8b469d07b9e62a1004024ec8322d00d1a882ed"
},
"downloads": -1,
"filename": "plpygis-0.5.5.tar.gz",
"has_sig": false,
"md5_digest": "17d1678be01fc1828446ad723742cde7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 34097,
"upload_time": "2024-09-08T13:49:32",
"upload_time_iso_8601": "2024-09-08T13:49:32.621016Z",
"url": "https://files.pythonhosted.org/packages/e1/b6/c658eb83673b14ef553a2b0f9c3a6e94edaf3b54b11cb89ba88c20b472eb/plpygis-0.5.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-08 13:49:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bosth",
"github_project": "plpygis",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "plpygis"
}