0

这是半径的公式

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'

有没有办法解决它,当我使用我计算的变量时会产生一个数值?

4

1 回答 1

2

您从 CSV 获得的值属于str. 您需要将它们转换为floatDecimal使其正常工作,例如,如果将它们转换为浮点数,则可以对您的函数执行此操作。

def calculateDistance( latOne, lonOne, latTwo, lonTwo ):
    latOne, lonOne, latTwo, lonTwo = [float(x) for x in (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

输出测试:

>>> calculateDistance('-20', '100', '-30', '120')
2295032.2183717163

或者转换成Decimal哪个提供浮点数的精确表示。

def calculateDistance( latOne, lonOne, latTwo, lonTwo ):
    from decimal import Decimal
    latOne, lonOne, latTwo, lonTwo = [Decimal(x) for x in (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

输出测试 -

>>> calculateDistance('-20', '100', '-30', '120')
2295032.2183717163
于 2013-05-30T06:40:47.967 回答