PostGISMeasurement

ST_3DLongestLine

What is ST_3DLongestLine?

ST_3DLongestLine returns the 3D line connecting the two points — one on each input geometry — that are farthest apart in three-dimensional space. It is the 3D counterpart of ST_LongestLine; its length equals ST_3DMaxDistance(g1, g2).

SQL
ST_3DLongestLine(geometry g1, geometry g2)geometry

The result is a two-point LINESTRING with Z values preserved.

When would you use ST_3DLongestLine?

Use ST_3DLongestLine when elevation matters to the maximum-separation calculation: the longest clearance diagonal between two 3D building envelopes, the maximum separation of two corridor surfaces across different floors, or the 3D diameter of a complex solid. It is useful in BIM, aviation corridor analysis, and any engineering QA where a worst-case 3D span is needed.

SQL
1-- 3D diameter of each building envelope
2SELECT id, ST_3DLength(ST_3DLongestLine(geom, geom)) AS diameter_m
3FROM building_envelopes;

FAQs

Does it handle 2D input?

Yes — missing Z values are treated as 0, so the result is effectively the 2D longest line. Mix carefully; populate Z on all inputs before using if that is not your intent.

What geometry types are supported?

POINT, LINESTRING, and POLYGON. Collections and curves are not supported directly; decompose with ST_Dump first.

Is it symmetric?

Yes. ST_3DLongestLine(g1, g2) and ST_3DLongestLine(g2, g1) return LINESTRINGs with the same two endpoints (possibly in reversed order) and the same length.

How is it different from ST_LongestLine?

ST_LongestLine projects to 2D and ignores Z; ST_3DLongestLine uses the full (x, y, z) distance. For geometries at different elevations this produces a noticeably longer line.