0

My goal is to retrieve a user's lat/long position then find the nearest lat/long in my list of locations. Below is what I have and I believe it works however I am not sure if this is the best method to get to the end result I want.

I go through the list of locations converting them to absolute values then subtract out the user's location value to get the distance. If either the X oy Y is less than the previously recorded X or Y the distance variable gets updated with the new values.

This appears to work however, like I said, I am not sure if I went about it in the best manner possible. My list of locations will update every so often but it will not exceed 100 possible positions.

Thank you very much for your time.

locations = [(-71.43994800000002,41.6919549),
(-71.61075089999997,41.577545),
(-71.06653670000003,42.41383099999999),
(-71.41283429999999,41.8239891),
(-71.05888010000001,42.3600825),
(-74.00594130000002,40.7127837)]

userlocation = (-71.28254930000003,41.7303793)

distance = [999,999] #initial value holder for distance
for location in locations:
    x = abs(location[0]) # absolute value of latitude
    y = abs(location[1]) #absolute value of longitude
    xu = abs(userlocation[0]) #absolute value of user's latitude
    yu = abs(userlocation[1]) #absolute value of user's longitude
    dx = x-xu #Subtract user from location X
    dy = y-yu #subtract user from location Y
    if dx < distance[0]: #if distance X is less than the current distance value
        distance[0] = dx #update with new values
        distance[1] = dy
        continue #go to the next one
    if dy < distance[1]: #if distance Y is less than the current distance value
        distance[0] = dx #update with new values
        distance[1] = dy
        continue #go to the next one
print(distance) #print the end smallest result
Dapper
  • 93
  • 1
  • 8
  • latitude and longitude are not x/y. You cannot simply calculate the Euclidean or Manhatten distance, etc. between two such coordinates. Furterhermore there is a wraparond. – Willem Van Onsem Jul 19 '17 at 13:30
  • How precise do you need it? Because Euclidean distance might not be precise enough - for example a degree of latitude at the poles takes ~111.7km, but at the equator it covers 'only' ~110.6km. – zwer Jul 19 '17 at 13:31
  • I understand when scaled up to the entire globe that we can not represent these coordinates as a grid however I am working with a small region of the USA so I thought that within my restricted area it may work for relative distance. Is my assumption incorrect? – Dapper Jul 19 '17 at 13:32
  • @zwer Accuracy is not very important. If the end result is "Generally closer" , that is solid enough. This is for marketing purposes, not scientific so absolute accuracy is not necessary. – Dapper Jul 19 '17 at 13:33
  • @Reizvoller - you still need to deal with the difference in coverage of a degree of latitude vs a degree of longitude so treating them as Euclidean distances will not yield correct results except for a very generalized case. Use the [haversine formula](https://en.wikipedia.org/wiki/Haversine_formula) (here's a [Python example](https://stackoverflow.com/a/4913653/7553525)) to get a general-precision distance, as suggested bellow. Then all you need is to loop through your list and compare the distances, keeping the one that yields the lowest result. – zwer Jul 19 '17 at 13:38
  • @zwer Attempting that solution right now. I hadn't ever heard of the Haversine Formula before. Yay new knowledge! :) – Dapper Jul 19 '17 at 13:40
  • @Reizvoller - keep in mind that the haversine formula treats the Earth as a perfect sphere so it's not a high-precision formula (you don't want your airplane's navigation to depend on it ;)), but it should be more than enough for your use case. – zwer Jul 19 '17 at 13:43
  • These results are making more sense. I've accepted the answer provided. Thank you for your help in thinking this through :) – Dapper Jul 19 '17 at 13:49

1 Answers1

0

I would try to get the real distance then comparing the distance in a hacky way Calculate distance between two latitude-longitude points? (Haversine formula) and then

def calculate_distance(lat1, lon1, lat2, lon2):
   # go to the link to use the implementation
   pass
locations = []
user_loc = (-71.28254930000003,41.7303793)
ulat, ulon = user_loc
res = map(lambda x: (calculate_distance(ulat, ulon, x[0], x[1]), x), locations)
print min(res, key=lambda x: x[0])
Hamuel
  • 633
  • 5
  • 16