ST_FilterByM
What is ST_FilterByM?
ST_FilterByM is a PostGIS function that removes vertices from a geometry whose M (measure) values fall outside a given range. It returns a geometry containing only the vertices whose M-value is greater than or equal to min and (optionally) less than or equal to max.
ST_FilterByM(geometry geom, double precision min, double precision max = null, boolean returnM = false) → geometryIf returnM = false (the default), the M dimension is stripped from the output. If the input lacks an M dimension, the geometry is returned unchanged.
When would you use ST_FilterByM?
Use ST_FilterByM when M-values encode per-vertex metadata — timestamps on a GPS track, confidence scores along a digitised line, or river-mile markers — and you want to keep only the vertices that meet a threshold. It is the fastest way to drop low-quality points from an otherwise valid geometry without rebuilding it vertex by vertex.
1SELECT ST_FilterByM(track_geom, 0.8) AS high_confidence_track
2FROM vehicle_tracks
3WHERE trip_id = 1024;FAQs
What happens if min is set but max is omitted?
All vertices with M >= min are kept; there is no upper bound. This is the common case when filtering on a minimum quality, confidence, or timestamp threshold.
Does ST_FilterByM always remove the M dimension?
Yes by default. Pass returnM = true to preserve M on the surviving vertices — useful when you want to continue filtering or rendering downstream based on M-values.
What if removing vertices leaves the geometry invalid?
ST_FilterByM does not validate the result. A LineString with fewer than two vertices or a ring with fewer than four becomes invalid. After filtering, check the output with ST_IsValid and consider fallback logic (e.g. returning the original geometry) if the result is degenerate.
Does this work on 3D geometries?
Yes — Z coordinates are preserved on the surviving vertices. The filter only consults the M dimension; X, Y, and Z pass through untouched.