0
我想查找最短距離的經緯度組合。 x_lat,x_long是恆定的。我想獲得y_latitude,y_longitude的組合並計算距離並找出最小距離並返回相應的y_latitude,y_longitude。在python中返回最短距離的緯度
以下是嘗試,
x_lat = 33.50194395
x_long = -112.048885
y_latitude = ['56.16', '33.211045400000003', '37.36']
y_longitude = ['-117.3700631', '-118.244']
我有一個距離函數將返回的距離,
from math import radians, cos, sin, asin, sqrt
def distance(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
所以,我試圖像下面,
dist = []
for i in itertools.product(y_latitude , y_longitude):
print i
dist.append(distance(float(i[1]),float(i[0]),float(x_long), float(x_lat)))
print dist.index(min(dist))
所以這會創建y_latitude和y_longitude的所有可能組合並計算距離和返回ns最小距離的指數。我無法讓它返回相應的y_latitude和y_longitude。
這裏最小距離的索引是2,輸出是2.所需輸出是('33.211045400000003','-117.3700631'),我不能使它返回。
有人能幫我解決最後一塊嗎?
感謝
回報'分鐘(DIST)',而不是返回其索引 – Arman
這隻會返回最小距離。我想要最小距離的相應y_lat和y_long – Observer