Created
January 23, 2014 21:24
-
-
Save fitnr/8587125 to your computer and use it in GitHub Desktop.
MySQL distance function. Yes, just use PostGIS. Whatever.
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 DISTANCE_MI( start_lat DOUBLE, start_lon DOUBLE, end_lat DOUBLE, end_lon DOUBLE ) | |
RETURNS DOUBLE DETERMINISTIC | |
RETURN 3959 * | |
acos( | |
cos( radians(start_lat) ) | |
* cos( radians( end_lat ) ) | |
* cos( | |
radians(end_lon) - radians(start_lon) | |
) | |
+ sin( radians(start_lat) ) | |
* sin( radians(end_lat) ) | |
); | |
CREATE FUNCTION DISTANCE_KM( start_lat DOUBLE, start_lon DOUBLE, end_lat DOUBLE, end_lon DOUBLE ) | |
RETURNS DOUBLE DETERMINISTIC | |
RETURN 6378 * | |
acos( | |
cos( radians(start_lat) ) | |
* cos( radians( end_lat ) ) | |
* cos( | |
radians(end_lon) - radians(start_lon) | |
) | |
+ sin( radians(start_lat) ) | |
* sin( radians(end_lat) ) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment