0

我必须计算给定的一组点与来自列表的多个点之间的距离。

列表中的一行示例是;

['14', '"Name of place"', '-31.000', '115.000']

由于计算距离函数需要四个参数,因此我将两个给定点放入然后列表的 long 和 lat 值中。

我的理解是要做到这一点,我可以简单地参考列表,即“列表”,然后我想访问每行的哪一部分,即 2 和 3

        User_E = raw_input("First enter your longitude(easting) value")
        User_N = raw_input("Now enter your latitude(northing) value")
        Radius = raw_input("Now enter a search radius in kilometres")
        for lines in ListOfLandmarks:
            CalculateDistance( User_N, User_E, ListOfLandmarks[2], ListOfLandmarks[3] )

当我运行程序时,我收到以下错误:

TypeError: unsupported operand type(s) for -: 'str' and 'list'

我试图使用它们int并将float它们识别为数字,但它们产生以下结果:

TypeError: int() argument must be a string or a number, not 'list'

TypeError: float() argument must be a string or a number

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

只是想知道我哪里错了,干杯!

4

1 回答 1

2

跳过转换问题(当您将坐标存储为字符串而不是浮点数时)

for lines in ListOfLandmarks:
        CalculateDistance( User_N, User_E, ListOfLandmarks[2], ListOfLandmarks[3] )

应该

for lines in ListOfLandmarks:
        CalculateDistance( User_N, User_E, lines[2], lines[3] )

当您询问到您迭代的特定地标的距离时,ListOfLandmarks[2]第二个地标(因此list您的解释器不知道如何在float上下文中比较/使用它),而当前地标的第一个坐标lines[2]

于 2013-10-19T05:22:32.930 回答