【发布时间】:2020-06-09 10:25:44
【问题描述】:
我有一个 2D 地图,分为一个矩形网格 - 其中大约 45,000 个 - 以及一些从 shapefile 派生的多边形/多多边形(我目前使用 shapefile 库 pyshp 读取它们)。不幸的是,其中一些多边形相当复杂,由大量点组成(其中一个有 640,000 个点)并且其中可能有孔。
我要做的是检查每个多边形的哪些单元格中心(我的网格的单元格)位于该特定多边形内。然而,拥有大约 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 万次检查,运行时间很容易进入数小时。
我想必须有更聪明的方法来处理这个问题 - 如果可能的话,我宁愿保持身材,但任何建议都非常感谢。
提前谢谢你。
【问题讨论】:
-
也许你可以做一些初步的消除?我会研究确定每个形状的边界矩形(这只需要发生一次),然后执行快速检查(4 次操作)以验证一个点是否属于边界矩形。如果它不能属于边界矩形,那么验证它是否属于底层形状是没有意义的。