Приблизительное значение GPS в Python - PullRequest
0 голосов
/ 18 декабря 2018

Как выполнить приблизительный поиск значения координаты "широта, долгота" в списке "file.txt" в Python?

Значение

37.04508, -95.57605

file.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

Любая помощь будет принята с благодарностью!Спасибо.

Ответы [ 2 ]

0 голосов
/ 18 декабря 2018

Использовал другой подход к fixatd, но он работает и при открытии запрошенного вами 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)
0 голосов
/ 18 декабря 2018

Что вы можете сделать, это вычислить расстояние между каждой координатой, а затем проверить, является ли оно ближайшим:

from math import radians, cos, sin, asin, sqrt

# Taken from /2530076/formula-haversine-v-python-azimut-i-rasstoyanie-mezhdu-dvumya-tochkami-gps
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 для вычисления расстояния

...