ST_Distance

Calculates the shortest distance between two spatial objects.

Calculates the shortest distance between two spatial objects. For GEOMETRY objects, the distance is measured in Cartesian coordinate units. For GEOGRAPHY objects, the distance is measured in meters.

Parameters g1 and g2 must be both GEOMETRY objects or both GEOGRAPHY objects.

Behavior type

Immutable

Syntax

ST_Distance( g1, g2
        [USING PARAMETERS spheroid={ true | false } ] )

Arguments

g1
Spatial object, type GEOMETRY or GEOGRAPHY
g2
Spatial object, type GEOMETRY or GEOGRAPHY

Parameters

spheroid = { true | false }

(Optional) BOOLEAN that specifies whether to use a perfect sphere or WGS84.

Default: False

Returns

FLOAT

Supported data types

Data Type GEOMETRY GEOGRAPHY (Perfect Sphere) GEOGRAPHY (WGS84)
Point Yes Yes Yes
Multipoint Yes Yes Yes
Linestring Yes Yes Yes
Multilinestring Yes Yes Yes
Polygon Yes Yes No
Multipolygon Yes Yes No
GeometryCollection Yes No No

Compatible GEOGRAPHY pairs:

Data Type GEOGRAPHY (Perfect Sphere) GEOGRAPHY (WGS84)
Point-Point Yes Yes
Linestring-Point Yes Yes
Multilinestring-Point Yes Yes
Polygon-Point Yes No
Multipoint-Point Yes Yes
Multipoint-Multilinestring Yes No
Multipolygon-Point Yes No

Recommendations

Vertica recommends pruning invalid data before using ST_Distance. Invalid geography values could return non-guaranteed results.

Examples

The following examples show how to use ST_Distance.

Distance between two polygons:

=> SELECT ST_Distance(ST_GeomFromText('POLYGON((-1 -1,2 2,0 1,-1 -1))'),
                      ST_GeomFromText('POLYGON((5 2,7 4,5 5,5 2))'));
 ST_Distance
-------------
           3
(1 row)

Distance between a point and a linestring in meters:

=> SELECT ST_Distance(ST_GeographyFromText('POINT(31.75 31.25)'),
    ST_GeographyFromText('LINESTRING(32 32,32 35,40.5 35,32 35,32 32)'));
   ST_Distance
------------------
 86690.3950562969
(1 row)