python-计算距离纬度和经度范围内的地标,并附加到循环中
给出了一个二维的横向、纵向和地标特征列表 我如何在用户输入的当前位置(lat long)10公里范围内找到这些地标? 然后,当我进入循环时,我想将这些距离(StationList)附加到一个新的2d列表(distanceList)中 如何用Python代码编写此代码?我不知道!!请帮忙python-计算距离纬度和经度范围内的地标,并附加到循环中,python,Python,给出了一个二维的横向、纵向和地标特征列表 我如何在用户输入的当前位置(lat long)10公里范围内找到这些地标? 然后,当我进入循环时,我想将这些距离(StationList)附加到一个新的2d列表(distanceList)中 如何用Python代码编写此代码?我不知道!!请帮忙 import math def main(): DISTANCE_CONSTANT = 111120.0 latOne = 55.9669 lonOne = 114.5967 l
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
你会怎么用手做?我不知道你们是在数学方面还是在Python方面遇到了问题。蒂姆,你们能帮忙吗?对不起,不,我现在要睡觉了。现在,您添加了一些初始尝试,我希望其他人能够接受!你好非常感谢你的回复。那么,我是否仍然需要循环来要求列表中的每一行计算距离,然后追加?如果您可以编辑您的问题以显示数据格式的示例,这将有所帮助。有很多方法可以做到这一点(字典,如果名字是唯一的),我只是展示了一个。在回答你关于循环的问题时,据我所知,你仍然需要循环。您可以使用列表理解作为一行,但它仍然有效地循环。如果有很多地标,其中大部分都不接近参考,那么你可以预过滤,因此如果度数的差异超过一定量,不要计算,但这是一个相当普遍的解决方案。查找所有距离,在进行计算时,将范围内的距离添加到另一个列表中。(您不必打印每个距离;这只是为了演示)。
close : 11.1
far : 5889.4
kindaclose : 333.4
ratherfar : 9561.9
These are within 12 km: [['close',11.11200]]