Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 507
fread2281 Avatar asked Apr 01 '13 02:04

fread2281


People also ask

How do you find the distance between two lines of longitude?

As a general rule, the formula for the ​distance between meridians​ is therefore ​(2πR)(cos L)​, where L is latitude and R is the radius of the Earth.

What is the distance between 2 latitudes?

What is the Distance Between Lines of Latitude? Lines of latitude are called parallels and in total there are 180 degrees of latitude. The distance between each degree of latitude is about 69 miles (110 kilometers).

Can you calculate distance based on latitude and longitude?

One of the most common ways to calculate distances using latitude and longitude is the haversine formula, which is used to measure distances on a sphere. This method uses spherical triangles and measures the sides and angles of each to calculate the distance between points.


2 Answers

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

like image 43
TreyA Avatar answered Oct 06 '22 23:10

TreyA


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))      # Radius of earth in kilometers is 6371     km = 6371* 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.

like image 164
Aaron D Avatar answered Oct 07 '22 01:10

Aaron D