ST_ForcePolygonCCW
What is ST_ForcePolygonCCW?
ST_ForcePolygonCCW reorients polygon rings so that exterior rings are counter-clockwise (CCW) and interior rings (holes) are clockwise (CW). Non-polygonal geometries are returned unchanged.
ST_ForcePolygonCCW(geometry geom) → geometryCCW exterior rings are the GeoJSON RFC 7946 and OGC Simple Features convention for the XY plane.
When would you use ST_ForcePolygonCCW?
Use ST_ForcePolygonCCW before exporting to GeoJSON per RFC 7946, which mandates CCW exterior rings for correct rendering across compliant clients (notably Mapbox GL and MapLibre GL on antimeridian-crossing polygons). It is also useful when feeding polygons to algorithms that assume a particular winding order — some geometry engines return different results for inside-out polygons.
For vector-tile pipelines, matching the expected orientation avoids client-side polygon-fill anomalies such as reversed fills or incorrect mask rendering.
FAQs
What happens if the input is already CCW?
The function is idempotent — already-CCW polygons are returned unchanged in terms of orientation.
Does it affect non-polygonal geometry?
No. Points and lines are returned as-is. Collections pass through and only polygonal members are reoriented.
What about holes?
Holes are forced to clockwise — the reverse of the exterior — which matches the OGC and GeoJSON convention.
How does this relate to ST_ForceRHR?
ST_ForceRHR forces the right-hand rule (exterior CW, holes CCW) — the opposite of ST_ForcePolygonCCW. Use ST_ForceRHR for ESRI shapefile output conventions and ST_ForcePolygonCCW for GeoJSON RFC 7946.