ST_Zmflag
What is ST_Zmflag?
ST_Zmflag is a PostGIS function that returns a small integer indicating which optional dimensions a geometry carries:
ST_Zmflag(geometry g) → smallint0= 2D (X, Y only)1= 2D with M (X, Y, M)2= 3D with Z (X, Y, Z)3= 4D with both Z and M (X, Y, Z, M)
More informative than ST_NDims or ST_CoordDim, which cannot distinguish a POINT M from a POINT Z (both return 3).
When would you use ST_Zmflag?
Use ST_Zmflag when you need to distinguish Z-only, M-only, Z+M, and 2D geometries — for example when routing to Z-aware versus M-aware processing paths:
1SELECT id,
2 CASE ST_Zmflag(geom)
3 WHEN 0 THEN '2D'
4 WHEN 1 THEN 'XYM'
5 WHEN 2 THEN 'XYZ'
6 WHEN 3 THEN 'XYZM'
7 END AS dim_kind
8FROM features;Also useful for data audits and schema-validation queries: confirming an entire table holds 3D-Z data, for instance.
FAQs
Why not just use ST_NDims?
ST_NDims returns 3 for both POINT Z and POINT M and cannot distinguish them. ST_Zmflag gives you the exact combination.
What does 2D mean here?
No Z, no M. Standard X/Y geometry.
Can the flag vary per vertex?
No — dimensionality is a type-level property, constant for all vertices of a given geometry.
How do I force a specific dimensionality?
Use ST_Force2D, ST_Force3D (adds Z=0), ST_Force3DM, ST_Force4D, or ST_ForceDimension variants to coerce input into a desired dimensionality.