ST_Reverse
What is ST_Reverse?
ST_Reverse returns the input geometry with its vertex order reversed. Line direction is flipped, polygon ring winding is inverted, and component order in collections is reversed.
ST_Reverse(geometry geom) → geometryThe spatial shape is unchanged — only traversal direction differs. SRID is preserved.
When would you use ST_Reverse?
Use ST_Reverse to flip the direction of a LineString — for instance, aligning a one-way road with the direction of travel, or inverting a measured line before computing M values that start at the correct end. In polygon editing, reversing flips orientation, equivalent to toggling between CW and CCW.
It is also useful in test fixtures where you want to generate permutations of the same geometry with different vertex ordering to verify that downstream code is direction-agnostic.
FAQs
Does ST_Reverse change polygon area or line length?
No. Only vertex order changes — length, area, and spatial position are identical.
How is it different from ST_ForcePolygonCW / CCW?
ST_ForcePolygonCW/CCW enforce a specific orientation regardless of current state. ST_Reverse always flips, so calling it twice is a no-op.
Does ST_Reverse affect Z or M values?
Z values are preserved. M values are preserved but their assignment along the line is effectively inverted — consider recomputing measures with ST_AddMeasure after reversal if M is semantically important.
Is ST_Reverse useful for topology?
Yes. It is often applied to align edge directions in topology datasets, particularly when ingesting unsigned edges that must follow an agreed direction convention.