All Products
Search
Document Center

PolarDB:ST_AccompanyIntervals

Last Updated:Mar 28, 2026

Identifies the time periods during which two trajectories travel within a specified distance of each other.

Use these functions to detect accompanying behavior in mobility data—for example, identifying when vehicles travel in convoy, when people move together in a crowd, or when animals migrate in groups.

  • ST_AccompanyIntervals returns the accompanying time periods as (start_time, end_time) pairs.

  • ST_AccompanyParts returns the actual sub-trajectories on each trajectory during those periods.

Syntax

Table(timestamp start_time, timestamp end_time) ST_AccompanyIntervals(trajectory traj1, trajectory traj2, double precision tol_dist, interval merge_gap default '0 minute', interval min_length default '0 minute', character(1) dist_config defaut 'A');

Table(trajectory traj1, trajectory subtraj2) ST_AccompanyParts(trajectory traj1, trajectory traj2, double precision tol_dist, interval merge_gap default '0 minute', interval min_length default '0 minute', character(1) dist_config defaut 'A');

Parameters

ParameterDescription
traj1The first trajectory.
traj2The second trajectory.
tol_distThe accompanying distance threshold. If the distance between the two trajectories is less than this value, the trajectories are considered to be accompanying each other. The unit is determined by the spatial reference system.
Note

When the spatial reference system identifier (SRID) is 4326, the distance is measured in meters in most cases.

merge_gapST_AccompanyIntervals: If the time gap between two accompanying periods is less than merge_gap, the periods are merged. Merging only occurs when merge_gap is specified. ST_AccompanyParts: If the distance between two groups of accompanying sub-trajectories is less than merge_gap, the groups are merged. Merging only occurs when merge_gap is specified. Default: '0 minute'.
min_lengthIf the duration of an accompanying period is less than min_length after any merging, the period is discarded. Discarding only occurs when min_length is specified. Default: '0 minute'.
dist_configThe distance calculation method. Valid values: 'W', 'C', 'M', 'D', 'A'. Default: 'A'. See the table below.

dist_config values

ValueDescription
'W'Converts horizontal and vertical coordinates to WGS 84 coordinate system points, then calculates distance.
'C'Calculates distance directly using the raw horizontal and vertical coordinates.
'M'Calculates distance in meters. Equivalent to 'W'.
'D'Calculates distance in degrees. Equivalent to 'C'.
'A'Automatically selects 'W' when the SRID is 4326, or 'C' when the SRID is not 4326.

Return values

ST_AccompanyIntervals

ColumnTypeDescription
start_timetimestampThe start time of the accompanying period.
end_timetimestampThe end time of the accompanying period.

ST_AccompanyParts

ColumnTypeDescription
traj1trajectoryThe sub-trajectories extracted from trajectory 1 during the accompanying periods.
traj2trajectoryThe sub-trajectories extracted from trajectory 2 during the accompanying periods.

How it works

Both functions identify the time intervals during which the distance between traj1 and traj2 is less than tol_dist.

ST_AccompanyIntervals processes these intervals as follows:

  1. Identifies all time intervals where the distance is below tol_dist.

  2. If the time gap between two intervals is less than merge_gap, merges them into a single interval.

  3. If a resulting interval's duration is less than min_length, discards it.

  4. Returns the remaining intervals as (start_time, end_time) pairs.

ST_AccompanyParts follows the same logic, but instead of returning time periods, it extracts and returns the corresponding sub-trajectories from both traj1 and traj2.

Choose ST_AccompanyIntervals when you need time periods only. Choose ST_AccompanyParts when you need the actual trajectory segments for further spatial analysis.

Examples

Find accompanying time periods

The following example creates two trajectories and returns the time periods during which they travel within a distance of 0.5 units.

With traj AS
(
SELECT ST_MakeTrajectory('STPOINT','LINESTRING(0 0, 1 0, 0 0)'::geometry, '2000-01-01'::timestamp, '2000-01-03', NULL) as a, ST_MakeTrajectory('STPOINT','LINESTRING(0 0, 0 0, 0 0)'::geometry, '2000-01-01'::timestamp, '2000-01-03', NULL) as b
)
SELECT (ST_AccompanyIntervals(a,b,0.5)).* from traj;

The two trajectories accompany each other at the start and end of the time range, but diverge in the middle. The result contains two separate periods:

      start_time      |      end_time
---------------------+---------------------
 2000-01-01 00:00:00 | 2000-01-01 12:00:00
 2000-01-02 12:00:00 | 2000-01-03 00:00:00
(2 rows)

Merge nearby time periods

Setting merge_gap to '2 day' merges the two periods above—since the gap between them is less than 2 days—into a single continuous period.

With traj AS
(
SELECT ST_MakeTrajectory('STPOINT','LINESTRING(0 0, 1 0, 0 0)'::geometry, '2000-01-01'::timestamp, '2000-01-03', NULL) as a, ST_MakeTrajectory('STPOINT','LINESTRING(0 0, 0 0, 0 0)'::geometry, '2000-01-01'::timestamp, '2000-01-03', NULL) as b
)
SELECT (ST_AccompanyIntervals(a,b,0.5,'2 day')).* from traj;
      start_time      |      end_time
---------------------+---------------------
 2000-01-01 00:00:00 | 2000-01-03 00:00:00
(1 row)

Get sub-trajectories during accompanying periods

Use ST_AccompanyParts to retrieve the actual trajectory segments, not just the time periods.

With traj AS
(
SELECT ST_MakeTrajectory('STPOINT','LINESTRING(0 0, 1 0, 0 0)'::geometry, '2000-01-01'::timestamp, '2000-01-03', NULL) as a, ST_MakeTrajectory('STPOINT','LINESTRING(0 0, 0 0, 0 0)'::geometry, '2000-01-01'::timestamp, '2000-01-03', NULL) as b
)
SELECT (ST_AccompanyParts(a,b,0.5)).* from traj;

The result contains two rows, each holding the sub-trajectory pair for one accompanying period:

 {"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-01 12:00:00","spatial":"LINESTRING(0 0,0.5 0)","timeline":["2000-01-01 00:00:00","2000-01-01 12:00:00"]}} | {"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-01 12:00:00","spatial":"LINESTRING(0 0,0 0)","timeline":["2000-01-01 00:00:00","2000-01-01 12:00:00"]}}
 {"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-02 12:00:00","end_time":"2000-01-03 00:00:00","spatial":"LINESTRING(0.5 0,0 0)","timeline":["2000-01-02 12:00:00","2000-01-03 00:00:00"]}} | {"trajectory":{"version":1,"type":"STPOINT","leafcount":2,"start_time":"2000-01-02 12:00:00","end_time":"2000-01-03 00:00:00","spatial":"LINESTRING(0 0,0 0)","timeline":["2000-01-02 12:00:00","2000-01-03 00:00:00"]}}
(2 rows)

What's next