gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:53:43 25 4
gpt4 key购买 nike

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

关于python - 计算距纬度和经度一定距离内的地标并附加到循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19886276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com