这是半径的公式
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"
这将显示回溯(最近一次呼叫最后一次):
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'
不管怎样,当我使用我计算的变量时,会产生一个数值吗?你知道吗
从CSV获取的值的类型为
str
。您需要将它们转换为float
或Decimal
以使它们正常工作,例如,如果您将它们转换为float,您可以对函数执行此操作。你知道吗输出测试:
或者转换成
Decimal
,它提供浮点数的精确表示。你知道吗输出测试-
相关问题 更多 >
编程相关推荐