ST_ForcePolygonCCW

更新时间:
复制 MD 格式

ST_ForcePolygonCCW orients all exterior rings of a polygon counter-clockwise (CCW) and all interior rings clockwise (CW). Non-polygon geometries are returned unchanged.

Syntax

geometry ST_ForcePolygonCCW(geometry geom);

Parameters

ParameterDescription
geomThe geometry object to reorient.

Description

ST_ForcePolygonCCW enforces the OGC ring-orientation convention on polygon geometries: exterior rings are wound counter-clockwise and interior rings (holes) are wound clockwise. Passing a non-polygon geometry returns it unchanged.

The function preserves all coordinate dimensions: Z coordinates are retained for 3D geometries, and M coordinates are retained for geometries that include a measure dimension.

Example

The following example forces a clockwise polygon to counter-clockwise orientation:

SELECT ST_AsText(ST_ForcePolygonCCW(ST_GeomFromText('POLYGON((1 1,1 2,2 2,2 1,1 1))')));

Output:

           st_astext
--------------------------------
 POLYGON((1 1,2 1,2 2,1 2,1 1))
(1 row)

The input polygon POLYGON((1 1,1 2,2 2,2 1,1 1)) has a clockwise exterior ring. ST_ForcePolygonCCW reverses it to produce POLYGON((1 1,2 1,2 2,1 2,1 1)), which follows the counter-clockwise convention.