我有一个包含许多多边形(具体来说是普查区域)的geoJSON数据库和许多经纬度点。
我希望存在一种高效的Python代码,可以确定给定坐标所在的普查区域,但目前我的谷歌搜索并没有发现任何有用信息。
谢谢!
我找到了一篇有趣的文章,描述了如何做你想做的事情。
简而言之:使用Shapely
你会在文章结尾找到这段代码:
import json
from shapely.geometry import shape, Point
# depending on your version, use: from shapely.geometry import shape, Point
# load GeoJSON file containing sectors
with open('sectors.json') as f:
js = json.load(f)
# construct point based on lon/lat returned by geocoder
point = Point(-122.7924463, 45.4519896)
# check each polygon to see if it contains the point
for feature in js['features']:
polygon = shape(feature['geometry'])
if polygon.contains(point):
print 'Found containing polygon:', feature
psycopg2
在python中引用它。虽然不是纯Python,但与纯Python相比具有惊人的性能优势(下文讨论)。SELECT
ST_WITHIN({YOUR_POINT},boundary)
FROM census;
-- returns true or false for each of your tracts
在Python中无法编写真正快速的几何代码。相反,通常的方法是使用带有Python包装器的快速C/C++库。
例如,您可以从CGAL开始 - 一个非常全面的C++几何库。它具有大多数例程的Python绑定,请参见链接http://code.google.com/p/cgal-bindings/。
open
,例如:with open('sectors.json', 'r') as f:
。 - Kirkman14