Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created August 14, 2017 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cviebrock/b2dd681d1970f59e00d04d59f1320bb1 to your computer and use it in GitHub Desktop.
Save cviebrock/b2dd681d1970f59e00d04d59f1320bb1 to your computer and use it in GitHub Desktop.
Vincenty distance formula as a MySQL function
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