Calculates the rotation angle at each intermediate point of a trajectory and returns the results as an array of double-precision floating-point numbers.
Syntax
double precision[] ST_Angle(trajectory traj, boolean hasdirection DEFAULT false);Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
traj | trajectory | — | The trajectory object. |
hasdirection | boolean | false | Specifies whether to account for the direction of rotation. When set to true, counterclockwise rotation returns a negative value. |
Description
For a trajectory with n points, ST_Angle computes the rotation angle at each of the n - 2 intermediate points (excluding the first and last points).
The return value at each intermediate point follows these rules:
0 — the trajectory moves in a straight line at that point.
NaN — the trajectory stays at the same position at that point (zero displacement).
When hasdirection is false (the default), all angles are returned as positive values regardless of rotation direction. When hasdirection is true, counterclockwise rotation returns a negative angle.
Examples
The following examples use the same nine-point trajectory. The trajectory has 7 intermediate points, so both queries return an array of 7 values.
Example 1: Without direction (default)
SELECT ST_Angle(
ST_MakeTrajectory(
'STPOINT'::leaftype,
'LINESTRING(0 0, 0 10, 10 10, 20 10, 30 0, 30 0, 20 10, 0 10, 0 0)',
'2000-01-01',
'2000-01-01'::timestamp + '1 day'::interval
* (ST_NPoints('LINESTRING(0 0, 0 10, 10 10, 20 10, 30 0, 30 0, 20 10, 0 10, 0 0)') - 1),
'{}'
)
);Result:
st_angle
-------------------------
{90,0,45,NaN,NaN,45,90}Example 2: With direction
SELECT ST_Angle(
ST_MakeTrajectory(
'STPOINT'::leaftype,
'LINESTRING(0 0, 0 10, 10 10, 20 10, 30 0, 30 0, 20 10, 0 10, 0 0)',
'2000-01-01',
'2000-01-01'::timestamp + '1 day'::interval
* (ST_NPoints('LINESTRING(0 0, 0 10, 10 10, 20 10, 30 0, 30 0, 20 10, 0 10, 0 0)') - 1),
'{}'
),
true
);Result:
st_angle
---------------------------
{90,0,45,NaN,NaN,-45,-90}The last two values change from 45, 90 to -45, -90 because the trajectory turns counterclockwise at those points.