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)
4条答案
按热度按时间r1zk6ea11#
如果您没有在客户端(Python)对几何对象做任何花哨的操作,psycopg 2可以使用带有几何访问器的本地数据类型或其他GIS输出格式(如GeoJSON)获取最基本的信息。让服务器(PostgreSQL/PostGIS)来完成这些艰巨的工作。
下面是一个随机的例子,将GeoJSON返回到距离感兴趣点1公里以内的形状:
字符串
vtwuwzda2#
您实际上可以使用Shapely或GDAL/OGR,但这两个库都有一长串依赖项。
如果您只有很少的用例,您也可以自己实现一个小的协议,基于super slick pygeoif库,如下例所示
字符串
6yoyoihd3#
由于提出了这个问题,Geopandas软件包添加了
字符串
它将从具有几何列的SQL表中检索geodaframe
http://geopandas.org/reference.html#geopandas.GeoDataFrame.from_postgis
afdcj2ne4#
最简单的方法是以wkb的形式给予几何图形。有了它,你可以取消所有psycopg2的函数,如execute_values
字符串