这是半径的公式
import math
def calculateDistance( latOne, lonOne, latTwo, lonTwo ):
DISTANCE_CONSTANT = 111120.0
coLat = math.fabs(lonOne - lonTwo)
alpha = 90 - latTwo
beta = 90 - latOne
cosAlpha = math.cos(math.radians(alpha))
cosBeta = math.cos(math.radians(beta))
sinAlpha = math.sin(math.radians(alpha))
sinBeta = math.sin(math.radians(beta))
cosC = math.cos(math.radians(coLat))
cos_of_angle_a = (cosAlpha * cosBeta)
cos_of_angle_b = (sinAlpha * sinBeta * cosC)
cos_of_angle_c = cos_of_angle_a + cos_of_angle_b
angle = math.degrees(math.acos(cos_of_angle_c))
distance = angle * DISTANCE_CONSTANT
return distance
print calculateDistance(latOne, lonOne, latTwo, lonTwo), "metres"
现在,如果我像这样将值放入等式中
print calculateDistance(-20.73, 116.75, -21.06, 117.44), "metres"
The answer is 80470.8270982 metres
现在麻烦来了。我有从 csv 文件夹计算的变量,因此lat1 = -20 , lon1 = 100 and lat2 = -30 and lon2 = 120
当我运行以下代码时
print calculateDistance(lat1, lon1, lat2, lon2), "metres"
这出现 Traceback(最近一次调用最后一次):
File "C:\Documents and Settings\Guest\My Documents\SC\Actual work\distance.py", line 104, in <module>
print calculateDistance(lat0, long0, lat1, long1), "metres"
File "C:\Documents and Settings\Guest\My Documents\SC\Actual work\distance.py", line 5, in calculateDistance
coLat = math.fabs(lonOne - lonTwo)
TypeError: unsupported operand type(s) for -: 'str' and 'str'
有没有办法解决它,当我使用我计算的变量时会产生一个数值?