ST_ForcePolygonCW

更新时间:
复制 MD 格式

Orients all exterior rings of a polygon or multipolygon clockwise and all interior rings counter-clockwise.

Syntax

geometry ST_ForcePolygonCW(geometry geom);

Parameters

ParameterDescription
geomThe geometry object to reorient.

Description

ST_ForcePolygonCW enforces the standard clockwise-exterior, counter-clockwise-interior ring orientation on polygonal geometries. Use it to normalize vertex winding order before passing geometries to libraries or systems that require a specific orientation, such as GeoJSON consumers or spatial indexing pipelines.

  • Non-polygon geometries are returned unchanged.

  • 3D geometry is supported. Z coordinates are preserved.

  • M coordinates are supported and preserved.

Example

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

Output:

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

The input ring (1 1,2 1,2 2,1 2,1 1) is counter-clockwise. ST_ForcePolygonCW reverses it to (1 1,1 2,2 2,2 1,1 1), which is clockwise.