ST_Dimension
What is ST_Dimension?
ST_Dimension is a PostGIS function that returns the topological (inherent) dimension of a geometry — 0 for points, 1 for lines, 2 for polygons. For GEOMETRYCOLLECTIONs, it returns the maximum dimension among the components.
ST_Dimension(geometry g) → integerThis is the OGC-defined topological dimension, distinct from coordinate dimension (ST_CoordDim, which counts X/Y/Z/M) or spatial-reference dimensionality.
When would you use ST_Dimension?
Use ST_Dimension to filter or branch by the kind of feature without parsing a type string — pointy data is 0, linear is 1, areal is 2:
1SELECT id, geom
2FROM features
3WHERE ST_Dimension(geom) = 2; -- polygons onlyIt is the cleanest way to separate mixed-type collections into points, lines, and polygons, and a reliable predicate for spatial-statistics pipelines that branch on feature kind.
FAQs
How is ST_Dimension different from GeometryType?
GeometryType returns a string like 'POLYGON'; ST_Dimension returns an integer. Use ST_Dimension for numeric comparisons and aggregation, GeometryType for display.
What does ST_Dimension return for a GeometryCollection?
The maximum dimension of the components. A collection containing a point and a polygon returns 2.
Does ST_Dimension consider Z or M?
No. Only X/Y topology matters. A 3D polygon still has dimension 2.
What about empty geometries?
Empty geometries still report their inherent dimension — an empty polygon returns 2, an empty point returns 0. Pair with ST_IsEmpty if you need to filter empties.