python - 用python计算往返距离

标签 python graph-algorithm

我的任务是计算从起始位置到我从 Facebook 获取的所有事件位置(以英里为单位)并返回起始位置的往返距离。 到目前为止我的代码:

import json
import re
from urllib import urlopen
import math

def getLatRad(latitude):
    return float(latitude) * (math.pi/180.0)
def getLongRad(longitude):
    return float(longitude) * (math.pi/180.0)
tuplist =[]
finaltuplist = []
#start_latitude = float(input('Pls enter the latitude co-ordinate of your starting location:'))
#start_longitude = float(input('Pls enter the longitude co-ordinate of your starting location:'))
start_latitude = 41.721194054071
start_longitude = -73.934258235003
longRad1= getLongRad(start_longitude)
latRad1 = getLatRad(start_latitude)
def main():

    eventids = [
                '264100516989470',
                '129843580476568',
                '158475914271199',
               ]
    for event in eventids:
        f = urlopen('http://graph.facebook.com/%s' % event)
        d = json.load(f)
        name = d['name']
        longitude = d["venue"]["longitude"]
        latitude = d["venue"]["latitude"]
        tuplist.append((name,longitude,latitude))
    for coordinates in tuplist:
        longRad2= getLongRad(coordinates[1])
        latRad2= getLatRad(coordinates[2])
        dlon = longRad2 - longRad1 
        dlat = latRad2 - latRad1
        a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2
        c = 2 * math.asin(math.sqrt(a)) 
        m = 3960 * c
        sum = m + m
        print sum
if __name__ == '__main__':
    main()

据我所知,这是我自己能做的。是否有人可以为我指明正确的方向,以获取总往返距离,而不是距起始位置的单独距离?

最佳答案

所以分解你的问题和你的解决方案。 目前可以获取start和event1之间的距离,但是无法获取event1和event2之间的距离。您还可以获得 start 和 event2 之间的距离。您需要在计算中更改哪些内容才能获得事件 1 到事件 2 的距离?

编辑详细说明请求:

latRad1 = getLatRad(start_latitude) longRad1= getLongRad(start_longitude)

dlon = 长拉德2 - 长拉德1 a = math.sin(dlat/2)**2 + math.cos(latRad1) * math.cos(latRad2) * math.sin(dlon/2)**2

上面的 2 个在 for 循环之外并且没有改变(我注意到)。因此,当您移动到新位置来计算距离时,您仍然会根据起始坐标来计算距离。

关于python - 用python计算往返距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003036/

相关文章:

iterator - 在 Boost Graph Library 中,为什么添加边会使边迭代器失效(以及其他问题)?

python - 将 py.test 的输出作为对象读取

python - 如何根据环境禁用 FastAPI 中的身份验证?

python - 在 Google App Engine 中上传文件

algorithm - 减少曲线中的点数

algorithm - 寻找具有最大最小度数的生成树

python - 如何在 python 中读取原始图像的数据 - 方法

python - 仅在函数内更改 np.seterr 行为

algorithm - Warshall算法思想及可能的改进

algorithm - Dijkstra 算法是否带有优先级队列句柄未找到目标?