wiki:UsersWikiNewbieDistanceMeasurement

Newbie Question

What is the measurement returned by ST_Distance?

ST_Distance always returns in the units of the projection for the geometry data type. For the geography data type introduced in PostGIS 1.5 -- units are always in meters and SRID is wgs 84 long lat (4326).

So if you are using geometry - for example you chose SRID 4326 - then your measurement would be in degrees. If you use geography -- your units are in meters. With geography you can do much less but you need to know less and cna cover a wider range of distance. With geometry data type you can do a lot more geometric processing, but you need to understand spatial reference systems etc and how to transform between them to get the most out of them.

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["WGS 84",6378137,298.257223563]'
     ) ;

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

Last modified 15 years ago Last modified on 12/02/09 10:39:43
Note: See TracWiki for help on using the wiki.