ST_3DMaxDistance
What is ST_3DMaxDistance?
ST_3DMaxDistance returns the 3D maximum distance in Euclidean space between two geometries — the length of the longest possible line connecting a point on g1 to a point on g2, using Z coordinates. It is the 3D counterpart of ST_MaxDistance.
ST_3DMaxDistance(geometry g1, geometry g2) → floatUnits are those of the input CRS — typically metres for projected 3D data. There is no geography overload.
When would you use ST_3DMaxDistance?
Use ST_3DMaxDistance when elevation affects the worst-case separation: the largest diagonal of a 3D building envelope, the maximum clearance span between two utility corridors across different floors, or the 3D diameter of a complex solid. It is often used alongside ST_3DLongestLine when you need both the distance and the LINESTRING.
1SELECT a.id, b.id, ST_3DMaxDistance(a.geom, b.geom) AS max_3d_m
2FROM zones a, zones b
3WHERE a.id < b.id;FAQs
What if one input is 2D?
Missing Z values are treated as 0. Mixed 2D/3D inputs can give misleading results — explicitly ST_Force3D or verify all inputs are 3D.
How does it compare with ST_MaxDistance?
ST_MaxDistance projects inputs to 2D and ignores Z. ST_3DMaxDistance uses the full (x, y, z) distance, which is larger for geometries separated in elevation.
Is it symmetric?
Yes. ST_3DMaxDistance(g1, g2) = ST_3DMaxDistance(g2, g1).
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 first.