半径计算中的数字

2024-04-27 05:01:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这是半径的公式

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'

不管怎样,当我使用我计算的变量时,会产生一个数值吗?你知道吗


Tags: andofmathcosdocumentsdistanceprintangle
1条回答
网友
1楼 · 发布于 2024-04-27 05:01:58

从CSV获取的值的类型为str。您需要将它们转换为floatDecimal以使它们正常工作,例如,如果您将它们转换为float,您可以对函数执行此操作。你知道吗

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

相关问题 更多 >