Find the nearest latitude and longitude

I am writing a small program and increasing efficiency, I need to find the nearest latitude and longitude in my array.

Suppose you have the following code:

tempDataList = [{'lat': 39.7612992 , 'lon': -86.1519681}, {"lat": 39.762241, "lon": -86.158436}, {"lat": 39.7622292, "lon": -86.1578917}] tempLatList = [] tempLonList = [] for item in tempDataList: tempLatList.append(item['lat']) tempLonList.append(item['lon']) closestLatValue = lambda myvalue: min(tempLatList, key=lambda x: abs(x - myvalue)) closestLonValue = lambda myvalue: min(tempLonList, key=lambda x: abs(x - myvalue)) print(closestLatValue(39.7622290), closestLonValue(-86.1519750)) 

The result is:

 (39.7622292, -86.1519681) 

What should be (in this example, the last object in the list)

 (39.7622292, -86.1578917) 

I know how to get the closest cell with a single value, but I would like to get the lambda function to take both values ​​into account, but I'm not quite sure how to do this. Help?

+8
source share
4 answers

For the correct calculation of the distance between points of the globe, you need something like the Haversine formula. Using the Python implementation suggested in this answer , you can encode it like this:

 from math import cos, asin, sqrt def distance(lat1, lon1, lat2, lon2): p = 0.017453292519943295 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2 return 12742 * asin(sqrt(a)) def closest(data, v): return min(data, key=lambda p: distance(v['lat'],v['lon'],p['lat'],p['lon'])) tempDataList = [{'lat': 39.7612992, 'lon': -86.1519681}, {'lat': 39.762241, 'lon': -86.158436 }, {'lat': 39.7622292, 'lon': -86.1578917}] v = {'lat': 39.7622290, 'lon': -86.1519750} print(closest(tempDataList, v)) 
+18
source

if the earth is flat

 from itertools import combinations from math import sqrt coords = [{'lat': 39.7612992 , 'lon': -86.1519681}, {"lat": 39.762241, "lon": -86.158436}, {"lat": 39.7622292, "lon": -86.1578917}] def euclidean(l1, l2): return ((l1[0]**2)-(l2[0]**2)) + ((l1[1]**2)-(l2[1]**2)) pairs = [j for j in combinations([i.values() for i in coords], 2)] pairs.sort(key= lambda x: euclidean(*x)) print pairs[-1] 
+1
source

@trincot

I tried this algorithm using the json city list from openweathermap ( http://bulk.openweathermap.org/sample/city.list.json.gz ), provided these lat / long values. in France, the result is not correct, what is wrong ???? 'lat': 48.3155115, 'lon': 6.887635400000022

 #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import simplejson from math import cos, asin, sqrt with open("city.list.json") as f: json_data = simplejson.load(f) tempDataList = [] for station in json_data: tempDataList.append({'lat': station['coord']['lat'], 'lon': station['coord']['lat']}) def distance(lat1, lon1, lat2, lon2): p = 0.017453292519943295 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2 return 12742. * asin(sqrt(a)) def closest(data, v): return min(data, key=lambda p: distance(v['lat'],v['lon'],p['lat'],p['lon'])) v = {'lat': 48.3155115, 'lon': 6.887635400000022} station_proche = closest(tempDataList, v) print(station_proche) 
0
source

Also you can simply do:

 import mpu def distance(point1, point2): return mpu.haversine_distance(point1, point2) def closest(data, this_point): return min(data, key=lambda x: distance(this_point, x)) 
-1
source

Source: https://habr.com/ru/post/1261924/


All Articles