在Python 3上使用PostGIS

Mil*_*Bii 14 python postgresql postgis

我正在使用Python 3,需要使用postGIS扩展连接到postGre.我打算使用psycopg2驱动程序.
这个PPyGIS是我发现的唯一扩展,但它适用于python 2.7而不是3.3.0.
任何人都知道在3.3.0上工作的解决方案?

Mik*_*e T 11

如果您对客户端(Python)上的几何对象没有任何兴趣,psycopg2可以使用具有几何访问器的本机数据类型或其他GIS 输出格式(GeoJSON)获取最基本的信息.让服务器(PostgreSQL/PostGIS)做艰苦的工作.

下面是一个随机示例,将GeoJSON返回到距离感兴趣点1公里范围内的形状:

import psycopg2
conn = psycopg2.connect(database='postgis', user='postgres')
curs = conn.cursor()

# Find the distance within 1 km of point-of-interest
poi = (-124.3, 53.2)  # longitude, latitude

# Table 'my_points' has a geography column 'geog'
curs.execute("""\
SELECT gid, ST_AsGeoJSON(geog), ST_Distance(geog, poi)
FROM my_points, (SELECT ST_MakePoint(%s, %s)::geography AS poi) AS f
WHERE ST_DWithin(geog, poi, 1000);""", poi)

for row in curs.fetchall():
    print(row)
Run Code Online (Sandbox Code Playgroud)