我想从数据库表计算纬度和经度坐标之间的距离,表中的第一个查询结果是其他纬度和经度位置的参考。 Db表将由“ USR,REF,LONG,LAT”组成。
最终,我的目标是制作一个用户可以在其中查找REF记录的GUI。然后,用户将从表中进行选择。并且基于选定的记录(将被用作其他纬度点的参考),将显示参考的经纬度点与其他纬度点和纬度点之间的距离。
我尝试过haversine配方。但是我看到大多数教程仅显示了两个纬度和经度点
import sys
import math
def haversine(lon1, lat1, lon2, lat2):
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) *math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
km = 6367 * c
return km
conn = pypyodbc.connect(# My MsAccessDB Location)
cursor = conn.cursor()
query = "SELECT*FROM Test_dist ORDER BY Licensee"
cursor.execute(query)
results = cursor.fetchall()
for x in results:
print(x) ```
OUTPUT
(' ABC JIM', 469.55, 18.533333, -33.85)
(' CALLY', 383.5125, 23.460278, -33.234722)
(' ROSS', 469.55, 21.983333, -34.166667)
(' SALLY M', 383.5625, 23.2325, -33.072222)
('KRACE', 400.36, 23.5688, -33.045668)
('LEEMER', 202.36, 23.2566, -33.158859)
When i run the code it displays all my records . I am not sure how to implement this into the haversine function thou as I would need to do it from my database table .
答案 0 :(得分:0)
如果要将每一行与第一行的经度和纬度进行比较,这就是您要做的事情。
for i in range(1,len(results)):
print haversine(results[0][2],results[0][3],results[i][2]results[i][3])