Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

python - Find the closest latitude and longitude

I'm writing a small program and to improve efficiency, I need to be able to find the closest latitude and longitude in my array.

Assume 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 I get is:

(39.7622292, -86.1519681)

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

(39.7622292, -86.1578917)

I know how to get a single value's closest cell but, I would like to make the lambda function to consider both values but I'm not entirely sure how. Help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For a correct calculation of the distance between points on the globe, you need something like the Haversine formula. Using the Python implementation offered in this answer, you could code it like this:

from math import cos, asin, sqrt

def distance(lat1, lon1, lat2, lon2):
    p = 0.017453292519943295
    hav = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p)*cos(lat2*p) * (1-cos((lon2-lon1)*p)) / 2
    return 12742 * asin(sqrt(hav))

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))

Haversine formula

The formula is given on Wikipedia as follows:

              1 ? cos(??)  
     hav(??) = ──────────  
                  2

...where ?? is either the difference in latitude (??), or the difference in longitude (??). For the actual angle ?? between two points, the formula is given as:

     hav(??) = hav(??? ? ???) + cos(???)cos(???)hav(??? ? ???)

So that becomes:

              1 ? cos(??? ? ???)                 1 ? cos(??? ? ???)
     hav(??) = ──────────────── + cos(???)cos(???)────────────────
                  2                                   2

The distance is calculated from that, using this formula (also on Wikipedia):

     ?? = 2?? arcsin(√hav(??))

In the above script:

  • p is the factor to convert an angle expressed in degrees to radians: π/180 = 0.017453292519943295...

  • hav is the haversine calculated using the above formula

  • 12742 is the diameter of the earth expressed in km, and is thus the value of 2?? in the above formula.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...