从 GPS 点绘制线

Plot line from GPS points

我有大约 100 个 gps 坐标列表,我想画出每个列表所构成的线。

使用散点图绘制的列表之一,看起来有点像这样:

很明显那里有一条线;

我尝试了几种方法来对 gps 位置进行排序并绘制它们:

lats = []
lngs = []
with open(filename) as f:
    for line in f:
        lat, lng = line.split("\t")
        lats.append(float(lat))
        lngs.append(float(lng))

def sort_positions(position):
    return position[0]+position[1]

positions= zip(lngs, lats)
positions = sorted(poss, key=sort_positions)
for i, positionin enumerate(positions):
    lng, lat = position
    #plt.scatter(lng, lat)
    try:
        n_lng, n_lat = positions[i+1]
        plt.plot((lng, n_lng),(lat, n_lat), "b")
    except IndexError:
        pass

plt.show()

按经度排序

def sort_positions(position):
    return position[0]

按纬度排序

def sort_positions(position):
    return position[1]

两者相加排序

def sort_positions(position):
    return position[0]+position[1]

如果线在其中一个轴上大部分是直的(latitude/longitude),它绘制得很好(仍然显示一些小的颠簸)

这是绘制精美的列表之一,按纬度排序。

我只在两点之间的距离小于 200~500 米时绘制,但由于缺少一些数据,我最终在线条中出现了漏洞。

可能是我做错了。有谁知道如何正确绘制这条线?

编辑:

回应rth回答:

蓝线是他在这道题中使用的方法,红线是使用他中的方法。

忽略红色正在关闭循环的事实。

两者都有一些局限性,首先,红色的不能处理太多的点,我不得不为两者使用 1/90 的点,蓝色的在点时会产生一些奇怪的急转弯太聚集了(图像中的黑点),而红色的那些地方有些奇怪的曲线。

我怀疑所有(不错但)奇怪的图(第一个除外)是因为您正在尝试对不允许按字母顺序排序的内容进行排序。

也没有理由单独绘制每条线段。尝试只做:

plt.plot(lngs, lats, 'b')

这是我放置的示例曲目:我没有正确缩放水平轴和垂直轴,因此绘图在垂直方向上被拉长了(太高)。

这里最简单的事情就是首先找出 GPS 坐标混淆的原因并加以纠正。

如果那不可能,我唯一能想到的就是迭代算法,它采用 xy 点并根据某些标准(例如距离、连续点的方向等)决定下一步应该去哪个点。

类似的东西,

import numpy as np
from scipy.spatial.distance import pdist, squareform

def find_gps_sorted(xy_coord, k0=0):
    """Find iteratively a continuous path from the given points xy_coord,
      starting by the point indexes by k0 """      
    N = len(xy_coord)
    distance_matrix = squareform(pdist(xy_coord, metric='euclidean'))
    mask = np.ones(N, dtype='bool')
    sorted_order = np.zeros(N, dtype=np.int)
    indices = np.arange(N)

    i = 0
    k = k0
    while True:
        sorted_order[i] = k
        mask[k] = False

        dist_k = distance_matrix[k][mask]
        indices_k = indices[mask]

        if not len(indices_k):
            break

        # find next unused closest point
        k = indices_k[np.argmin(dist_k)]
        # you could also add some criterion here on the direction between consecutive points etc.
        i += 1
    return sorted_order, xy_coord[sorted_order]

您可以用作,

 xy_coord = np.random.randn(10, 2)
 sorted_order, xy_coord_sorted = find_gps_sorted(xy_coord, k0=0)

尽管这可能需要扩展。

例如,您可以说除了最近的距离标准之外,您不希望轨迹转弯超过 90°(如果这对公共汽车有意义)并且在每次迭代时忽略这些点.本质上,您可以在此算法中添加足够的约束以获得您正在寻找的结果。

编辑: 由于 GPS 坐标有一些不确定性,最后一步可能是使用 scipy.interpolate.splprep 并使用 s 参数(见相关问题,

将每个坐标放入一个数组(纬度、经度)中。 将所有坐标放入另一个数组 集合(array1,array2,... arrayn)

排序集合数组然后绘制。