All Products
Search
Document Center

PolarDB:ST_Density

Last Updated:Mar 28, 2026

Calculates the density of a trajectory, defined as the number of trajectory points in the spatiotemporal neighborhood of each point.

Syntax

integer[] ST_Density(trajectory traj, float dr, interval dt);

Parameters

ParameterTypeDescription
trajtrajectoryThe trajectory to evaluate.
drfloatThe spatial range of the neighborhood. Distance is calculated based on the spatial reference identifier (SRID) of the trajectory.
dtintervalThe time range of the neighborhood.

Description

ST_Density returns an integer array where each element is the density of the corresponding trajectory point — the count of sampling points (including the point itself) that fall within the spatiotemporal neighborhood.

A point P is in the neighborhood of point Q if both conditions hold:

  • The spatial distance between P and Q is less than dr. Distance is computed using the SRID of the trajectory.

  • The time difference between P and Q is less than dt.

Each point always counts itself, so the minimum density value is 1.

Example

SELECT ST_Density(
  st_makeTrajectory(
    'STPOINT'::leaftype,
    ARRAY[1::float8, 2, 3, 4, 5],
    ARRAY[2::float8, 10, 9, 8, 7],
    4326,
    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,
  '10 minute'
);

Output:

 st_density
-------------
 {1,3,4,4,3}
(1 row)

The trajectory has 5 points with SRID 4326, timestamps from 11:30 to 11:34, a spatial neighborhood of 400,000 units, and a time neighborhood of 10 minutes. The result {1,3,4,4,3} gives the density for each point in order: point 1 has 1 neighbor (itself), points 2–4 are clustered together with higher densities, and point 5 has 3 neighbors.