You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取GeoDataFrame中LineString几何图形的接触点坐标?

获取LineString接触点的高效方法

你已经通过空间索引筛选出了接触的图形对,接下来可以通过以下高效步骤提取接触点坐标:

  • 去重并过滤无效对:query_bulk会返回所有满足条件的索引对,包括重复对(如A→B和B→A)以及自接触的情况(如果存在),先过滤掉这些冗余数据:
# 过滤掉自接触和重复对
valid_pairs = heads < tails
heads_clean = heads[valid_pairs]
tails_clean = tails[valid_pairs]
  • 提取对应几何对并计算交点:利用Shapely的intersection方法直接计算每对接触LineString的交点,这一步用列表推导式比循环更高效:
from shapely.geometry import Point

# 获取对应的几何图形
geoms_head = data.geometry.iloc[heads_clean].values
geoms_tail = data.geometry.iloc[tails_clean].values

# 计算每对的交点,并提取坐标
touch_points = []
for g1, g2 in zip(geoms_head, geoms_tail):
    intersection = g1.intersection(g2)
    # 因为是touches关系,交点应该是单个Point或MultiPoint(极少数情况)
    if intersection.geom_type == 'Point':
        touch_points.append((intersection.x, intersection.y))
    elif intersection.geom_type == 'MultiPoint':
        # 若为多点,逐个提取坐标
        touch_points.extend([(p.x, p.y) for p in intersection.geoms])
  • 整理结果(可选):如果需要把结果和原数据关联,可以创建一个包含接触对索引和坐标的DataFrame:
import pandas as pd

result = pd.DataFrame({
    'index_head': heads_clean,
    'index_tail': tails_clean,
    'touch_x': [p[0] for p in touch_points],
    'touch_y': [p[1] for p in touch_points]
})

优化说明

  • 空间索引已经帮你缩小了计算范围,只需要处理确认接触的图形对,避免了全量几何计算
  • 直接操作Shapely几何对象的values属性,跳过GeoSeries的包装,提升速度
  • 提前判断几何类型,避免不必要的计算开销

内容的提问来源于stack exchange,提问作者Kewitschka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:35:29