Compresses a trajectory object using the Ramer-Douglas-Peucker (RDP) algorithm based on synchronous Euclidean distance (SED) between trajectory points.
Syntax
trajectory ST_CompressSed(trajectory traj, float8 dist);Parameters
| Parameter | Description |
|---|---|
traj | The original trajectory object. |
dist | The SED offset threshold. Points with an SED offset greater than this value are retained to preserve the spatial trend of the original trajectory. |
How it works
ST_CompressSED calculates the SED at each trajectory point, discards points whose SED offset falls below dist, and returns the compressed trajectory object.
The RDP algorithm operates on two-dimensional (2D) trajectories, where all points lie on a plane. For spatio-temporal trajectories or three-dimensional (3D) polylines, using 2D spatial distance causes a problem: if the trajectory stays at a location for a long period, the segment becomes a line parallel to the t-axis in 3D space but collapses to a single point in 2D space. Applying RDP based on 2D spatial distance then discards the start or end point of this segment, corrupting the 3D trajectory.
SED addresses this by accounting for time. Given a start point (xs, ys, ts), end point (xe, ye, te), and an intermediate point (xm, ym, tm), SED projects a point (xm', ym', tm) onto the line segment and measures the distance between (xm, ym) and (xm', ym'), subject to ts ≤ tm ≤ te. This time-aware distance prevents the corruption that occurs with vertical Euclidean distance when stay time is significant.
When timestamps are present, use ST_CompressSED instead of ST_Compress.
Examples
Basic compression
SELECT st_compressSED(traj, 0.001) AS traj FROM traj_test;Output:
{"trajectory":{"version":1,"type":"STPOINT","leafcount":12,"start_time":"2017-01-15 09:06:39","end_time":"2017-01-15 21:18:30","spatial":"LINESTRING(-179.48077 51.72814,-179.42595 51.80094,-179.39734 51.83398,-179.37474 51.86568,-179.18826 52.10105,-179.16482 52.12996,-179.14599 52.15132,-177.76666 52.85042,-177.47319 52.90084,-177.31238 52.92697,-177.03751 52.97394,-176.68481 53.03327)","timeline":["2017-01-15 09:06:39","2017-01-15 09:34:40","2017-01-15 09:47:38","2017-01-15 09:59:09","2017-01-15 11:30:09","2017-01-15 11:42:00","2017-01-15 11:50:28","2017-01-15 18:01:00","2017-01-15 18:56:22","2017-01-15 19:26:10","2017-01-15 20:15:49","2017-01-15 21:18:30"],"attributes":{"leafcount":12,"sog":{"type":"float","length":8,"nullable":false,"value":[10.5,11.0,10.6,11.2,9.1,9.7,9.9,12.3,12.0,12.2,12.8,12.7]},"cog":{"type":"float","length":8,"nullable":false,"value":[23.3,28.5,29.2,27.1,26.8,30.1,30.0,74.2,81.3,73.4,74.2,72.9]},"heading":{"type":"float","length":8,"nullable":false,"value":[22.0,27.0,24.0,29.0,27.0,26.0,27.0,69.0,72.0,71.0,72.0,73.0]}}}}
(1 row)See also
ST_Compress: Compresses a trajectory using 2D spatial distance only. Use this function when your trajectory data has no timestamps or when stay time is not relevant to your use case.