This topic describes the ST_lcsDistance function, which calculates the distance and time difference between two trajectories based on the longest common sub-sequence (LCSS) algorithm.

## Syntax

``````float8 ST_lcsDisatance(trajectory traj1, trajectory traj2, float8 dist, distanceUnit unit default 'M');
float8 ST_lcsDisatance(trajectory traj1, trajectory traj2, float8 dist, interval lag, distanceUnit unit default 'M');``````

## Parameters

Parameter Description
traj1 The trajectory numbered 1.
traj2 The trajectory numbered 2.
dist The tolerance that is allowed for the distance between a pair of trajectory points. Unit: meters.
lag The tolerance that is allowed for the time difference between a pair of trajectory points.
unit The unit that is used to measure the distance. Valid values:
• 'M': meters.
• 'KM': kilometers.
• 'D': degrees. This value is valid only when the WGS84 (4326) spatial reference is used.

## Description

This function uses the LCSS algorithm to calculate the longest common sub-sequence between two trajectories. During the calculation, this function checks the consistency between every two specified trajectory points based on two conditions: distance and time difference.

The return result is calculated based on the following formula: 1- (LCSS)/min(leafcount(traj1), leafcount(traj2)).

In the example shown in the preceding figure, the following three pairs of trajectory points meet the specified conditions: 1, 3, and 6. In this case, the LCSS value is 3, and the return result is 0.4 based on the following calculation: 1 - 3/5 = 0.4.

In most cases, each trajectory has a spatial reference identifier (SRID). If a trajectory does not have an SRID, this function uses the default value 4326 as the SRID of the trajectory.

## Example

``````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_LCSDistance(a, b, 100) from traj;
st_lcsdistance
-------------------
0.666666666666667
(1 row)

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_LCSDistance(a, b, 100, interval '30 seconds') from traj;
st_lcsdistance
-------------------
0.666666666666667
(1 row)

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_LCSDistance(a, b, 100, interval '30 seconds', 'M') from traj;
st_lcsdistance
-------------------
0.666666666666667
(1 row)``````