ST_SwapOrdinates
What is ST_SwapOrdinates?
ST_SwapOrdinates swaps any two ordinates of a geometry. The pair is specified as a two-character string from the set {x, y, z, m}. Available from PostGIS 2.2.
ST_SwapOrdinates(geometry geom, cstring ords) → geometryValid pairs include 'xy', 'xz', 'xm', 'yz', 'ym', and 'zm'. The geometry must have the requested ordinates present.
When would you use ST_SwapOrdinates?
Use ST_SwapOrdinates when fixing up data where Z and M were loaded into the wrong slots — common with CAD imports that put elevation in M and chainage in Z. Swapping with 'zm' moves each value to its correct ordinate.
It is more expressive than ST_FlipCoordinates, which only swaps X and Y. For anything involving Z or M swaps, ST_SwapOrdinates is the right tool.
FAQs
What happens if the geometry does not have the requested ordinates?
PostGIS raises an error. Check with ST_HasZ or ST_HasM before calling, or use ST_Force3DZ / ST_Force3DM to add the ordinate first.
Is it the same as ST_FlipCoordinates?
ST_SwapOrdinates(geom, 'xy') is equivalent to ST_FlipCoordinates(geom). The main advantage of ST_SwapOrdinates is generality — any two ordinates can be swapped.
What version introduced ST_SwapOrdinates?
PostGIS 2.2.
Does it preserve SRID and geometry type?
Yes. Only coordinate values are reordered.