Py学习  »  Python

用度数表示的Python-haversine公式还差得很远,我不知道为什么

charlie • 5 年前 • 1566 次点击  

这是我所有的python代码,距离返回正确的距离还很远。我打破了哈弗辛公式,知道它在某个地方出错了 C . C类 数字太大了 D 返回正确的距离。

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

First are my functions then my main part of the code
#-----FUNCTIONS------

#Header function
def Header():
    print("This program will calculate the distance between two geographic points!")

#get_location function
def Get_location():
    userLat = input("\n\n Please enter the latitude of your location in decimal degrees: ")
    userLon = input("Enter the longitude of the location in decimal degrees: ")
    return (userLat, userLon)


#Calculate distance function
#def Distance(lat1, lon1, lat2, lon2):
def Distance(location1, location2):
    radEarth = 6371 #km
    #location1 = Get_location()
    #location2 = Get_location()

    lat1 = location1[0]
    lon1 = location1[1]
    lat2 = location2[0]
    lon2 = location2[1]

    B = sin((lat1-lat2)/2)**2
    S = sin((lon1-lon2)/2)**2
    F = (cos(lat1))

    A = B + (F * (cos(lat2)) * S)


    C = 2 * (atan2(sqrt(A),sqrt(1-A)) * (180/pi))
    print(C)

    D = radEarth * C

    return D                                                       

This is the main part of my program

#-------MAIN---------

#Call header function
Header()

在用户继续时开始执行另一个循环:

doAnother = 'y'
while doAnother == 'y':

    #Collect location points from user
    location1 = Get_location()
    location2 = Get_location()
    print(location1)
    print(location2)
    #Calculate distance between locations
    distance = Distance(location1, location2)

    print('The distance between your two locations is: ' + str(distance))


    doAnother = raw_input('Do another (y/n)?'.lower())

#Display goodbye
print('Goodbye!')
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/51499
 
1566 次点击  
文章 [ 1 ]  |  最新文章 5 年前