简体   繁体   English

检查点是否在多边形内

[英]Checking if points are inside of polygons

I have two datasets in geoJSON format.我有两个 geoJSON 格式的数据集。 Using geoPandas, on the one hand I have a dataset with coordinates of various points in a certain city.使用 geoPandas,一方面我有一个数据集,其中包含某个城市中各个点的坐标。

Table gdf:表gdf:

╔═══╦════════╦══════════╦══════════════════════╦═════════╦══════════╦══════════════════════════╗
║   ║ id     ║ layer_id ║ title                ║ lon     ║ lat      ║ geometry                 ║
╠═══╬════════╬══════════╬══════════════════════╬═════════╬══════════╬══════════════════════════╣
║ 0 ║ 83969  ║ 12       ║ Garces               ║ 2.15351 ║ 41.37926 ║ POINT (2.15351 41.37926) ║
╠═══╬════════╬══════════╬══════════════════════╬═════════╬══════════╬══════════════════════════╣
║ 1 ║ 86258  ║ 146      ║ Ritsch               ║ 2.16235 ║ 41.38429 ║ POINT (2.16235 41.38429) ║
╠═══╬════════╬══════════╬══════════════════════╬═════════╬══════════╬══════════════════════════╣
║ 2 ║ 83964  ║ 40       ║ Lunch & Catering Bar ║ 2.15368 ║ 41.37913 ║ POINT (2.15368 41.37913) ║
╠═══╬════════╬══════════╬══════════════════════╬═════════╬══════════╬══════════════════════════╣
║ 3 ║ 83970  ║ 8        ║ Galaxia              ║ 2.15343 ║ 41.37932 ║ POINT (2.15343 41.37932) ║
╠═══╬════════╬══════════╬══════════════════════╬═════════╬══════════╬══════════════════════════╣
║ 4 ║ 74866  ║ 40       ║ Celler de l`Abi      ║ 2.14207 ║ 41.3694  ║ POINT (2.14207 41.36941) ║
╚═══╩════════╩══════════╩══════════════════════╩═════════╩══════════╩══════════════════════════╝

On the other hand, I have another dataset where the polygons of different areas are located in the same city.另一方面,我有另一个数据集,其中不同区域的多边形位于同一个城市。

Table polys:表多边形:

╔═══╦═══════╦═════╦═══════════════════════════════════════════════════╗
║   ║ Name  ║ ... ║ geometry                                          ║
╠═══╬═══════╬═════╬═══════════════════════════════════════════════════╣
║ 0 ║ aoi_1 ║ ... ║ POLYGON Z ((2.13049 41.38221 0.00000, 2.13101 ... ║
╠═══╬═══════╬═════╬═══════════════════════════════════════════════════╣
║ 1 ║ aoi_2 ║ ... ║ POLYGON Z ((2.14463 41.39321 0.00000, 2.14495 ... ║
╠═══╬═══════╬═════╬═══════════════════════════════════════════════════╣
║ 2 ║ aoi_3 ║ ... ║ POLYGON Z ((2.14592 41.39374 0.00000, 2.14613 ... ║
╠═══╬═══════╬═════╬═══════════════════════════════════════════════════╣
║ 3 ║ aoi_4 ║ ... ║ POLYGON Z ((2.14860 41.39433 0.00000, 2.14884 ... ║
╠═══╬═══════╬═════╬═══════════════════════════════════════════════════╣
║ 4 ║ aoi_5 ║ ... ║ POLYGON Z ((2.14845 41.39443 0.00000, 2.14873 ... ║
╚═══╩═══════╩═════╩═══════════════════════════════════════════════════╝

What I want to do is determine what points lie within each of the polygons and generate a new dataset.我想要做的是确定每个多边形内的哪些点并生成一个新的数据集。 I would also like to know the most optimal way to do it because, I have around 100 polygons and around 100K points to verify in the polygons.我还想知道最优化的方法,因为我有大约 100 个多边形和大约 100K 个点要在多边形中进行验证。

Using Shapely and property contains , what I am trying to do is the following::使用Shapely和 property contains ,我想做的是以下内容:

inside = gdf[gdf.apply(lambda row: polys.contains(Point(row.lon, row.lat)), axis=1)] 

The problem is that I don't want to get only the points within a single polygon (which the code above does) but all the points and know what polygons they are in.问题是我不想只获取单个多边形内的点(上面的代码就是这样做的),而是获取所有点并知道它们在哪些多边形中。

我找到了我要找的东西,它是这样的:

points_inside = gpd.sjoin(gdf, polys[['Name', 'geometry']], op='within')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM