Pyscopg帮助程序与Postgis一起工作。
psycopg-postgis2的Python项目详细描述
[![圆ci](https://img.shields.io/circleci/project/yohanboniface/psycopg-postgis.svg)](https://circleci.com/gh/yohanboniface/psycopg-postgis)[![pypi](https://img.shields.io/pypi/v/psycopg-postgis.svg)](https://pypi.python.org/pypi/psycopg-postgis)[![pypi](https://img.shields.io/pypi/pyversions/psycopg-postgis.svg)](https://pypi.python.org/pypi/psycopg-postgis)[![PYPI](https://img.shields.io/pypi/implementation/psycopg-postgis.svg)](https://pypi.python.org/pypi/psycopg-postgis)[![pypi](https://img.shields.io/pypi/status/psycopg-postgis.svg)](https://pypi.python.org/pypi/psycopg-postgis)
#心理学博士后
psycopg2的postgis帮助程序。
##安装
pip install psycopg-postgis
如果需要编译版本,请首先安装cython
pip install cython pip install psycopg-postgis
##用法
您需要注册分机:
> import postgis > postgis.register(mydatabase.get_cursor())
然后您可以将python geometries实例传递给psycopg:
> cursor.execute(‘INSERT INTO table (geom) VALUES (%s)’, [Point(x=1, y=2, srid=4326)])
并以python几何实例的形式检索数据:
> cursor.execute(‘SELECT geom FROM points LIMIT 1’) > geom = cursor.fetchone()[0] > geom <Point POINT(1.0 2.0)>
##示例
> import psycopg2 > from postgis import register, LineString > db = psycopg2.connect(dbname=”test”) > cursor = db.cursor() > register(cursor) > cursor.execute(‘CREATE TABLE IF NOT EXISTS mytable (“geom” geometry(LineString) NOT NULL)’) > cursor.execute(‘INSERT INTO mytable (geom) VALUES (%s)’, [LineString([(1, 2), (3, 4)], srid=4326)]) > cursor.execute(‘SELECT geom FROM mytable LIMIT 1’) > geom = cursor.fetchone()[0] > geom <LineString LINESTRING(1.0 2.0, 3.0 4.0)> > geom[0] <Point POINT(1.0 2.0)> > geom.coords ((1.0, 2.0), (3.0, 4.0)) > geom.geojson {‘coordinates’: ((1.0, 2.0), (3.0, 4.0)), ‘type’: ‘LineString’} > str(geom.geojson) ‘{“type”: “LineString”, “coordinates”: [[1, 2], [3, 4]]}’