简体   繁体   中英

Approximate GPS Value in Python

How can I do an approximate search for a "latitude, longitude" coordinate value in a "file.txt" list in Python?

Value

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

Expected Result

37.04508, -95.57914

Additional Information (if possible)

Line 17

Any help will be greatly appreciated! Thank you.

What you could do is compute the distance between each coordinate then check if that is the closest:

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)

Edit: Used Haversine to compute distance

Used a different approach to fixatd but this works aswell opening the txt file you requested.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM