擅长:python、mysql、java
<p>如果您对客户端(Python)上的几何体对象不感兴趣,那么psycopg2可以使用带有<a href="http://postgis.net/docs/manual-2.0/reference.html#Geometry_Accessors">geometry accessors</a>的本机数据类型或其他类似于<a href="http://www.geojson.org/">GeoJSON</a>的GIS获得最基本的信息。让服务器(PostgreSQL/PostGIS)来做这项艰苦的工作。</p>
<p>下面是一个随机示例,用于将GeoJSON返回到距关注点1公里内的形状:</p>
<pre><code>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)
</code></pre>