All Products
Search
Document Center

ApsaraDB RDS:ST_Density

Last Updated:Mar 28, 2026

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

ParameterTypeDescription
trajtrajectoryThe trajectory to analyze.
drfloatThe spatial range of the neighborhood.
dtintervalThe 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 indexTimestampNeighborhood count
111:301
211:313
311:324
411:334
511:343

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.