ST_M
What is ST_M?
ST_M is a PostGIS function that returns the M (measure) coordinate of a POINT geometry. When the point has no M dimension, it returns NULL.
ST_M(geometry point) → double precisionInput must be a POINT; non-point input returns NULL. For extracting M from arbitrary geometries, first obtain a specific vertex with ST_PointN, ST_StartPoint, or (ST_DumpPoints(g)).geom.
When would you use ST_M?
Use ST_M in linear-referencing and trajectory analysis, where the M value encodes chainage, time, or another scalar along the feature:
1SELECT id, ST_M(ST_EndPoint(route_geom)) AS total_chainage_km
2FROM pipelines
3WHERE ST_CoordDim(route_geom) >= 3;Pair with ST_AddMeasure, ST_LocateAlong, and the rest of the linear-referencing toolkit when M is your primary analysis axis.
FAQs
What if the point has no M?
ST_M returns NULL. Check dimensionality with ST_CoordDim or ST_Zmflag first to avoid surprises.
How do I get M for every vertex of a line?
Expand the line with ST_DumpPoints(line) and apply ST_M to (d).geom.
What is the difference between ST_M and ST_Z?
ST_Z returns the Z (elevation) coordinate; ST_M returns the M (measure) coordinate. A POINT Z has Z but NULL M; a POINT M has M but NULL Z; a POINT ZM has both.
Is M preserved through spatial operations?
Mostly yes, but operations that produce new vertices (buffering, intersection) may lose M. Check with ST_Zmflag after transformation.