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

如何对四个经纬度坐标点排序以正确连接绘制平行四边形?

问题原因

  1. 原始提取的坐标顺序本身不符合平行四边形的环绕逻辑,你给出的示例数据提取后得到的4个点顺序为:
    • 点0:(51.071833, 6.237204)
    • 点1:(51.071836, 6.237195)
    • 点2:(51.071833, 6.237195)
    • 点3:(51.071836, 6.237204)
      直接按0→1→2→3的顺序连接,会先连对角线再连边,必然出现交叉。另外你原提取代码中for col in test的test应为df,否则会运行报错。
  2. 最近邻+DFS的方案不适用于凸四边形排序:K=2的最近邻匹配会混淆相邻顶点和对角顶点,DFS遍历也无法保证输出顺时针/逆时针的环绕顺序,也不会自动补全闭环。

解决方案

方案1:手动修正顺序(适配当前示例数据)

直接按逆时针逻辑调整顺序,末尾补第一个点实现闭环即可:

# 修正列筛选的变量名错误
xx = df[[col for col in df.columns if col.startswith('lat')]].stack().to_list() 
yy = df[[col for col in df.columns if col.startswith('lon')]].stack().to_list() 

# 调整为逆时针环绕顺序,末尾补起点闭环
order = [0, 2, 1, 3, 0]
sorted_xx = [xx[i] for i in order]
sorted_yy = [yy[i] for i in order]

plt.scatter(xx, yy)
plt.plot(sorted_xx, sorted_yy)
plt.show()

方案2:通用凸包自动排序(适配任意凸四边形/平行四边形)

如果需要适配任意四个点组成的凸四边形,不用手动指定顺序,可以用凸包算法自动计算环绕顺序:

import numpy as np
from scipy.spatial import ConvexHull

xx = df[[col for col in df.columns if col.startswith('lat')]].stack().to_list() 
yy = df[[col for col in df.columns if col.startswith('lon')]].stack().to_list() 
points = np.c_[xx, yy]

# 计算凸包,自动返回顺时针的顶点环绕顺序
hull = ConvexHull(points)
# 补全起点实现闭环
order = hull.vertices.tolist() + [hull.vertices[0]]

sorted_xx = [xx[i] for i in order]
sorted_yy = [yy[i] for i in order]

plt.scatter(xx, yy)
plt.plot(sorted_xx, sorted_yy)
plt.show()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 12:51:00