ST_intersection

Updated at:

Returns an array of sub-trajectories representing the portions of a trajectory that fall within both a specified time range and a specified geometry.

Use ST_intersection to clip trajectory data against a geographic boundary—for example, to identify the segments of a vehicle route that passed through a geofenced area within a given time window.

Syntax

trajectory[] ST_intersection(trajectory traj, tsrange range, geometry g);
trajectory[] ST_intersection(trajectory traj, timestamp t1, timestamp t2, geometry g);

The two overloads accept the time range either as a tsrange value or as separate timestamp start and end values.

Parameters

ParameterTypeOverloadDescription
trajtrajectoryBothThe trajectory object to intersect.
rangetsrangeFirstThe time range.
t1timestampSecondThe start time of the range.
t2timestampSecondThe end time of the range.
ggeometryBothThe geometry object to intersect against.

Description

ST_intersection evaluates the trajectory within the specified time window and returns only the portions that intersect the given geometry.

When the trajectory crosses the geometry boundary multiple times, the function returns multiple sub-trajectories—one for each contiguous segment of intersection. The return type is always trajectory[], even when only a single sub-trajectory is found.

Example

Extract the sub-trajectories from traj_table that intersect a diagonal line segment between 13:00 and 14:00 on January 1, 2010:

SELECT ST_intersection(
    traj,
    '2010-1-1 13:00:00',
    '2010-1-1 14:00:00',
    'LINESTRING(0 0, 5 5, 9 9)'::geometry
)
FROM traj_table;

The query returns a trajectory[] value for each row. If a trajectory crosses the LINESTRING at two separate points within the time range, the array contains two sub-trajectory elements.