Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way of finding distance between two coordinates using spatial function in MySql

Tags:

mysql

gis

spatial

I am trying to calculate distance between two locations using spatial functions in both Mysql and PostgresSQL. I have taken the latitude and longitude from Google. The details are below

Location one - Lat: 42.260223; Lon: -71.800010

Location two - Lat: 42.245647; Lon: -71.802521

SQL Query used:

SELECT DISTANCE(GEOMFROMTEXT('Point(42.260223 -71.800010)'),GEOMFROMTEXT('Point(42.245647 -71.802521)'))

The both databases are giving the same result 0.014790703059697. But when I calculate distance in other systems the results are different. Please refer the below links

http://www.zip-codes.com/distance_calculator.asp?zip1=01601&zip2=01610&Submit=Search = 1.44 miles

http://www.distancecheck.com/zipcode-distance.php?start=01601&end=01610 = 1.53 miles

So I want to know whether my calculation method/query is right or not. And if it is wrong, then what is the right way of querying the db for the distance.

like image 317
Vic Avatar asked Mar 16 '11 11:03

Vic


1 Answers

The simple answer is to use the Haversine formula. This assumes the earth is a sphere, which it isn't, but it's not a bad approximation. This, with lots of other details are described in this presentation:

http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

like image 182
Joshua Martell Avatar answered Sep 20 '22 14:09

Joshua Martell