Checking if a point is contained in a polygon/multipolygon - for many points(检查点是否包含在多边形/多多边形中-针对多个点)
本文介绍了检查点是否包含在多边形/多多边形中-针对多个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个2D地图,它被分成矩形网格(大约有45,000个)和一些从shapefile派生的多边形/多多边形(我目前使用shapefile库pyshp读取它们)。不幸的是,这些多边形中有几个相当复杂,由大量的点组成(其中一个点有64万个点),可能会有洞。
我尝试做的是为每个多边形检查哪些单元中心(我的网格的单元)落在那个特定的多边形内。然而,拥有大约45,000个细胞中心和150个多边形,使用Shapely检查一切都需要相当长的时间。这就是我所做的,或多或少:
# nx and ny are the number of cells in the x and y direction respectively
# x, y are the coordinates of the cell centers in the grid - numpy arrays
# shapes is a list of shapely shapes - either Polygon or MultiPolygon
# Point is a shapely.geometry.Point class
for j in range(ny):
for i in range(nx):
p = Point([x[i], y[j]])
for s in shapes:
if s.contains(p):
# The shape s contains the point p - remove the cell
根据所涉及的多边形,每次检查的时间在200微秒到80毫秒之间,600多万次检查的运行时间很容易进入小时。
我想一定有更聪明的方法来处理这个问题--如果可能的话,我宁愿保持形状,但任何建议都是非常感谢的。
提前感谢您。
推荐答案
如果您希望执行较少的比较操作,可以尝试使用形状字符串树功能。请考虑以下代码:
from shapely.strtree import STRtree
from shapely.geometry import Polygon, Point
# this is the grid. It includes a point for each rectangle center.
points = [Point(i, j) for i in range(10) for j in range(10)]
tree = STRtree(points). # create a 'database' of points
# this is one of the polygons.
p = Polygon([[0.5, 0], [2, 0.2], [3, 4], [0.8, 0.5], [0.5, 0]])
res = tree.query(p). # run the query (a single shot).
# do something with the results
print([o.wkt for o in res])
此代码的结果为:
['POINT (3 0)', 'POINT (2 0)', 'POINT (1 0)', 'POINT (1 1)', 'POINT (3 1)',
'POINT (2 1)', 'POINT (2 2)', 'POINT (3 2)', 'POINT (1 2)', 'POINT (2 3)',
'POINT (3 3)', 'POINT (1 3)', 'POINT (2 4)', 'POINT (1 4)', 'POINT (3 4)']
这篇关于检查点是否包含在多边形/多多边形中-针对多个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:检查点是否包含在多边形/多多边形中-针对多个点
基础教程推荐
猜你喜欢
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
