Created
August 14, 2017 20:56
-
-
Save cviebrock/b2dd681d1970f59e00d04d59f1320bb1 to your computer and use it in GitHub Desktop.
Vincenty distance formula as a MySQL function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION VINCENTY( | |
lat1 FLOAT, lon1 FLOAT, | |
lat2 FLOAT, lon2 FLOAT | |
) RETURNS FLOAT | |
NO SQL | |
DETERMINISTIC | |
COMMENT 'Returns the distance in degrees on the Earth between two known points | |
of latitude and longitude using the Vincenty formula from: | |
http://en.wikipedia.org/wiki/Great-circle_distance' | |
BEGIN | |
RETURN DEGREES( | |
ATAN2( | |
SQRT( | |
POW(COS(RADIANS(lat2))*SIN(RADIANS(lon2-lon1)),2) + | |
POW(COS(RADIANS(lat1))*SIN(RADIANS(lat2)) - | |
(SIN(RADIANS(lat1))*COS(RADIANS(lat2)) * | |
COS(RADIANS(lon2-lon1)) | |
), | |
2 | |
) | |
), | |
SIN(RADIANS(lat1))*SIN(RADIANS(lat2)) + | |
COS(RADIANS(lat1))*COS(RADIANS(lat2))*COS(RADIANS(lon2-lon1)) | |
) | |
); | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment