如何使用带线的 GeoPandas 空间索引?

How to use GeoPandas Spatial Index with lines?

我试图找到离一堆点最近的线(大约 240 亿个点,400 万条线)。点存在于一个 GeoDataFrame 中,而线存在于另一个 GeoDataFrame 中。我试着按照这个:https://github.com/geopandas/geopandas/issues/140,并这样做:

lines_sidx = lines_df['geom'].sindex
[list(lines_sidx.intersection((points.loc[i,'geom'].y, points.loc[i,'geom'].x))) for i in range(len(points))]

而这只是 returns 一个空列表。这是怎么回事?

(请注意,我将其应用于两个数据集中的前 100 条线和点)。

您的问题以您尝试执行最近邻查询的上下文开头,但您的问题本身询问的是该 geopandas 交集代码块中发生了什么。我将尝试解决您的问题而不是它的前言,因为它们似乎不一致。看起来您的交集代码逻辑已关闭。将 rtree 与空间交集一起使用的要点是,您首先找到与您的索引的可能匹配项(一些误报,但没有漏报),然后找到精确匹配项。

类似这样的事情,如 geopandas r-tree tutorial:

所示
spatial_index = gdf.sindex
possible_matches_index = list(spatial_index.intersection(polygon.bounds))
possible_matches = gdf.iloc[possible_matches_index]
precise_matches = possible_matches[possible_matches.intersects(polygon)]

如果您尝试使用一组点和一组线进行最近邻搜索,则可能 none 个要素相交,这可能 return 您的空集结果。