ST_FlipCoordinates
What is ST_FlipCoordinates?
ST_FlipCoordinates returns a new geometry in which every vertex has its X and Y values swapped. It is the quickest remedy for data that was loaded in (lat, lon) order when PostGIS expects (lon, lat).
ST_FlipCoordinates(geometry geom) → geometryZ and M ordinates are left untouched. The function operates per-vertex and preserves geometry type, SRID, and dimensionality.
When would you use ST_FlipCoordinates?
Use ST_FlipCoordinates when ingesting data from sources that follow the ISO 19107 or EPSG "latitude, longitude" axis order — some GML feeds, government APIs, and legacy scientific datasets. PostGIS treats geometry(Point, 4326) values as (longitude, latitude), so without flipping, points plotted on a web map land in the wrong hemisphere.
It is also handy in emergency data-patching situations: detect an anomalous extent (for instance longitudes between -90 and 90 and latitudes outside ±90) and apply ST_FlipCoordinates conditionally to correct the geometry column.
FAQs
Does ST_FlipCoordinates change the SRID?
No. Only coordinate values are swapped — SRID and geometry type remain the same. If you need a different CRS, follow up with ST_Transform.
How do I detect whether my data needs flipping?
Inspect the extent with ST_Extent. For data claiming SRID 4326, X should fall within ±180 and Y within ±90. If X exceeds 90 but Y stays within ±180, coordinates are likely swapped.
Is this the same as ST_SwapOrdinates?
No. ST_FlipCoordinates is a shortcut for swapping X and Y only. ST_SwapOrdinates takes an axis pair argument (e.g. 'xy', 'xz', 'ym') and can swap any two ordinates including Z and M.
Does ST_FlipCoordinates affect Z or M values?
No. Only X and Y are swapped. Z and M are preserved untouched.