Created
January 23, 2014 09:36
-
-
Save ianthrive/8575667 to your computer and use it in GitHub Desktop.
Orthodrome ("great circle") distance between two latitude/longitude points. This function uses the spherical law of cosines formula, rather than the seemingly more popular (probably because it's older) "haversine" formula. It is assumed the earth is a perfect sphere with a radius of 6,371km.
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 OR REPLACE FUNCTION public.orthodrome_km(lat1 numeric, lon1 numeric, lat2 numeric, lon2 numeric) | |
RETURNS double precision | |
LANGUAGE plpgsql | |
AS $function$ | |
BEGIN | |
return acos(sin(radians(lat1))*sin(radians(lat2))+cos(radians(lat1))*cos(radians(lat2))*cos(radians(lon2)-radians(lon1)))*6371; | |
END; | |
$function$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment