2

给定经纬度和地标特征的 2d 列表,我如何在用户输入当前位置 lat long 的 10 公里范围内找到这些地标?然后,当我进入一个循环时,我想将这些距离(stationDist)附加到一个新的二维列表(distanceList)中。

我如何用 Python 代码编写这个?我不知道!!请帮忙

import math

def main():
    DISTANCE_CONSTANT = 111120.0
    latOne = 55.9669
    lonOne = 114.5967
    latTwo = 55.9622
    lonTwo = 114.5889
    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
    print '\nThe distance is: ', distance
4

1 回答 1

2

这是设置它的一种方法。我已将您的米转换为公里...

import math

def calcdist(p1,p2):
    latOne,lonOne=p1
    latTwo,lonTwo=p2
    DISTANCE_CONSTANT = 111.1200

    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


ref = [36.0,-122.0]
names = ['close','far','kindaclose','ratherfar']
points = [[36.1,-122.0],[89.0,-123.0],[39.0,-122.0],[36.0,123.0]]

closelist=[]
threshold =12  #distance in km

for n,p in zip(names,points):   
    d = calcdist(ref,p)
    print '{} : {:.1f}'.format(n,d) 
    if d < threshold:  
       closelist.append([n,d])

print 'These are within {} km:'.format(threshold) , closelist

输出:

close : 11.1
far : 5889.4
kindaclose : 333.4
ratherfar : 9561.9
These are within 12 km: [['close',11.11200]]
于 2013-11-10T06:33:10.493 回答