以度为单位的Python haversine公式已经过时了
这是我所有的python代码,距离返回正确的距离还很远。我把哈弗森公式拆开,知道它在C的某个地方出错了。C是一个太大的数字,不允许D返回正确的距离以度为单位的Python haversine公式已经过时了,python,Python,这是我所有的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 w
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!')
看起来您正在实施所述的哈弗森公式。顺便说一句,我必须做同样的事情。你是对的,C中有一个问题 您的Python代码:
C = 2 * (atan2(sqrt(A),sqrt(1-A)) * (180/pi))
来自上面Javascript的URL的代码:
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
问题是,你们用180/pi将C转换成度,但下一个计算D=radEarth*C只有在C是弧度时才有数学意义