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 | |
); |
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
Va falloir benchmarker !
Så skriv dog på engelsk!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
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é.