[英]Point in Polygon with geoJSON in Python
I have a geoJSON database with lots of polygons (census tracts specifically) and I have lots of long,lat points. 我有一个包含大量多边形的geoJSON数据库(特别是人口普查区),我有很多长的纬度点。
I am hoping that there would exist an efficient Python code to identify which census tract a given coordinate is in, however so far my googling hasn't revealed anything. 我希望有一个有效的Python代码来识别给定坐标所在的人口普查区域,但到目前为止我的谷歌搜索还没有透露任何内容。
Thanks! 谢谢!
I found an interesting article describing how to do exactly what you are looking to do. 我发现了一篇有趣的文章,描述了如何完成你想做的事情。
TL;DR: Use Shapely TL; DR:使用Shapely
You will find this code at the end of the article: 您将在本文末尾找到此代码:
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
A great option for working with these types of data is PostGIS , a spatial database extender for PostgreSQL . 使用这些类型数据的一个很好的选择是PostGIS ,它是PostgreSQL的空间数据库扩展器。 I personally keep all of my geo data in a PostGIS database, and then reference it in python using
psycopg2
. 我个人将所有地理数据保存在PostGIS数据库中,然后使用
psycopg2
在python中引用它。 I know it's not pure python, but it's got unbelievable performance benefits (discussed below) over pure python. 我知道这不是纯粹的python,但它在纯python上有令人难以置信的性能优势(如下所述)。
PostGIS has functionality built in to determine if a point or shape is within another shape. PostGIS具有内置功能,以确定点或形状是否在另一个形状内。 The good documentation on the ST_Within function expands upon this simple example:
关于ST_Within函数的优秀文档扩展了这个简单的例子:
SELECT
ST_WITHIN({YOUR_POINT},boundary)
FROM census;
-- returns true or false for each of your tracts
The benifit you'll gain from PostGIS that you likely won't achieve elsewhere is indexing, which can improve your speed 1,000x [1], making it better than even the best written C program (unless the C program also creates an index for your data). 您可能从其他地方获得的PostGIS中获得的好处是索引,这可以提高您的速度1,000x [1],使其比最好的书面C程序更好(除非C程序还创建了一个索引你的数据)。 The database, when properly setup, will cache information about your tracts, and when you ask if a point is within a tract, it won't have to search everything... it can take advantage of it's index.
如果设置正确,数据库将缓存有关您的小册子的信息,当您询问某个点是否在某个区域内时,它不必搜索所有内容......它可以利用它的索引。
Getting data into and out of PostGRES is pretty simple. 将数据输入和输出PostGRES非常简单。 A great tutorial that will walk you through the basics of PostGIS with sample datasets not too different from yours can be found here.
您可以在此处找到一个很棒的教程,它将引导您完成PostGIS的基础知识,其中的样本数据集与您的样本数据集并无太大差别。 It's reasonably long, but if you're new to PostGIS (as I was), you'll be very entertained and excited the entire time:
这是相当长的,但如果你是PostGIS的新手(就像我一样),你会一直很受欢迎和兴奋:
http://workshops.boundlessgeo.com/postgis-intro/ http://workshops.boundlessgeo.com/postgis-intro/
[1] Indexing decreased a nearest neighbor search in one of my huge databases (20 m from 53 seconds to 8.2 milliseconds. [1]索引减少了我的一个庞大数据库中的最近邻搜索(从53秒到8.2毫秒,20米)。
One cannot have really fast geometric code in Python. 人们不可能在Python中拥有真正快速的几何代码。 Instead the usual approach is to use fast C/C++ library with Python wrappers.
相反,通常的方法是使用带有Python包装器的快速C / C ++库。
For example, you can start with CGAL - a very comprehensive C++ geometric library. 例如,您可以从CGAL开始 - 一个非常全面的C ++几何库。 It has Python bindings for most of its routines, see the link http://code.google.com/p/cgal-bindings/ .
它的大部分例程都有Python绑定,请参阅http://code.google.com/p/cgal-bindings/链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.