例如,GPS坐标数组:
GPSS = [{"Lat":40.641099,"Lon": -73.917094},{"Lat":40.60442,"Lon": -74.054873},{"Lat":40.779582,"Lon": -73.920213},{"Lat":40.651616,"Lon": -73.89097},{"Lat":40.755183,"Lon": -73.846248}]
我已经为每个可能的组合计算了以下距离:
Distances = [{'GPSS': [0, 1], 'Distance': 12.34895151892164}, {'GPSS': [0, 2], 'Distance': 15.380561959360797}, {'GPSS': [0, 3], 'Distance': 2.499303143635897}, {'GPSS': [0, 4], 'Distance': 14.012560598709298}, {'GPSS': [1, 2], 'Distance': 22.53687775052488}, {'GPSS': [1, 3], 'Distance': 14.824576927209662}, {'GPSS': [1, 4], 'Distance': 24.318038568441654}, {'GPSS': [2, 3], 'Distance': 14.423642658224264}, {'GPSS': [2, 4], 'Distance': 6.807346029310139}, {'GPSS': [3, 4], 'Distance': 12.106031672624894}]
0.1=参考40.641099、-73.917094和40.60442、-74.054873
1,4=40.641099,-73.917094和40.755183,-73.846248
我现在想找出访问每一组坐标的最短距离(路线),所以它很可能不是点0到1到2到3到4。 但是大概是1到3到4到2到0。你知道吗
我该如何完成这样的任务?
就我所知:
for index, d in enumerate(Distances):
print(d['GPSS'])
Total = d['Distance']
Start = d['GPSS'][1] #[0]
CheckPoints = []
CheckPoints.append(d['GPSS'][0])
CheckPoints.append(d['GPSS'][1])
for index2, d2 in enumerate(Distances):
if index != index2:
if Start == d2['GPSS'][0]: #0-1, 1-2, 2-3
Total += d2['Distance']
Start += 1
if d2['GPSS'][0] not in CheckPoints:
CheckPoints.append(d2['GPSS'][0])
if d2['GPSS'][1] not in CheckPoints:
CheckPoints.append(d2['GPSS'][1])
#print(CheckPoints)
print("+"+str(d2['Distance'])+" = "+str(Total)+" | "+str(Start)+" - "+str(d2['GPSS']))
if len(CheckPoints) <= len(GPSS)-1: #GPPS - is from above
for x in range(len(GPSS)-1):
if x not in CheckPoints:
for d3 in Distances:
if d3['GPSS'][0] == x and d3['GPSS'][1] == CheckPoints[-1]:
print("HERE")
print(d3)
Total += d3['Distance']
break
print(Total)
任何帮助都将不胜感激。 谢谢
最好的方法是创建一个图。如果您不知道这是什么,您应该查找它,因为它是一个非常重要的数据结构。您可能还需要知道要完全理解以下代码是什么。Python没有内置的图形,因此您需要创建自己的图形。你知道吗
您需要的图形类型是一个无向加权图,其中所有节点或GPS坐标相互连接。然后,您可以使用“Dijkstra算法”的形式对图形进行排序,以找到到所有点的最短路径。你知道吗
下面是您所寻找的实现。不过,我将其编码为使用包含成对坐标列表的列表。它还包括一个驱动程序driver(),您可以调用来测试它。你知道吗
我写得很快,并没有把它作为一个类来编写,但在现实世界中,你绝对应该这样做。你知道吗
请注意,当您运行driver函数时,它将执行代码并打印出所提供坐标列表的所有可能路径及其权重。”在你的例子中,“重量”指的是两个点之间的距离。打印的列表显示了它所走的路径,其中“1”表示坐标列表索引“0”处的一对点。列表中的下一个数字是它下一个指向的点对。你知道吗
如果您还有任何问题,请随时询问
编辑: 以下是调用驱动程序函数时的输出:
相关问题 更多 >
编程相关推荐