我需要将几十万个节点映射到它们最近的邻居,作为几万个其他节点的集合。当然,我会使用空间索引来加速这个过程。我以前通过生成较小节点集的Voronoi镶嵌,并使用MySQL空间查询来确定第一个集的哪些节点落入与第二个集的节点相关联的区域中,从而能够非常快速地做到这一点。它能够在短短几分钟内运行。从那以后,我离开了MySQL环境,想用python完成整个过程。我转向shapely包提供的流行的STRtree空间索引。然而,我发现这个索引非常慢。我尝试使用具有节点集的最近几何查询以及具有Voronoi区域的几何相交查询,但仅获得每秒51.787个匹配节点的速度,这将需要大约4.5h来运行我处理的每组节点。为什么shapely这么慢?还是我用错了?
以下是一些代码片段:
class Network:
...
def load_network(self, planspath):
log.info('Fetching temperatures.')
temperatures = self.fetch_temperatures()
log.info('Fetching centroids.')
centroids = self.fetch_centroids()
log.info('Fetching links.')
links = self.fetch_links()
Centroid.steps = len(next(iter(temperatures.values())))
log.info('Building spatial index (strtree) from centroids.')
points = []
for centroid in centroids.values():
uuid = centroid[0]
self.centroids[uuid] = Centroid(temperatures[centroid[1]])
point = loads(centroid[2])
setattr(point, 'id', uuid)
points.append(point)
tree = STRtree(points)
log.info('Mapping links to centroids.')
for link in links:
node = loads(link[4])
point = tree.nearest(node)
self.links[link[0]] = Link(link[1], link[2], self.centroids[point.id])
log.info('Loading network routes from output plans file.')
self.routes = self.fetch_routes(planspath)发布于 2021-03-02 10:13:23
糟糕的性能是Shapely中的一个bug造成的。已提交修复程序,并将在下一个版本中提供。
https://stackoverflow.com/questions/61025297
复制相似问题