我有一张2D的地图,被分成一个矩形网格 - 大约有45,000个矩形 - 和一些从shapefiles导出的多边形/多部分多边形(我目前使用shapefile库pyshp读取它们)。不幸的是,其中一些多边形相当复杂,由大量点组成(其中一个多边形有640,000个点),并且可能有洞。
我正在尝试做的是,检查每个多边形内有哪些单元格中心(即我的网格单元格)。然而,检查大约45,000个单元格中心和150个多边形需要花费相当长的时间来使用Shapely进行检查。这就是我现在所做的事情,或多或少:
我正在尝试做的是,检查每个多边形内有哪些单元格中心(即我的网格单元格)。然而,检查大约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万次的检查,因此运行时间会轻松超过几个小时。
我猜肯定有更智能的方法来处理这个问题 - 如果可能的话,我宁愿继续使用shapely,但任何建议都非常感谢。
提前感谢您。