ST_IsValidTrajectory
What is ST_IsValidTrajectory?
ST_IsValidTrajectory is a PostGIS function that returns true when the input is a valid trajectory — a measured (M) linestring whose M values are strictly increasing along the line. In the PostGIS trajectory model, M encodes time.
ST_IsValidTrajectory(geometry line) → booleanValid input types are LINESTRING M and LINESTRING ZM.
When would you use ST_IsValidTrajectory?
Use ST_IsValidTrajectory before running any of the other trajectory functions (ST_ClosestPointOfApproach, ST_DistanceCPA, ST_CPAWithin) to ensure your data is well-formed. It is also useful as a quality check after ingesting GPS or AIS tracks, where duplicate timestamps or out-of-order fixes can produce invalid trajectories.
1SELECT id
2FROM vessel_tracks
3WHERE NOT ST_IsValidTrajectory(track);FAQs
What exactly does "valid trajectory" mean?
The linestring must have M values that are strictly monotonically increasing — each successive vertex has a greater M than the previous one. Duplicated or non-monotonic M breaks the invariant assumed by the trajectory analytics.
Can trajectories have Z values?
Yes. A valid trajectory is typically LINESTRING ZM where X and Y give the position, Z gives altitude or elevation, and M gives time. PostGIS does not require Z, but encourages it for flight paths, vessel tracks, and drone logs.
What happens if a trajectory has duplicate M?
It is invalid and ST_IsValidTrajectory returns false. Remove duplicates before using trajectory functions, for example by filtering out repeated fixes at identical timestamps or by resampling with a regular cadence.
Is there a way to repair an invalid trajectory?
PostGIS does not provide an automatic repair function. The typical approach is to sort your points by time, drop duplicates, and rebuild the linestring. If small perturbations are acceptable, you can also add a tiny epsilon to tied M values to force strict monotonicity.