如何在Python的“ file.txt”列表中近似搜索“纬度,经度”坐标值?
37.04508,-95.57605
37.04278, -95.58895
37.04369, -95.58592
37.04369, -95.58582
37.04376, -95.58557
37.04376, -95.58546
37.04415, -95.58429
37.0443, -95.5839
37.04446, -95.58346
37.04461, -95.58305
37.04502, -95.58204
37.04516, -95.58184
37.04572, -95.58139
37.0459, -95.58127
37.04565, -95.58073
37.04546, -95.58033
37.04516, -95.57948
37.04508, -95.57914
37.04494, -95.57842
37.04483, -95.5771
37.0448, -95.57674
37.04474, -95.57606
37.04467, -95.57534
37.04462, -95.57474
37.04458, -95.57396
37.04454, -95.57274
37.04452, -95.57233
37.04453, -95.5722
37.0445, -95.57164
37.04448, -95.57122
37.04444, -95.57054
37.04432, -95.56845
37.04432, -95.56834
37.04424, -95.5668
37.04416, -95.56545
37.044, -95.56251
37.04396, -95.5618
37.04508,-95.57914
第17行
任何帮助将不胜感激! 谢谢。
答案 0 :(得分:0)
您可以做的是计算每个坐标之间的距离,然后检查是否最接近:
from math import radians, cos, sin, asin, sqrt
# Taken from https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points
def compute_distance(lon1, lat1, lon2, lat2):
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))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles
return c * r
def search_closest(to_find, coordinates):
closest_coord = (0, 0)
closest_distance = compute_distance(coordinates[0][1], coordinates[0][0], to_find[1], to_find[0])
for coord in coordinates:
current_distance = compute_distance(coord[1], coord[0], to_find[1], to_find[0])
if closest_distance > current_distance:
closest_coord = coord
closest_distance = current_distance
return closest_coord
if __name__ == "__main__":
# Placeholder for files.txt content
coordinates = [
(37.04278, -95.58895),
(37.04369, -95.58592),
(37.04369, -95.58582),
(37.04376, -95.58557),
(37.04376, -95.58546),
(37.04415, -95.58429),
(37.0443, -95.5839),
(37.04446, -95.58346),
(37.04461, -95.58305),
(37.04502, -95.58204),
(37.04516, -95.58184),
(37.04572, -95.58139),
(37.0459, -95.58127),
(37.04565, -95.58073),
(37.04546, -95.58033),
(37.04516, -95.57948),
(37.04508, -95.57914),
(37.04494, -95.57842),
(37.04483, -95.5771),
(37.0448, -95.57674),
(37.04474, -95.57606),
(37.04467, -95.57534),
(37.04462, -95.57474),
(37.04458, -95.57396),
(37.04454, -95.57274),
(37.04452, -95.57233),
(37.04453, -95.5722),
(37.0445, -95.57164),
(37.04448, -95.57122),
(37.04444, -95.57054),
(37.04432, -95.56845),
(37.04432, -95.56834),
(37.04424, -95.5668),
(37.04416, -95.56545),
(37.044, -95.56251),
(37.04396, -95.5618)
]
to_find = (37.04508, -95.57605)
closest = search_closest(to_find, coordinates)
print(closest)
编辑:使用 Haversine 计算距离
答案 1 :(得分:0)
使用了另一种方法来修复,但这也可以打开您要求的txt文件。
import sys, os
import math
coords = open('coords.txt').read().split("\n")
x=[]
y=[]
for r in coords:
row = r.split(", ")
x.append(row[0])
y.append(row[1])
lowest = None
currentval = None
store = None
value = (37.04508, -95.57605)
for i in range(len(x)):
currentval = (math.sqrt((((float(x[i]) - value[0])**2) + ((float(y[i]) - value[1])**2))) * 111000)
if i == 0:
lowest = currentval
if currentval < lowest:
lowest = currentval
store = (float(x[i]), float(y[i]))
else:
continue
print (store)