Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active July 19, 2018 23:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

Le rayon moyen de la terre est 6370, il te manque un kilomètre.

Pour des points proches, je me demande si tu ne risque pas de grosses erreurs d'arrondies:

Soit une longueur d'arc de 1km et un rayon de 6371km, l'angle est de 0,00015696123058~ radian. Soit un cosinus de 0,999999987681586~.

Idéalement il faudrait considérer la mesure en prenant en compte une section de la terre ce qui est une ellipse excentrée. Mais on va dire que je chipote :-]

Peut être voir dans le projet postgis, postgis/lwgeom_spheroid.c contient une fonction distance_ellipse_calculation() qui prend une sphere en paramètre. Mais pas sûr que ça prenne en compte l'excentricité.

@mhugo
Copy link

mhugo commented Feb 14, 2013

Aevc PostGIS, il doit falloir un truc comme ça :

select st_distance_spheroid(
st_setsrid(st_makepoint(22,34),4326),
st_setsrid(st_makepoint(33,44),4326),
'SPHEROID["WGS 84",6378137,298.257223563]'
);

Je sais pas à quel point c'est plus précis que la proposition initiale

@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