Resamples a trajectory by adding or removing points based on a distance or time threshold.
Syntax
trajectory ST_Resample(trajectory traj, config)Parameters
| Parameter | Description |
|---|---|
traj | The trajectory to resample. |
config | The resampling rule in JSON format: {"Rule": "Value"}. Only one rule can be specified per call. |
Config rules
| Rule | Action | Type | Description |
|---|---|---|---|
add_point.distance_lesser | Add points | Floating point | Inserts intermediate points so that the distance between any two consecutive points is less than the specified value. |
add_point.period_lesser | Add points | String (INTERVAL) | Inserts intermediate points so that the time gap between any two consecutive points is less than the specified value. |
drop_point.distance_lesser | Remove points | Floating point | Merges consecutive segments where the distance between all intermediate points and the segment start point is less than the specified value. Only the start and end points of each merged segment are kept. |
drop_point.period_lesser | Remove points | String (INTERVAL) | Merges consecutive segments where the total elapsed time is less than the specified value. Only the start and end points of each merged segment are kept. |
Description
ST_Resample adjusts the point density of a trajectory using one of two strategies:
Add points (
add_point.*): Interpolates new points along the trajectory so that no two consecutive points exceed the specified distance or time threshold. Use this when source data is too sparse.Remove points (
drop_point.*): Merges consecutive short segments into a single segment, retaining only the start and end points of each merged segment. Use this when source data is too dense or noisy.
Examples
Example 1: Add points by distance
Insert intermediate points so that no two consecutive points are more than 3 units apart.
WITH traj AS (
SELECT '{"trajectory":{"version":1,"type":"STPOINT","leafcount":5,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(1 1,10 10,11 11,13 13,15 15)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}'::trajectory a
)
SELECT ST_Resample(a, '{"add_point.distance_lesser":3}') FROM traj;Output (leafcount: 5 → 9, 4 points added):
{"trajectory":{"version":1,"type":"STPOINT","leafcount":9,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(1 1,2.8 2.8,4.6 4.6,6.4 6.4,8.2 8.2,10 10,11 11,13 13,15 15)","timeline":["2000-01-01 00:00:00","2000-01-01 04:48:00","2000-01-01 09:36:00","2000-01-01 14:24:00","2000-01-01 19:12:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}
(1 row)Example 2: Add points by time interval
Insert intermediate points so that no two consecutive points are more than 0.5 day apart.
WITH traj AS (
SELECT '{"trajectory":{"version":1,"type":"STPOINT","leafcount":5,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(1 1,10 10,11 11,13 13,15 15)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}'::trajectory a
)
SELECT ST_Resample(a, '{"add_point.period_lesser":"0.5 day"}') FROM traj;Output (leafcount: 5 → 9, 4 points added):
{"trajectory":{"version":1,"type":"STPOINT","leafcount":9,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(1 1,5.5 5.5,10 10,10.5 10.5,11 11,12 12,13 13,14 14,15 15)","timeline":["2000-01-01 00:00:00","2000-01-01 12:00:00","2000-01-02 00:00:00","2000-01-02 12:00:00","2000-01-03 00:00:00","2000-01-03 12:00:00","2000-01-04 00:00:00","2000-01-04 12:00:00","2000-01-05 00:00:00"]}}
(1 row)Example 3: Remove points by distance
Merge consecutive segments where intermediate points are within 3 units of the segment start. Only the start and end points of each merged segment are kept.
WITH traj AS (
SELECT '{"trajectory":{"version":1,"type":"STPOINT","leafcount":5,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(1 1,10 10,11 11,13 13,15 15)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-03 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}'::trajectory a
)
SELECT ST_Resample(a, '{"drop_point.distance_lesser":3}') FROM traj;Output (leafcount: 5 → 4, 1 point removed):
{"trajectory":{"version":1,"type":"STPOINT","leafcount":4,"start_time":"2000-01-01 00:00:00","end_time":"2000-01-05 00:00:00","spatial":"LINESTRING(1 1,10 10,13 13,15 15)","timeline":["2000-01-01 00:00:00","2000-01-02 00:00:00","2000-01-04 00:00:00","2000-01-05 00:00:00"]}}
(1 row)