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?
Use min on the original list of dicts - no use in separating it into two lists - and use Pythagoras' theorem in your key function. You are correctly getting the lowest longitude value. You are separating out those values. Don't separate out the values, calculate the distance for the latitude and longitude together.
First get your current location Lattitude & Longitude , then get Lattitude & Longitude of each locations you have and find out distance of each place from your current location using distanceTo method of Location class and after that find out least distance from your list. Show activity on this post.
Format("POINT({0} {1})", longitude, latitude)); var nearbyLocations = (from location in _context. Locations where // (Additional filtering criteria here...) select new { LocationID = location.ID, Address1 = location. Address1, City = location. City, State = location.
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))
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.
Also u can simple 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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With