ST_ForcePolygonCW — Orients all exterior rings clockwise and all interior rings counter-clockwise.
geometry ST_ForcePolygonCW(geometry geom);
Forces (Multi)Polygons to use a clockwise orientation for their exterior ring, and a counter-clockwise orientation for their interior rings. Non-polygonal geometries are returned unchanged.
Availability: 2.4.0
This function supports 3d and will not drop the z-index.
This function supports M coordinates.
Force a polygon and its hole to use clockwise exterior rings and counter-clockwise interior rings.
WITH data AS (
SELECT 'POLYGON((0 0,4 0,4 4,0 4,0 0),
(1 1,1 3,3 3,3 1,1 1))'::geometry AS geom
)
SELECT geom AS input_polygon,
ST_ForcePolygonCW(geom) AS cw_polygon
FROM data;
POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,1 3,3 3,3 1,1 1)) | POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,3 1,3 3,1 3,1 1))