Created
November 13, 2015 14:10
-
-
Save alxarch/ee20315505960e774537 to your computer and use it in GitHub Desktop.
ST_Distance_Sphere Polyfill
This file contains 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 `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