Last active
July 19, 2018 23:08
-
-
Save chanmix51/1999886 to your computer and use it in GitHub Desktop.
Latitude, longitude and orthodromic distance in PgSQL
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 DOMAIN latlong AS point CHECK (VALUE[0] BETWEEN -90.0 AND 90.0 AND VALUE[1] BETWEEN -180 AND 180); | |
CREATE OR REPLACE FUNCTION orthodromic_distance(latlong, latlong) RETURNS float AS $_$ | |
SELECT acos( | |
sin(radians($1[0])) | |
* | |
sin(radians($2[0])) | |
+ | |
cos(radians($1[0])) | |
* | |
cos(radians($2[0])) | |
* | |
cos(radians($2[1]) | |
- | |
radians($1[1])) | |
) * 6370.0; | |
$_$ LANGUAGE sql IMMUTABLE; | |
CREATE OPERATOR <-> ( PROCEDURE = orthodromic_distance | |
, LEFTARG = latlong, RIGHTARG = latlong | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Så skriv dog på engelsk!