Name

ST_3DDistance — Returns the 3D cartesian minimum distance (based on spatial ref) between two geometries in projected units.

Synopsis

float ST_3DDistance(geometry g1, geometry g2);

Description

Returns the 3-dimensional minimum cartesian distance between two geometries in projected units (spatial ref units).

To find the nearest geometry within a known 3D search radius, filter with ST_3DDWithin and sort the matches by ST_3DDistance. For XYZ geometries, an n-dimensional GiST index can accelerate the filter by checking XY and Z bounds. If both operands carry M, the n-dimensional bounding-box filter also checks M, while ST_3DDWithin measures only XYZ; the filter can therefore discard matches. In that case, use the default 2D GiST index. If both 2D and n-dimensional indexes exist, verify that the query plan uses the 2D index. The geometry <-> operator orders by XY distance.

This function supports 3d and will not drop the z-index.

This function supports Polyhedral surfaces.

This method implements the SQL/MM specification. SQL-MM ISO/IEC 13249-3

Availability: 2.0.0

Changed: 2.2.0 - In case of 2D and 3D, Z is no longer assumed to be 0 for missing Z.

Changed: 3.0.0 - SFCGAL version removed

Examples

Geometry example - units in meters (SRID: 2163 US National Atlas Equal area) (3D point and line compared 2D point and line) Note that currently no vertical datum support so Z is not transformed and assumed to be same units as final.

Code
SELECT ST_3DDistance(
            ST_Transform('SRID=4326;POINT(-72.1235 42.3521 4)'::geometry, 2163),
                ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45 15,-72.123 42.1546 20)'::geometry, 2163)
            ) As dist_3d,
            ST_Distance(
                ST_Transform('SRID=4326;POINT(-72.1235 42.3521)'::geometry, 2163),
                ST_Transform('SRID=4326;LINESTRING(-72.1260 42.45,-72.123 42.1546)'::geometry, 2163)
            ) As dist_2d;
Output
     dist_3d      |     dist_2d
------------------+-----------------
 127.295059324629 | 126.66425605671

Multilinestring and polygon both 3D and 2D distance. Same example as 3D closest point example.

Code
SELECT ST_3DDistance(poly, mline) As dist3d,
    ST_Distance(poly, mline) As dist2d
        FROM (SELECT  'POLYGON((175 150 5,20 40 5,35 45 5,50 60 5,100 100 5,175 150 5))'::geometry as poly,
               'MULTILINESTRING((175 155 2,20 40 20,50 60 -2,125 100 1,175 155 1),(1 10 2,5 20 1))'::geometry as mline) as foo;
Output
      dist3d       | dist2d
-------------------+--------
                 0 |      0
Figure
Geometry figure for visual-st-3ddistance-02

Find the nearest point within six units. The point with the nearest XY coordinates is outside the search radius in 3D.

Code
WITH candidates(id, geom) AS (
  VALUES (1, ST_MakePoint(0, 0, 10)),
         (2, ST_MakePoint(3, 0, 4)),
         (3, ST_MakePoint(4, 0, 0))
),
target(geom) AS (
  VALUES (ST_MakePoint(0, 0, 0))
)
SELECT id, ST_3DDistance(candidates.geom, target.geom) AS distance_3d
FROM candidates CROSS JOIN target
WHERE ST_3DDWithin(candidates.geom, target.geom, 6)
ORDER BY distance_3d, id
LIMIT 1;
Output
 id | distance_3d
----+-------------
  3 |           4
(1 row)