Checks whether the distance between two trajectory objects on a specified axis is within a given threshold, using the index on the column that stores the first trajectory (traj1).
Syntax
bool ST_{2D|2DT|3D|3DT}DWithin_IndexLeft(trajectory traj1, trajectory traj2, float8 dist);
bool ST_{2D|2DT|3D|3DT}DWithin_IndexLeft(trajectory traj1, trajectory traj2, timestamp ts, timestamp te, float8 dist);Parameters
| Parameter | Description |
|---|---|
traj1 | The trajectory to compare, or the source trajectory from which a sub-trajectory is extracted. The index on the column storing this trajectory is used. |
traj2 | The trajectory to compare, or the source trajectory from which a sub-trajectory is extracted. |
ts | The start of the time range for sub-trajectory extraction. Optional. |
te | The end of the time range for sub-trajectory extraction. Optional. |
dist | The distance threshold. |
Description
The four function variants correspond to different spatial dimensions:
| Variant | Comparison axis |
|---|---|
ST_2DDWithin_IndexLeft | 2D spatial distance (X, Y) |
ST_2DTDWithin_IndexLeft | 2D spatial distance within a time range |
ST_3DDWithin_IndexLeft | 3D spatial distance (X, Y, Z) |
ST_3DTDWithin_IndexLeft | 3D spatial distance within a time range |
When you call the base function ST_ndDWithin(...) to compare two trajectories, the query planner cannot determine which column's index to use, which adds computing overhead.
We recommend that you useST_{2D|2DT|3D|3DT}DWithin_IndexLeft. This function allows you to manually specify to use the index on the column storingtraj1, reducing computing overheads.
Example
The following query uses ST_3DTDWithin_IndexLeft to count trajectories in the sqltr_test_trajs table that fall within a distance of 2 from a reference trajectory. The EXPLAIN output confirms that the GiST index sqltr_test_traj_gist_2dt on the traj column is used.
EXPLAIN SELECT count(*) FROM sqltr_test_trajs WHERE ST_3DTDWithin_IndexLeft(traj, ST_makeTrajectory('STPOINT', 'LINESTRING(-70 -30, -72 -34, -73 -35)'::geometry, '[2000-01-01 00:01:30, 2000-01-01 00:15:00)'::tsrange,
'{"leafcount":3,"attributes":{"velocity": {"type": "integer", "length": 2,"nullable" : true,"value": [120,130,140]}, "accuracy": {"type": "float", "length": 4, "nullable" : false,"value": [120,130,140]}, "bearing": {"type": "float", "length": 8, "nullable" : false,"value": [120,130,140]}, "acceleration": {"type": "string", "length": 20, "nullable" : true,"value": ["120","130","140"]}, "active": {"type": "timestamp", "nullable" : false,"value": ["Fri Jan 01 11:35:00 2010", "Fri Jan 01 12:35:00 2010", "Fri Jan 01 13:30:00 2010"]}}, "events": [{"2" : "Fri Jan 02 15:00:00 2010"}, {"3" : "Fri Jan 02 15:30:00 2010"}]}'), 2);Output:
Aggregate (cost=4341.89..4341.90 rows=1 width=8)
-> Bitmap Heap Scan on sqltr_test_trajs (cost=103.31..4340.56 rows=529 width=0)
Recheck Cond: ('BOX2DT(-75 -37 2000-01-01 00:01:29.999994,-68 -28 2000-01-01 00:15:00)'::boxndf &/#& traj)
Filter: _st_3dtdwithin(traj, '{"trajectory":{"version":1,"type":"STPOINT","leafcount":3,"start_time":"2000-01-01 00:01:30","end_time":"2000-01-01 00:15:00","spatial":"LINESTRING(-70 -30,-72 -34,-73 -35)","timeline":["2000-01-01 00:01:30","2000-01-01 00:08:15","2000-01-01 00:15:00"],"attributes":{"leafcount":3,"velocity":{"type":"integer","length":2,"nullable":true,"value":[120,130,140]},"accuracy":{"type":"float","length":4,"nullable":false,"value":[120.0,130.0,140.0]},"bearing":{"type":"float","length":8,"nullable":false,"value":[120.0,130.0,140.0]},"acceleration":{"type":"string","length":20,"nullable":true,"value":["120","130","140"]},"active":{"type":"timestamp","length":8,"nullable":false,"value":["2010-01-01 11:35:00","2010-01-01 12:35:00","2010-01-01 13:30:00"]}},"events":[{"2":"2010-01-02 15:00:00"},{"3":"2010-01-02 15:30:00"}]}}'::trajectory, '2'::double precision)
-> Bitmap Index Scan on sqltr_test_traj_gist_2dt (cost=0.00..103.18 rows=1586 width=0)
Index Cond: (traj &/#& 'BOX2DT(-75 -37 2000-01-01 00:01:29.999994,-68 -28 2000-01-01 00:15:00)'::boxndf)The Bitmap Index Scan on sqltr_test_traj_gist_2dt confirms that the query uses the GiST index on traj, reducing the number of rows the subsequent filter must evaluate.