PostGISGeometry Editors

ST_SnapToGrid

What is ST_SnapToGrid?

ST_SnapToGrid rounds the vertices of a geometry to an evenly spaced grid, producing lower-precision geometry suitable for deduplication, compression, or topology cleanup.

SQL
1ST_SnapToGrid(geometry geom, float size)geometry
2ST_SnapToGrid(geometry geom, float xsize, float ysize)geometry
3ST_SnapToGrid(geometry geom, float origin_x, float origin_y, float xsize, float ysize)geometry
4ST_SnapToGrid(geometry geom, geometry origin, float xsize, float ysize, float zsize, float msize)geometry

The grid origin defaults to (0, 0). Consecutive duplicate vertices produced by snapping are collapsed, so the output may have fewer vertices than the input.

When would you use ST_SnapToGrid?

Use ST_SnapToGrid to normalise geometry to a target precision — rounding WGS84 coordinates to six decimal places (about 11 cm) before deduplication, or snapping cadastral corners to a 1 cm grid to eliminate near-duplicates that cause topology errors.

It is also useful for generating small vector-tile payloads, where unnecessary precision inflates file size with no visible benefit.

FAQs

Does it guarantee topological validity?

No. Snapping can collapse adjacent vertices and change polygon winding or self-intersection. Run ST_MakeValid afterwards if validity matters.

What is the difference from ST_QuantizeCoordinates?

ST_SnapToGrid rounds to a coarse, explicit grid — usually visible at high zoom. ST_QuantizeCoordinates zeroes trailing mantissa bits for compression with minimal visible change.

Can I snap Z and M values too?

Yes, with the six-argument form that takes explicit Z and M grid sizes.

How do I choose the grid size?

Match your desired precision. For EPSG:4326, 0.000001 is ~11 cm; for EPSG:3857, 0.01 is 1 cm. Over-snapping merges vertices that should remain distinct.