This topic describes the ST_Distance function. This function calculates the 2D Euclidean distance between two geometry objects in the reference coordinate system of the geometry objects. If you specify two geography objects, this function returns the minimum spherical distance in meters between the geography objects.

Syntax

float  ST_Distance(geometry  g1 , geometry  g2);
float  ST_Distance(geography  gg1 , geography  gg2);
float  ST_Distance(geography  gg1 , geography  gg2 , boolean  useSpheroid);

Parameters

Parameter Description
g1 The first geometry object that you want to specify.
g2 The second geometry object that you want to specify.
gg1 The first geography object that you want to specify.
gg2 The second geography object that you want to specify.
useSpheroid Specifies whether to use an ellipsoid. The default is true, if you set this parameter to false, a specific precision is applied to improve the speed.

Description

This function supports circular strings and curves.

Examples

The following example shows the difference between the ST_Distance function and the ST_3DDistance function.
SELECT ST_Distance(g1,g2),ST_3DDistance(g1,g2) FROM(SELECT 'POINT(0 0 0)'::geometry as g1,
                                                                 'POINT(1 1 1)'::geometry as g2) as test;
   st_distance   |  st_3ddistance
-----------------+------------------
 1.4142135623731 | 1.73205080756888
(1 row)