Skip to content

Measurement Functions

Idan Sheinberg edited this page Dec 23, 2023 · 25 revisions

ST_Area

Description: Returns the 2D Cartesian (planar) area of a polygonal geometry. The units of length is determined by the geometry's CRS.
Input Argument #1: Geometry (WKB)
Output: Double (The calculated area)

SELECT
 ST_Area(geom) AS square_foot,
 ST_Area(ST_Transform(geom,26986)) AS square_meters 
FROM (
    SELECT ST_GeomFromText(
        'POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))',
        2249
    ) AS geom
)

Result

square_foot square_meters
928.625 86.27243059503235

Note #1
Unlike PostGIS, there's no geography type support.
You cannot cast to a geography to calculate the area based on spheroid distance and curvature.
Thus, you are required to use ST_Transform with your CRS of choice in order to achieve "sensical" results.

Note #2
Considering the previous comment, achieving accurate when measuring large distances (across the globe), might prove challenging. Choose your CRS carefully!

Note #3
For non polygonal geometries, 0.0 is returned.

ST_Length

Description: Returns the 2D Cartesian length of the geometry. The units of length is determined by the geometry's CRS.
Input Argument #1: Geometry (WKB)
Output: Double (The calculated length)

SELECT ST_Length(
  ST_Transform(
    ST_GeomFromText(
      'LINESTRING (-102.37139138506360325 35.87124792259123041, -101.99697242983519629 35.85439029202107974, -101.7265587399479756 36.0059798203529553)',
      4326
    ),32614
  )
) AS meters

Result

meters
63540.63973129354

Note #1
Unlike PostGIS, there's no geography type support.
You cannot cast to a geography to calculate the length based on spheroid distance and curvature.
Thus, you are required to use ST_Transform with your CRS of choice in order to achieve "sensical" results.

Note #2
Considering the previous comment, achieving accurate when measuring large distances (across the globe), might prove challenging. Choose your CRS carefully!

Note #3
For non linear geometries, 0.0 is returned.

ST_Perimeter

Description: Returns the 2D Cartesian perimeter of the geometry. The units of length is determined by the geometry's CRS.
Input Argument #1: Geometry (WKB)
Output: Double (The calculated perimeter)

SELECT ST_Perimeter(
  ST_GeomFromText(
    'POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))',
    2249
  )
) AS ft

Result

ft
122.630744

Note #1
Unlike PostGIS, there's no geography type support.
You cannot cast to a geography to calculate the perimeter based on spheroid distance and curvature.
Thus, you are required to use ST_Transform with your CRS of choice in order to achieve "sensical" results.

Note #2
Considering the previous comment, achieving accurate when measuring large distances (across the globe), might prove challenging. Choose your CRS carefully!

Note #3
For non areal geometries, 0.0 is returned.

ST_Angle

2 Lines variant:

Description: Computes the angle enclosed by the points L1 and L2. Input Argument #1: LineString (WKB)
Input Argument #2: LineString (WKB)
Output: Double (The calculated angle, in radians)

SELECT DEGREES( 
  ST_Angle(
    ST_GeomFromText('LINESTRING(0 0, 0.3 0.7, 1 1)'),
    ST_GeomFromText('LINESTRING(0 0, 0.2 0.5, 1 0)')
   )
);

Result

45.0

3 Points variant:

Description: Computes the angle enclosed by the points P1-P2-P3. Input Argument #1: Point (WKB)
Input Argument #2: Point (WKB)
Input Argument #3: Point (WKB) Output: Double (The calculated angle, in radians)

SELECT DEGREES( 
  ST_Angle(
    ST_GeomFromText('POINT(0 0)'),
    ST_GeomFromText('POINT(10 10)'), 
    ST_GeomFromText('POINT(20 0)')
   )
);

Result

270.0

4 Points variant:

Description: Computes the angle enclosed by the points P1-P2 and P3-P4. Input Argument #1: Point (WKB)
Input Argument #2: Point (WKB)
Input Argument #3: Point (WKB) Input Argument #4: Point (WKB) Output: Double (The calculated angle, in radians)

SELECT DEGREES( 
  ST_Angle(
    ST_GeomFromText('POINT(10 10)'),
    ST_GeomFromText('POINT(0 0)'),
    ST_GeomFromText('POINT(90 90)'),  
    ST_GeomFromText('POINT(100 80)')
   )
);

Result

270.0

ST_Azimuth

Description: Returns the azimuth in radians of the target point from the origin point, or NULL if the two points are coincident
Input Argument #1: Geometry (WKB) Input Argument #1: Geometry (WKB)
Output: Double (The calculated azimuth, in radians)

SELECT 
  DEGREES(
    ST_Azimuth( 
      ST_Point(25, 45,4326), 
      ST_Point(75, 100,4326)
    )
  ) AS deg_a_to_a,
  DEGREES(
    ST_Azimuth(
      ST_Point(75, 100,4326), 
      ST_Point(25, 45,4326)
    )
  ) AS deb_b_to_a

Result

deg_a_to_b deg_b_to_a
42.27368900609373 222.27368900609375

Clone this wiki locally