微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何找到用python捕获符号值的两条线段之间的最短距离

如何解决如何找到用python捕获符号值的两条线段之间的最短距离

我有一个 pandas dataframe 的形式:

    benchmark_x benchmark_y ref_point_x ref_point_y
0   525039.140  175445.518  525039.145  175445.539
1   525039.022  175445.542  525039.032  175445.568
2   525038.944  175445.558  525038.954  175445.588
3   525038.855  175445.576  525038.859  175445.576
4   525038.797  175445.587  525038.794  175445.559
5   525038.689  175445.609  525038.679  175445.551
6   525038.551  175445.637  525038.544  175445.577
7   525038.473  175445.653  525038.459  175445.594
8   525038.385  175445.670  525038.374  175445.610
9   525038.306  175445.686  525038.289  175445.626

我试图找到从线到基准的最短距离,如果线高于基准,则距离为正,如果低于基准,距离为负。见下图:

enter image description here

我像这样使用了 KDTree 中的 scipy

from scipy.spatial import KDTree
tree=KDTree(df[["benchmark_x","benchmark_y"]])
test = df.apply(lambda row: tree.query(row[["ref_point_x","ref_point_y"]]),axis=1)
test=test.apply(pd.Series,index=["distance","index"])

这似乎有效,只是由于线低于基准而无法捕获负值。

解决方法

# recreating your example
columns = "benchmark_x benchmark_y ref_point_x ref_point_y".split(" ")
data = """525039.140  175445.518  525039.145  175445.539
525039.022  175445.542  525039.032  175445.568
525038.944  175445.558  525038.954  175445.588
525038.855  175445.576  525038.859  175445.576
525038.797  175445.587  525038.794  175445.559
525038.689  175445.609  525038.679  175445.551
525038.551  175445.637  525038.544  175445.577
525038.473  175445.653  525038.459  175445.594
525038.385  175445.670  525038.374  175445.610
525038.306  175445.686  525038.289  175445.626"""
data = [float(x) for x in data.replace("\n"," ").split(" ") if len(x)>0]
arr = np.array(data).reshape(-1,4)
df = pd.DataFrame(arr,columns=columns)

# adding your two new columns to the df
from scipy.spatial import KDTree
tree=KDTree(df[["benchmark_x","benchmark_y"]])
df["distance"],df["index"] = tree.query(df[["ref_point_x","ref_point_y"]])

现在要比较一条线是否在另一条线上,我们必须在相同的 x 位置评估 y。因此,我们需要为另一条线的 x 位置插入 y 点。

df = df.sort_values("ref_point_x") # sorting is required for interpolation
xy_refpoint = df[["ref_point_x","ref_point_y"]].values
df["ref_point_y_at_benchmark_x"] = np.interp(df["benchmark_x"],xy_refpoint[:,0],1])

最后可以评估和应用您的标准:

df["distance"] = np.where(df["ref_point_y_at_benchmark_x"] < df["benchmark_y"],-df["distance"],df["distance"])
# or change the < to <,>,<=,>= as you wish

enter image description here enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?