So you calculate the distance between lat / long pairs using the haversine formula:
import math R = 6371 # km dLat = (lat2-lat1) # Make sure it in radians, not degrees dLon = (lon2-lon1) # Idem a = math.sin(dLat/2) * math.sin(dLat/2) + math.cos(lat1) * math.cos(lat2) * math.sin(dLon/2) * math.sin(dLon/2) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) d = R * c;
Now it’s trivial to check "d" (also in km) at your doorstep. If you want something more than km, adjust the radius.
Sorry, I can’t give you an insert solution, but I don’t understand your code skeleton (see comment).
Also note that these days you probably want to use the spherical law of cosines rather than Haversine. The advantages in numerical stability are no longer worth it, and it's damn easy to understand, encode and use.
drxzcl Jul 05 '10 at 21:50 2010-07-05 21:50
source share