Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active July 19, 2018 23:08
Show Gist options
  • Save chanmix51/1999886 to your computer and use it in GitHub Desktop.
Save chanmix51/1999886 to your computer and use it in GitHub Desktop.
Latitude, longitude and orthodromic distance in PgSQL
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
);
@hashar
Copy link

hashar commented Feb 14, 2013

Va falloir benchmarker !

@detbetteegern
Copy link

Så skriv dog på engelsk!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment