ST_3DClosestPoint
What is ST_3DClosestPoint?
ST_3DClosestPoint returns the 3D point on geometry g1 that is closest to geometry g2, considering Z coordinates. It is the 3D counterpart of ST_ClosestPoint and uses full Euclidean distance in (x, y, z) space.
ST_3DClosestPoint(geometry g1, geometry g2) → geometryBoth inputs should have Z values; if either is 2D, missing Z coordinates are treated as 0, which typically is not what you want. The function only supports POINT, LINESTRING, and POLYGON combinations (no curves or collections).
When would you use ST_3DClosestPoint?
Use ST_3DClosestPoint in engineering and BIM workflows where elevation matters: finding the point on a pipe nearest to a building façade, the point on a flight path closest to terrain, or the nearest approach point between two utility runs at different depths. In 3D city models it is used to tether a pedestrian at street level to the nearest feature on a building wall or roof.
1SELECT ST_AsText(ST_3DClosestPoint(pipe.geom, wall.geom))
2FROM pipes pipe, walls wall
3WHERE pipe.id = 42 AND wall.id = 17;FAQs
What happens if one input is 2D?
Missing Z values are treated as 0, which effectively places the geometry on the z = 0 plane. This is rarely the desired behaviour for mixed 2D/3D datasets — ensure both inputs are truly 3D before calling.
Is it symmetric?
No. The function returns a point on g1, so swapping arguments returns a point on g2. The two points coincide only when the geometries intersect in 3D.
How is it different from ST_ClosestPoint?
ST_ClosestPoint projects geometries to 2D and ignores Z. ST_3DClosestPoint uses full 3D distance, so two lines that "cross" in plan view but are at different elevations will not be considered touching.
Which geometry types are supported?
POINT, LINESTRING, and POLYGON. Curved geometries and collections are not supported; cast or decompose them first. For 3D distance on TINs or polyhedral surfaces, convert using ST_CollectionExtract or segment-by-segment evaluation.