PostGISMeasurement

ST_3DShortestLine

What is ST_3DShortestLine?

ST_3DShortestLine returns a two-point LINESTRING connecting the 3D closest points on two geometries. Its length equals ST_3DDistance(g1, g2). It is the 3D counterpart of ST_ShortestLine.

SQL
ST_3DShortestLine(geometry g1, geometry g2)geometry

The returned line preserves Z values so you can render or analyse it as a 3D connector.

When would you use ST_3DShortestLine?

Use ST_3DShortestLine in engineering and BIM workflows where you need to visualise the actual 3D clearance path between assets — pipes and beams, power lines and trees, pedestrian paths and overhead obstructions. It is especially valuable when two features cross in plan view at different elevations: the 2D ST_ShortestLine would show a spurious zero-length crossing, while ST_3DShortestLine reveals the real clearance.

SQL
1-- 3D clearance connectors between power lines and tree canopies
2SELECT ST_3DShortestLine(pl.geom, tc.geom) AS clearance_line
3FROM power_lines pl
4JOIN tree_canopies tc ON ST_3DDWithin(pl.geom, tc.geom, 5);

FAQs

What if inputs are 2D?

Missing Z is treated as 0. Mixed 2D/3D pairs give misleading results; ensure all inputs are 3D before calling.

What is the length of the returned line?

ST_3DLength of the result equals ST_3DDistance(g1, g2).

How does it differ from ST_ShortestLine?

ST_ShortestLine ignores Z. ST_3DShortestLine uses full 3D geometry, so for assets at different elevations it returns a longer, non-zero line even when the 2D projections cross.

Which geometry types are supported?

POINT, LINESTRING, and POLYGON. Triangles, TINs and polyhedral surfaces are supported from PostGIS 2.2+. Collections should be decomposed with ST_Dump.