ST_lcsSubDistance
Returns a normalized distance score between the longest common subsequence (LCSS) sub-trajectory of two trajectory objects and the first trajectory. A lower score indicates higher similarity.
Syntax
float8 ST_lcsSubDistance(trajectory traj1, trajectory traj2, float8 dist, distanceUnit unit default 'M');
float8 ST_lcsSubDistance(trajectory traj1, trajectory traj2, float8 dist, interval lag, distanceUnit unit default 'M');Parameters
| Parameter | Description |
|---|---|
traj1 | The first trajectory object. |
traj2 | The second trajectory object. |
dist | The distance tolerance between two trajectory points, in meters. |
lag | The time tolerance between two trajectory points. |
unit | The unit of the distance. Default: 'M'. Valid values: 'M' (meters), 'KM' (kilometers), 'D' (degrees, valid only when the spatial reference system identifier (SRID) of the trajectory object is WGS84, which defaults to EPSG code 4326). |
Description
ST_lcsSubDistance identifies the sub-trajectory of traj1 whose points match those in the LCSS sub-trajectory, then returns the ratio 1 - (LCSS points / traj1 sub-trajectory points). A score of 0 means the trajectories are identical within the LCSS; a score closer to 1 means they diverge more.

In the preceding figure, trajectory points 2, 3, and 5 are in the LCSS sub-trajectory, while trajectory points 2, 3, 4, and 5 are in the sub-trajectory of traj1. The score is 1 - 3/4 = 0.25.
If no SRID is specified for a trajectory object, it defaults to 4326 (WGS84).
Examples
The following examples use two six-point trajectories with the same starting coordinates but diverging paths. Both calls use a distance tolerance of 100 meters.
Without time 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_LCSSubDistance(a, b, 100) from traj;Output:
st_lcssubdistance
--------------------------
0.66666666666666666667
(1 row)With time 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_LCSSubDistance(a, b, 100, interval '30 seconds') from traj;Output:
st_lcssubdistance
-------------------
0.666666666666667
(1 row)