简体   繁体   English

使用Python中的geoJSON指向Polygon

[英]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.

相关问题 如何使用geojson和shapely确定一个点是否在多边形内 - how to determine if a point is inside a polygon using geojson and shapely 将 Python 中的列转换为多边形以执行多边形中的点 - Convert Column to Polygon in Python to perform Point in Polygon python 点内多边形(点云数据) - python point inside polygon (point cloud data) Python + Mysql:将geojson多边形值插入mysql sptial列 - Python + Mysql : Insert geojson polygon value to mysql sptial column 从 GeoJSON 加载的 Python 3.X Shapely Polygon - Python 3.X Shapely Polygon loaded from GeoJSON 如何使用 Python 向 Folium 中的每个 GeoJSON 多边形添加唯一的弹出窗口 - How to add unique popups to each GeoJSON polygon in Folium using Python GeoJSON 和 MuliPolygon:将多边形数据整形为数据框(python,匀称) - GeoJSON and MuliPolygon: shape polygon data into dataframe (python, shapely) Python PIL ImageDraw.polygon 不绘制多边形,如果它是一条线或一个点 - Python PIL ImageDraw.polygon not drawing the polygon if it is a line or a point 如何将具有许多几何列(多边形、点和线串)的 geosdataframe 保存到 geojson 文件(或 csv 文件)? - How to save a geosdataframe with many geomertry columns ( polygon, point and linestring) to a geojson file (or a csv file)? 使用 Python GeoPandas 进行多边形点分析 - Point-in-Polygon Analysis Using Python GeoPandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM