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

Python同起点相邻路径间距阈值校验异常问题求助

路径间距校验问题解决方法

问题背景

大家好,我是Python新手,特此求助。首先说明,英语不是我的母语,表述如有不当敬请谅解。
我需要计算路径之间的距离,若路径间距小于0.2米或大于0.3米则输出错误日志。目前我有4条路径数据如下:

Path_A =[
                    [101.66727417501791, 3.0590027072577914],
                    [101.66727830182118, 3.0589505710261693],
                    [101.66721266342564, 3.0589509682277645],
                    [101.66719249835002, 3.058978143007745],
                    [101.66715661762409, 3.0589486835991733],
]
Path_B= [
                    [101.66727417501791, 3.0590027072577914],
                    [101.66726593060882, 3.0589642020922305],
                    [101.66721954971536, 3.0589644833873666],
                    [101.6671948756873, 3.0589975933496878],
                    [101.6671506985855, 3.0589613252294408],
]
Path_C= [
                    [101.66727417501791, 3.0590027072577914],
                    [101.6672681056769, 3.058961477349686],
                    [101.66721819457155, 3.058961780119454],
                    [101.66719440310052, 3.0589937049008178],
                    [101.66714811902433, 3.0589557068948929],
                    [101.66700933438982, 3.0591449764349572],
                    [101.66717343271783, 3.0592761332405467],
]
Path_D= [
                    [101.66727417501791, 3.0590027072577914],
                    [101.66727043087246, 3.0589587522319492],
                    [101.66721673964463, 3.0589590780046292],
                    [101.66719393051378, 3.0589898164519278],
                    [101.66714762932398, 3.0589518049327267],
]
paths = [Path_A, Path_B, Path_C, Path_D]

注:原始paths列表重复写入两次Path_D、漏掉Path_B,此处已修正。
仅相邻且不相交的路径之间的距离校验有效,同时需忽略路径首尾的坐标点。我目前编写的代码如下:

import haversine as hs

def distance_between_two_point(coordinate_1,coordinate_2):
    if coordinate_1==coordinate_2:
        return 0
    else:
        return hs.haversine(coordinate_1,coordinate_2)*1000

def find_gap():
   content=[]
   j=0
   for path in paths:
      for a in path:
        i = 1
        for b in path:
            try:
                a1 = a[i]
                a2 = a[i+1]
                b1 = a[i]
                b2 = a[i+1]
                offset1=distance_between_two_point(a1,b1)
                offset2=distance_between_two_point(a2,b2)
                if not ((offset1 <= 0.3 and offset1>=0.2) and (offset2 <= 0.3 and offset2 >= 0.2)):
                    content.append(['The line between path are closer/further away from each other']) 
                i+=1
            except:
                pass

当前遇到的问题是:代码会对非相邻的路径也输出错误日志,比如Path A和Path B匹配输出错误的同时,也会错误输出Path C和Path D的间距错误。注意路径不一定平行,相邻的路径组合可能为A&D、A&C等任意组合。
路径示例图

问题原因

现有代码存在几个核心逻辑错误:

  • 仅遍历单条路径内部的点,没有实现两条不同路径的对比逻辑
  • 没有增加「路径是否相邻」、「路径是否相交」的前置判断,导致所有路径都会参与校验
  • 没有过滤路径首尾的坐标点
  • 变量赋值逻辑错误,b1、b2直接赋值为a路径的点,计算出来的距离永远为0

解决方法

实现思路

  • 新增两个辅助函数:分别用于判断两条线段是否相交、计算两条路径的平均最小距离用于判定是否相邻
  • 遍历所有两两不重复的路径组合,先做「相邻且不相交」的前置校验
  • 对符合条件的路径对,去掉首尾坐标点后逐段计算间距
  • 不符合0.2~0.3间距要求的记录错误日志

修复后代码

import haversine as hs
from itertools import combinations

# 计算两点距离(单位:米)
def distance_between_two_point(coordinate_1, coordinate_2):
    if coordinate_1 == coordinate_2:
        return 0
    # haversine入参为(纬度, 经度),原始坐标是(经度, 纬度),所以反转顺序
    return hs.haversine(coordinate_1[::-1], coordinate_2[::-1]) * 1000

# 判断两条线段是否相交
def segments_intersect(a1, a2, b1, b2):
    def ccw(A, B, C):
        return (B[0]-A[0])*(C[1]-A[1]) - (B[1]-A[1])*(C[0]-A[0])
    return ccw(a1,a2,b1)*ccw(a1,a2,b2) <= 0 and ccw(b1,b2,a1)*ccw(b1,b2,a2) <= 0

# 判断两条路径是否相邻(平均最小距离小于0.5米判定为相邻,可按需调整阈值)
def is_adjacent(path1, path2):
    min_dists = []
    for p1 in path1:
        min_dist = min([distance_between_two_point(p1, p2) for p2 in path2])
        min_dists.append(min_dist)
    return sum(min_dists)/len(min_dists) < 0.5

# 判断两条路径是否相交
def paths_intersect(path1, path2):
    for i in range(len(path1)-1):
        a1, a2 = path1[i], path1[i+1]
        for j in range(len(path2)-1):
            b1, b2 = path2[j], path2[j+1]
            if segments_intersect(a1, a2, b1, b2):
                return True
    return False

def find_gap(paths):
    content = []
    path_names = ["Path_A", "Path_B", "Path_C", "Path_D"]
    # 遍历所有两两不重复的路径组合
    for (idx1, path1), (idx2, path2) in combinations(enumerate(paths), 2):
        # 前置校验:相邻且不相交
        if not is_adjacent(path1, path2) or paths_intersect(path1, path2):
            continue
        # 去掉首尾坐标点
        path1_clip = path1[1:-1]
        path2_clip = path2[1:-1]
        # 取较短的路径长度作为遍历上限
        min_len = min(len(path1_clip)-1, len(path2_clip)-1)
        for i in range(min_len):
            a1, a2 = path1_clip[i], path1_clip[i+1]
            b1, b2 = path2_clip[i], path2_clip[i+1]
            offset1 = distance_between_two_point(a1, b1)
            offset2 = distance_between_two_point(a2, b2)
            # 判断间距是否符合要求
            if not (0.2 <= offset1 <= 0.3 and 0.2 <= offset2 <= 0.3):
                content.append(f"路径{path_names[idx1]}和{path_names[idx2]}在第{i+1}段间距不符合要求,端点间距分别为{round(offset1,3)}米、{round(offset2,3)}米")
    return content

if __name__ == "__main__":
    Path_A =[
        [101.66727417501791, 3.0590027072577914],
        [101.66727830182118, 3.0589505710261693],
        [101.66721266342564, 3.0589509682277645],
        [101.66719249835002, 3.058978143007745],
        [101.66715661762409, 3.0589486835991733],
    ]
    Path_B= [
        [101.66727417501791, 3.0590027072577914],
        [101.66726593060882, 3.0589642020922305],
        [101.66721954971536, 3.0589644833873666],
        [101.6671948756873, 3.0589975933496878],
        [101.6671506985855, 3.0589613252294408],
    ]
    Path_C= [
        [101.66727417501791, 3.0590027072577914],
        [101.6672681056769, 3.058961477349686],
        [101.66721819457155, 3.058961780119454],
        [101.66719440310052, 3.0589937049008178],
        [101.66714811902433, 3.0589557068948929],
        [101.66700933438982, 3.0591449764349572],
        [101.66717343271783, 3.0592761332405467],
    ]
    Path_D= [
        [101.66727417501791, 3.0590027072577914],
        [101.66727043087246, 3.0589587522319492],
        [101.66721673964463, 3.0589590780046292],
        [101.66719393051378, 3.0589898164519278],
        [101.66714762932398, 3.0589518049327267],
    ]
    paths = [Path_A, Path_B, Path_C, Path_D]
    errors = find_gap(paths)
    for err in errors:
        print(err)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 05:45:05