【问题标题】:Approximate GPS Value in PythonPython中的近似GPS值
【发布时间】:2018-12-18 19:09:48
【问题描述】:

如何在 Python 的“file.txt”列表中对“纬度、经度”坐标值进行近似搜索?

价值

37.04508, -95.57605

文件.txt

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 行

任何帮助将不胜感激! 谢谢。

【问题讨论】:

  • 您可以尝试浏览列表,然后查找 最接近 值。你试过什么?
  • 您可以使用Haversine formula

标签: python math gps coordinates


【解决方案1】:

你可以做的是计算每个坐标之间的距离,然后检查它是否是最近的:

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计算距离

【讨论】:

    【解决方案2】:

    使用不同的方法来修复,但这也可以打开您请求的 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)
    

    【讨论】:

      猜你喜欢
      • 2012-08-04
      • 2013-12-08
      • 2014-10-26
      • 2010-11-05
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      相关资源
      最近更新 更多