Skip to content

Instantly share code, notes, and snippets.

@alxarch
Created November 13, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alxarch/ee20315505960e774537 to your computer and use it in GitHub Desktop.
Save alxarch/ee20315505960e774537 to your computer and use it in GitHub Desktop.
ST_Distance_Sphere Polyfill
CREATE FUNCTION `ST_Distance_Sphere` (a POINT, b POINT)
RETURNS float
no sql deterministic
BEGIN
declare R INTEGER DEFAULT 6371000;
declare `φ1` float;
declare `φ2` float;
declare `Δφ` float;
declare `Δλ` float;
declare a float;
declare c float;
set `φ1` = radians(y(a));
set `φ2` = radians(y(b));
set `Δφ` = radians(y(b) - y(a));
set `Δλ` = radians(x(b) - x(a));
set a = pow(cos(`Δφ`/2), 2) + cos(`φ1`) * cos(`φ2`) + pow(sin(`Δλ`/ 2), 2);
set c = atan2(sqrt(a), sqrt(1-a));
return R * c;
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment