ST_Force2D
What is ST_Force2D?
ST_Force2D returns the input geometry stripped down to two dimensions — any Z or M ordinates are discarded. The result always has exactly X and Y per vertex.
ST_Force2D(geometry geom) → geometryThe function is idempotent on already-2D geometry and preserves SRID and geometry type. It is the standard cleanup step before calling OGC predicates that expect planar input.
When would you use ST_Force2D?
Use ST_Force2D when exporting to formats that do not support higher dimensions — most shapefile consumers, older web clients, and some tile generators assume 2D. It is also useful before inserting into a geometry(Point, 4326) column whose type modifier forbids Z or M.
In analytical pipelines, forcing 2D ensures that downstream operations like ST_Buffer or ST_Intersection do not accidentally propagate stale Z values copied from input data.
FAQs
Does ST_Force2D change the SRID?
No. SRID is preserved. Only the Z and M ordinates are removed.
Will it fail if the geometry is already 2D?
No. The function is idempotent — a 2D input is returned unchanged (still as a new geometry value).
What is the difference between ST_Force2D and casting to geometry?
A cast does not strip Z or M. ST_Force2D explicitly removes them. To see the difference, compare ST_NDims(geom) before and after.
Can I preserve M and only drop Z?
No. ST_Force2D drops both. To keep M, combine with ST_Force3DM or use ST_SwapOrdinates to move values between axes before forcing.