ST_Density returns an integer array where each element is the number of sampling points in the neighborhood of the corresponding trajectory point.
Syntax
integer[] ST_Density(trajectory traj, float dr, interval dt);Parameters
| Parameter | Type | Description |
|---|---|---|
traj | trajectory | The trajectory to analyze. |
dr | float | The spatial range of the neighborhood. |
dt | interval | The time range of the neighborhood. |
How it works
For each point in the trajectory, ST_Density counts all sampling points whose spatial distance from that point is less than dr and whose time difference from that point is less than dt. A point always counts itself.
The spatial distance is calculated using the SRID of the trajectory.
Example
The following example builds a 5-point trajectory and calculates the neighborhood density using a spatial radius of 400,000 units and a time radius of 10 minutes.
SELECT ST_Density(
st_makeTrajectory(
'STPOINT'::leaftype,
ARRAY[1::float8, 2, 3, 4, 5], -- x coordinates
ARRAY[2::float8, 10, 9, 8, 7], -- y coordinates
4326, -- SRID (WGS 84)
ARRAY[
'2010-01-01 11:30'::timestamp,
'2010-01-01 11:31',
'2010-01-01 11:32',
'2010-01-01 11:33',
'2010-01-01 11:34'
]
),
400000, -- dr: spatial range
'10 minute' -- dt: time range
);Result:
st_density
-------------
{1,3,4,4,3}
(1 row)Each value in the array corresponds to one trajectory point:
| Point index | Timestamp | Neighborhood count |
|---|---|---|
| 1 | 11:30 | 1 |
| 2 | 11:31 | 3 |
| 3 | 11:32 | 4 |
| 4 | 11:33 | 4 |
| 5 | 11:34 | 3 |
The middle points (3 and 4) have the highest counts because they are within both spatial and time range of more neighboring points.
What's next
ST_makeTrajectory: Build a trajectory from coordinate arrays and timestamps.
ST_DistanceCPA: Calculate the closest point of approach between two trajectories.