wiki:UsersWikiNewbieDistanceMeasurement

Version 1 (modified by pracine, 15 years ago) ( diff )

Newbie Question

What is the measurement returned by ST_Distance?

ST_Distance always returns in the units of the projection.

So if for example you chose SRID 4326 - then your measurement would be in degrees.

It is best to use a meter based or feet based projection for measurement.

Unfortunately you can not do that unless you are dealing with regional data such as country, state etc.

To get meters when using 4326 or other degree projection - use ST_Distance_Sphere or ST_Distance_Spheroid. These unfortunately only work with point geometries.

Faster but less precise

SELECT ST_Distance_Sphere(
    ST_GeomFromText('POINT(5.0 52.0)', 4326),
    ST_GeomFromText('POINT(6.0 56.0)', 4326) );

SELECT ST_Distance_Sphere(t1.the_geom, t2.the_geom)
FROM t1 INNER JOIN t2 ON t1.somefield1 = t2.somefield2;

Slower but more precise

SELECT ST_Distance_Spheroid(
       ST_GeomFromText('POINT(5.0 52.0)', 4326),
       ST_GeomFromText('POINT(6.0 56.0)', 4326),
         'SPHEROID[wiki:UsersWiki"WGS 84",6378137,298.257223563 "WGS 84",6378137,298.257223563]'
     );

SELECT ST_Distance_Spheroid(t1.the_geom, t2.the_geom,  'SPHEROID[wiki:UsersWiki"WGS 84",6378137,298.257223563 "WGS 84",6378137,298.257223563]')
FROM t1 INNER JOIN t2 ON t1.somefield1 = t2.somefield2;

Note: See TracWiki for help on using the wiki.