Python 从字典中的点查找大圆距离

Python 从字典中的点查找大圆距离,python,dictionary,coordinates,Python,Dictionary,Coordinates,我有下面的代码来计算地图上点之间的距离。我的目标是做到以下几点: 有一个起点和位置字典中的位置 循环浏览字典并计算与所有对象的距离 指向起点 找到距离起点最小的位置 然后将该位置与起点配对,形成一对节点 连接边缘 完成此操作后,将最小位置作为新的起点 并将其从位置字典中删除 然后,我将返回到上一步,了解其余的要点 目前,我可以获得从第一个起点的距离,但无法在“位置”字典中循环通过其余点 非常感谢您的建议 from math import atan2, cos, sin, sqrt, radi

我有下面的代码来计算地图上点之间的距离。我的目标是做到以下几点:

  • 有一个起点和位置字典中的位置

  • 循环浏览字典并计算与所有对象的距离 指向起点

  • 找到距离起点最小的位置

  • 然后将该位置与起点配对,形成一对节点 连接边缘

  • 完成此操作后,将最小位置作为新的起点 并将其从位置字典中删除

然后,我将返回到上一步,了解其余的要点

目前,我可以获得从第一个起点的距离,但无法在“位置”字典中循环通过其余点

非常感谢您的建议

from math import atan2, cos, sin, sqrt, radians

start = (43.82846160000000000000, -79.53560419999997000000)

locations = {
        'one':(43.65162010000000000000, -79.73558579999997000000),
        'two':(43.75846240000000000000, -79.22252100000003000000),
        'thr':(43.71773540000000000000, -79.74897190000002000000)
        }

cal_distances = {}

nodes = []

def dis():    

    y = len(locations)

    x = 0       

    while x != y:

        for key, value in locations.iteritems():                        
            d = calc_distance(value)
            cal_distances.setdefault(key,[])
            cal_distances[key].append(d)

        print cal_distances               

        min_distance = min(cal_distances, key = cal_distances.get)     

        if locations.has_key(min_distance):
            for ky, val in locations.iteritems():
                if ky == min_distance:
                    start = val   
            locations.pop(ky)                
        x = x+1  

    print locations

    print nodes

def calc_distance(destination):
     """great-circle distance between two points on a sphere from their longitudes and latitudes"""
    lat1, lon1 = start
    lat2, lon2 = destination
    radius     = 6371 # km. earth

    dlat = radians(lat2-lat1)
    dlon = radians(lon2-lon1) 

    a = (sin(dlat/2) * sin(dlat/2) + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon/2) * sin(dlon/2))
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    d = radius * c

    return d

dis()

您目前的代码相当混乱。我认为你想要实现的是:

start = (43.82846160000000000000, -79.53560419999997000000)

locations = {'one':(43.65162010000000000000, -79.73558579999997000000),
             'two':(43.75846240000000000000, -79.22252100000003000000),
             'thr':(43.71773540000000000000, -79.74897190000002000000)}

def dis(start, locations):

    nodes = []       

    while locations:
    # until the dictionary of locations is empty

        nearest = min(locations, key=lambda k: calc_distance(start, locations[k]))
        # find the key of the closest location to start

        nodes.append((start, locations[nearest]))
        # add a tuple (start, closest location) to the node list

        start = locations.pop(nearest)
        # remove the closest location from locations and assign to start

    return nodes

def calc_distance(start, destination):
    # ...

nodes = dis(start, locations)
请注意,我已将
start
作为
calc_distance
的显式参数,并将
start
位置
作为
dis
的显式参数-尽可能不依赖于变量的访问范围。我在
节点中得到的输出是:

[((43.8284616, -79.53560419999997), (43.7177354, -79.74897190000002)), 
 ((43.7177354, -79.74897190000002), (43.6516201, -79.73558579999997)), 
 ((43.6516201, -79.73558579999997), (43.7584624, -79.22252100000003))]

感谢您的回复和对我的代码的道歉,它只是想方设法让它工作,下次将在min中进行说明:)。这就是我想要做的,它给了我想要的。高度赞赏