Sunday, February 13, 2011

Distance on earth calculation in SQL

Here's how to create an SQL function to calculate the distance (in KM) between to points on earth. This is an estimation since the distance is calculated as if the earth was round, but it's close enough for most purposes:


Unknown said...

Really good and informative blog.Here are the two formulas implemented as MySQL UDFs (user defined functions). I use the built in RADIANS and DEGREES functions to convert between radians and degrees, but you can also simply multiply by PI/180 (0.017453293) to convert to radians and 180/PI (57.295780) to convert to degrees. These functions as written return results in miles, but you can replace 3956.547 (the average radius of the earth in miles) with 6367 (the same thing in kilometers) to get the final distance in kilometers.

distance calculator

Abdul bari said...

The distance between city distances/nodes in a collaboration graph is called the collaboration distance.

albina N muro said...

Nice statistics which are updated up to the latest. Can I see the past statistics? Would like to compare them. USA city distances