This topic describes the ST_Angle function. This function calculates the angle between three points or between two vectors that are depicted by four points or by two lines.

Syntax

float  ST_Angle(geometry  point1 , geometry  point2 , geometry  point3 , geometry  point4);
float  ST_Angle(geometry  line1 , geometry  line2);

Parameters

Parameter Description
point1 The first point that you want to specify.
point2 The second point that you want to specify.
point3 The third point that you want to specify.
point4 The fourth point that you want to specify.
line1 The first line that you want to specify.
line2 The second line that you want to specify.

Description

  • If you specify three points, which are named P1, P2, and P3 in sequence, this function calculates the angle clockwise from P1 to P2 and then to P3.
    ST_Angle(P1,P2,P3) = ST_Angle(P2,P1,P2,P3)
  • If you specify four points, which are named P1, P2, P3, and P4 in sequence, this function calculates the angle clockwise from P1 to P2 and from P3 to P4.
  • The angle that is returned is always positive, ranging between 0 and 2π radians. You can use the degrees() function to convert the angle into a degree.
  • If you specify two lines, this function considers the closing points of the lines as four points.

Examples

Calculate the angle between two lines.
SELECT degrees(ST_Angle('LINESTRING(0 0,0 1)'::geometry,'LINESTRING(0 0,1 1)'::geometry));
 degrees
---------
      45
(1 row)