Skip to main content
 首页 » 编程设计

python之计算距纬度和经度一定距离内的地标并附加到循环中

2024年11月01日14java哥

给定经纬度和地标特征的二维列表 我如何在用户输入的当前位置经纬度 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 

请您参考如下方法:

这是一种设置方法。我已经把你的米换算成了千米...

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]]