Skip to content

Instantly share code, notes, and snippets.

@slav123
Last active June 9, 2020 12:46
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 slav123/ce43f1c27f7b25fd07b51800b45c9d3e to your computer and use it in GitHub Desktop.
Save slav123/ce43f1c27f7b25fd07b51800b45c9d3e to your computer and use it in GitHub Desktop.
DELIMITER //
CREATE FUNCTION `GCDistDeg`(
_lat1 DOUBLE,
_lon1 DOUBLE,
_lat2 DOUBLE,
_lon2 DOUBLE
) RETURNS double
DETERMINISTIC
SQL SECURITY INVOKER
COMMENT 'Degrees in, Degrees out. For conversion: 69.172 mi/deg or 111.325 km/deg'
BEGIN
DECLARE _deg2rad DOUBLE DEFAULT PI()/180;
DECLARE _rlat1 DOUBLE DEFAULT _deg2rad * _lat1;
DECLARE _rlat2 DOUBLE DEFAULT _deg2rad * _lat2;
DECLARE _rlond DOUBLE DEFAULT _deg2rad * (_lon1 - _lon2);
DECLARE _m DOUBLE DEFAULT COS(_rlat2);
DECLARE _x DOUBLE DEFAULT COS(_rlat1) - _m * COS(_rlond);
DECLARE _y DOUBLE DEFAULT _m * SIN(_rlond);
DECLARE _z DOUBLE DEFAULT SIN(_rlat1) - SIN(_rlat2);
DECLARE _n DOUBLE DEFAULT SQRT(
_x * _x +
_y * _y +
_z * _z );
RETURN 2 * ASIN(_n / 2) / _deg2rad;
END
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment