Computes the similarity between two trajectory objects using the Longest Common Subsequence (LCSS) algorithm. Returns the count of trajectory points that are spatially and temporally consistent between the two trajectories.
Syntax
integer ST_lcsSimilarity(trajectory traj1, trajectory traj2, float8 dist, distanceUnit unit default 'M');
integer ST_lcsSimilarity(trajectory traj1, trajectory traj2, float8 dist, interval lag, distanceUnit unit default 'M');The second overload adds a lag parameter to constrain matching by time tolerance in addition to distance.
Parameters
| Parameter | Type | Description |
|---|---|---|
traj1 | trajectory | The first trajectory object. |
traj2 | trajectory | The second trajectory object. |
dist | float8 | The distance tolerance between two trajectory points. Unit: meters. |
lag | interval | The time tolerance between two trajectory points. Available only in the second overload. |
unit | distanceUnit | The unit of dist. Default: 'M'. Valid values: 'M' (meters), 'KM' (kilometers), 'D' (degrees — valid only when the Spatial Reference System Identifier (SRID) of the trajectory is WGS84, which is 4326 by default). |
How it works
LCSS finds the maximum number of trajectory points that are mutually consistent between two trajectories. Two trajectory points are considered consistent if they are within the specified distance tolerance (dist) and, when lag is provided, within the specified time tolerance.
The function returns the count of consistent trajectory point pairs — not a similarity ratio.

In the example above, trajectory points 1, 3, and 6 meet the consistency conditions, so the function returns 3.
If the SRID of a trajectory object is not specified, it defaults to 4326 (WGS84).
Examples
Distance-only similarity
Compute the similarity between two trajectories using a 100-meter distance tolerance.
With traj AS (
Select ST_makeTrajectory('STPOINT', 'LINESTRINGZ(114.000528 33.588163 54.87, 114.000535 33.588235 54.85, 114.000447 33.588272 54.69, 114.000348 33.588287 54.73, 114.000245 33.588305 55.26, 114.000153 33.588305 55.3)'::geometry,
ARRAY['2010-01-01 11:30'::timestamp, '2010-01-01 11:31', '2010-01-01 11:32', '2010-01-01 11:33','2010-01-01 11:34','2010-01-01 11:35'], NULL) a,
ST_makeTrajectory('STPOINT', 'LINESTRINGZ(114.000529 33.588163 54.87, 114.000535 33.578235 54.85, 114.000447 33.578272 54.69, 114.000348 33.578287 54.73, 114.000245 33.578305 55.26, 114.000163 33.588305 55.3)'::geometry,
ARRAY['2010-01-01 11:29:58'::timestamp, '2010-01-01 11:31:02', '2010-01-01 11:33', '2010-01-01 11:33:09','2010-01-01 11:34','2010-01-01 11:34:30'], NULL) b)
Select st_LCSSimilarity(a, b, 100) from traj;Result:
st_lcssimilarity
-------------------
2
(1 row)Two trajectory points fall within 100 meters of each other, so the function returns 2.
Distance and time tolerance
Add a 30-second time tolerance to apply both spatial and temporal constraints.
With traj AS (
Select ST_makeTrajectory('STPOINT', 'LINESTRINGZ(114.000528 33.588163 54.87, 114.000535 33.588235 54.85, 114.000447 33.588272 54.69, 114.000348 33.588287 54.73, 114.000245 33.588305 55.26, 114.000153 33.588305 55.3)'::geometry,
ARRAY['2010-01-01 11:30'::timestamp, '2010-01-01 11:31', '2010-01-01 11:32', '2010-01-01 11:33','2010-01-01 11:34','2010-01-01 11:35'], NULL) a,
ST_makeTrajectory('STPOINT', 'LINESTRINGZ(114.000529 33.588163 54.87, 114.000535 33.578235 54.85, 114.000447 33.578272 54.69, 114.000348 33.578287 54.73, 114.000245 33.578305 55.26, 114.000163 33.588305 55.3)'::geometry,
ARRAY['2010-01-01 11:29:58'::timestamp, '2010-01-01 11:31:02', '2010-01-01 11:33', '2010-01-01 11:34:15','2010-01-01 11:34:50','2010-01-01 11:34:30'], NULL) b)
Select st_LCSSimilarity(a, b, 100, interval '30 seconds') from traj;Result:
st_lcssimilarity
-------------------
2
(1 row)With the added time constraint, 2 trajectory point pairs satisfy both the 100-meter distance tolerance and the 30-second time tolerance.