ST_NDims
What is ST_NDims?
ST_NDims is a PostGIS function that returns the coordinate dimension of a geometry as an integer: 2 for XY, 3 for XYZ or XYM, and 4 for XYZM.
ST_NDims(geometry g) → integerSynonym of ST_CoordDim — PostGIS-native naming versus the SQL/MM-standard alias. Either works interchangeably.
When would you use ST_NDims?
Use ST_NDims to inspect or enforce dimensionality across a dataset, for instance to route 3D geometries to Z-aware operations and 2D geometries to the 2D path:
1SELECT id,
2 CASE WHEN ST_NDims(geom) >= 3
3 THEN ST_3DLength(geom)
4 ELSE ST_Length(geom)
5 END AS length
6FROM tracks;Also useful for data audits: SELECT ST_NDims(geom), COUNT(*) FROM features GROUP BY 1;
FAQs
ST_NDims vs ST_CoordDim — which should I use?
Equivalent functions. ST_CoordDim is the SQL/MM-standard name; ST_NDims is the PostGIS-native name. Pick one and use it consistently.
How does ST_NDims differ from ST_Dimension?
ST_Dimension returns the topological dimension (0 point, 1 line, 2 polygon) independent of Z/M. ST_NDims returns the coordinate count per vertex (2, 3, or 4).
Can ST_NDims tell me whether a geometry has Z or M?
Not directly — both POINT Z and POINT M return 3. Use ST_Zmflag to distinguish them.
Does ST_NDims inspect every vertex?
No — dimensionality is a type-level property stored in the geometry header, so this is a constant-time lookup regardless of vertex count.