Calculates the dissimilarity between two trajectories using the Longest Common Subsequence (LCSS) algorithm. A lower return value indicates greater similarity between the two trajectories.
Syntax
float8 ST_lcsDistance(trajectory traj1, trajectory traj2, float8 dist, distanceUnit unit default 'M');
float8 ST_lcsDistance(trajectory traj1, trajectory traj2, float8 dist, interval lag, distanceUnit unit default 'M');Parameters
| Parameter | Type | Description |
|---|---|---|
traj1 | trajectory | The first trajectory. |
traj2 | trajectory | The second trajectory. |
dist | float8 | The distance tolerance for matching a pair of trajectory points. Unit: meters. |
lag | interval | The time tolerance for matching a pair of trajectory points. If omitted, only distance is used as the matching condition. |
unit | distanceUnit | The unit for measuring distance. Default: 'M'. Valid values: 'M' (meters), 'KM' (kilometers), 'D' (degrees, valid only when the WGS84 (4326) spatial reference is used). |
How it works
The function applies the LCSS algorithm to find the longest common subsequence between two trajectories. Two trajectory points are considered a match when both of the following conditions are met:
The spatial distance between the points is within the
disttolerance.The time difference between the points is within the
lagtolerance (whenlagis specified).
The return value is calculated using the following formula:
1 - LCSS / min(leafcount(traj1), leafcount(traj2))
In the example above, three trajectory point pairs (1, 3, and 6) meet the matching conditions, so the LCSS value is 3. With min(leafcount(traj1), leafcount(traj2)) = 5, the return value is 1 - 3/5 = 0.4.
If a trajectory has no spatial reference identifier (SRID), the function uses WGS84 (SRID 4326) as the default.
Examples
All examples construct two trajectories using ST_makeTrajectory and compute their LCSS-based distance.
Example 1: Distance tolerance only
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)Example 2: Distance and 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: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)Example 3: Distance tolerance, time tolerance, and explicit unit
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)What's next
ST_makeTrajectory: Construct a trajectory object from geometry and timestamp arrays.
Trajectory overview: Learn about the Ganos trajectory data model and related functions.