Name

ST_Distance — Returns the shortest distance between two geometries.

Synopsis

double ST_Distance(bytea GeomA, bytea GeomB);

Alias

Distance

Description

Returns the shortest distance between two geometries. It is calculated in 2d. The resulting distance will be in the units of the incoming geometries.

[Note]

This function supports GeometryCollections

Coordinate Dimensions
2D3DM
Spatial Standards Support
OGC SFS for SQL. 1.1 (1999)OGC SFS for SQL. 1.1.0 (2005)OGC SFS for SQL. 1.2.0 (2006)SQL-MM Part 3
2.1.1.37.2.20.17.2.8.15.1.23

Examples

SELECT ST_Distance(ga,gb) from (SELECT ST_GeomFromText('POINT(5 4 2 8)') as ga, ST_GeomFromText('POINT(8 6 10 2)') as gb) as foo;
--Result
3.605551275463989

SELECT ST_Distance(ga,gb) from 
 (SELECT ST_GeomFromText('POLYGON ((10 30, 0 10, 30 20, 30 30, 20 40, 10 30))') as ga, 
         ST_GeomFromText('GEOMETRYCOLLECTION (POINT (40 30), LINESTRING (50 40, 70 20, 70 10, 70 10, 70 10))') as gb) as foo;
--Result
10.0

--Distance Barcelona-Paris degrees
SELECT ST_Distance(Barcelona,Paris) as Dist_degrees from (SELECT ST_GeomFromText('POINT(2.183333 41.383333)',4326) as Barcelona, 
       ST_GeomFromText('POINT(2.350833 48.856667)',4326) as Paris) as foo;
   dist_degrees
------------------
 7.47521085492282


--Distance Barcelona-Paris meters (SRID 25831 UTM ETRS89 H31)
SELECT ST_Distance(ST_Transform(Barcelona,25831),ST_Transform(Paris,25831)) as Dist_UTM
from (SELECT ST_GeomFromText('POINT(2.183333 41.383333)',4326) as Barcelona, 
       ST_GeomFromText('POINT(2.350833 48.856667)',4326) as Paris) as foo;

     dist_utm
------------------
 830348.883136392

Related functions

ST_Distance_Sphere,ST_Distance_Spheroid