How can I quickly estimate the distance between two (latitude, longitude) points?
How can I quickly estimate the distance between two (latitude, longitude) points?
I want to be able to get a estimate of the distance between two (latitude, longitude) points. I want to undershoot, as this will be for A* graph search and I want it to be fast. The points will be at most 800 km apart.
Answer by Raymond Hettinger for How can I quickly estimate the distance between two (latitude, longitude) points?
One idea for speed is to transform the long/lat coordinated into 3D (x,y,z) coordinates. After preprocessing the points, use the Euclidean distance between the points as a quickly computed undershoot of the actual distance.
Answer by Aaron D for How can I quickly estimate the distance between two (latitude, longitude) points?
The answers to Haversine Formula in Python (Bearing and Distance between two GPS points) provide Python implementations that answer your question.
Using the implementation below I performed 100,000 iterations in less than 1 second on an older laptop.  I think for your purposes this should be sufficient.  However, you should profile anything before you optimize for performance.  from math import radians, cos, sin, asin, sqrt  def haversine(lon1, lat1, lon2, lat2):      """      Calculate the great circle distance between two points       on the earth (specified in decimal degrees)      """      # convert decimal degrees to radians       lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])      # haversine formula       dlon = lon2 - lon1       dlat = lat2 - lat1       a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2      c = 2 * asin(sqrt(a))       km = 6367 * c      return km
To underestimate haversine(lat1, long1, lat2, long2) * 0.90 or whatever factor you want.  I don't see how introducing error to your underestimation is useful.
Answer by Aaron D for How can I quickly estimate the distance between two (latitude, longitude) points?
For maximal speed, you could create something like a rainbow table for coordinate distances. It sounds like you already know the area that you are working with, so it seems like pre-computing them might be feasible. Then, you could load the nearest combination and just use that.
For example, in the continental United States, the longitude is a 55 degree span and latitude is 20, which would be 1100 whole number points. The distance between all the possible combinations is a handshake problem which is answered by (n-1)(n)/2 or about 600k combinations. That seems pretty feasible to store and retrieve. If you provide more information about your requirements, I could be more specific.
Answer by TreyA for How can I quickly estimate the distance between two (latitude, longitude) points?
Since the distance is relatively small, you can use the equirectangular distance approximation. This approximation is faster than using the Haversine formula. So, to get the distance from your reference point (lat1/lon1) to the point your are testing (lat2/lon2), use the formula below. Important Note: you need to convert all lat/lon points to radians:
R = 6371  // radius of the earth in km  x = (lon2 - lon1) * cos( 0.5*(lat2+lat1) )  y = lat2 - lat1  d = R * sqrt( x*x + y*y )  Since 'R' is in km, the distance 'd' will be in km.
Reference: http://www.movable-type.co.uk/scripts/latlong.html
Answer by user1786747 for How can I quickly estimate the distance between two (latitude, longitude) points?
I am using the script Aaron D provided, but get some weird results. I make lac2 and lon2 fixed (It is somewhere in Mexico). And I have a list of lat1 and lon1. The result turned out some samples from Mexico showed longer distance than other from Argentina. That doesnot make sense. So, I want to ask if the negative latitude and longitude matters, do I need to convert to positive first?
Thanks!
Answer by uher for How can I quickly estimate the distance between two (latitude, longitude) points?
Please use the following code.
def distance(lat1, lng1, lat2, lng2):      #return distance as meter if you want km distance, remove "* 1000"      radius = 6371 * 1000         dLat = (lat2-lat1) * math.pi / 180      dLng = (lng2-lng1) * math.pi / 180        lat1 = lat1 * math.pi / 180      lat2 = lat2 * math.pi / 180        val = sin(dLat/2) * sin(dLat/2) + sin(dLng/2) * sin(dLng/2) * cos(lat1) * cos(lat2)          ang = 2 * atan2(sqrt(val), sqrt(1-val))      return radius * ang  Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
 

 
 
 
 
 
 
 
0 comments:
Post a Comment