ST_NumPatches
What is ST_NumPatches?
ST_NumPatches is a PostGIS function that returns the number of patches (polygon faces) in a POLYHEDRALSURFACE or TIN geometry.
ST_NumPatches(geometry g) → integerReturns NULL for geometry types other than POLYHEDRALSURFACE or TIN.
When would you use ST_NumPatches?
Use ST_NumPatches when working with 3D surface models — inspecting mesh complexity, iterating patches in combination with ST_PatchN, or validating imports from 3D formats:
1SELECT model_id, ST_NumPatches(shell_geom) AS face_count
2FROM buildings_3d
3ORDER BY face_count DESC;Pair with ST_PatchN to access a specific face by 1-based index, analogous to ST_NumGeometries + ST_GeometryN on multi-geometries.
FAQs
What is a patch?
A polygonal face in a 3D surface structure. POLYHEDRALSURFACE is a collection of arbitrary polygon patches; TIN is a triangulated surface where each patch is a triangle.
Does ST_NumPatches work on GeometryCollection?
No — only POLYHEDRALSURFACE and TIN. Use ST_NumGeometries on general collections.
How do I iterate patches?
SELECT ST_PatchN(g, n) FROM generate_series(1, ST_NumPatches(g)) n; or the more idiomatic ST_Dump, which also works on polyhedral surfaces in recent PostGIS.
Is this commonly used?
Not in 2D workflows. Only relevant if you are importing 3D meshes, CAD exports, or BIM data.