PostGISBounding Box

ST_3DExtent

What is ST_3DExtent?

ST_3DExtent is a PostGIS aggregate that returns a box3d enclosing every geometry in the input set. Unlike ST_Extent, it keeps track of minimum and maximum Z, producing a true 3D bounding cuboid.

SQL
ST_3DExtent(geometry set geoms) → box3d

When would you use ST_3DExtent?

Use ST_3DExtent whenever elevation is meaningful — LiDAR tiles, BIM models, subsurface utility networks — and you want the overall 3D extent of a group of features. It is the aggregate companion of Box3D and is essential input for 3D viewers that need to frame the scene.

SQL
1SELECT site_id, ST_3DExtent(geom) AS bbox
2FROM building_parts
3GROUP BY site_id;

FAQs

What does ST_3DExtent return when inputs are 2D?

For 2D geometries the Z component of the resulting box3d will be 0 on both sides. There is no error — the function gracefully degrades to effectively a 2D extent.

How does it differ from ST_Extent?

ST_Extent returns a 2D box2d and discards Z entirely. ST_3DExtent keeps Z. If you don't need elevation, ST_Extent is slightly cheaper.

How do empty geometries affect the aggregate?

Empty geometries are ignored. If every row in the group is empty, the aggregate returns NULL. For robust reporting, coalesce or filter with NOT ST_IsEmpty(geom).

Can I cast the box3d result to a geometry?

Yes — ST_3DExtent(geom)::geometry yields a 3D polygon representing the top and bottom of the bounding cuboid; it's a POLYHEDRALSURFACE or POLYGON Z depending on context. Use ST_AsText or a 3D viewer to inspect it.