ST_SnapToGrid — Snaps a geometry to a grid defined by its cell size and origin.
geometry ST_SnapToGrid(bytea
Geometry, double size);
geometry ST_SnapToGrid(bytea
Geometry, double
sizeX, double
sizeY);
geometry ST_SnapToGrid(bytea
Geometry, double
originX, double
originY, double
sizeX, double
sizeY);
geometry ST_SnapToGrid(bytea
Geometry, bytea
OriginPoint, double
sizeX, double
sizeY, double
sizeZ, double
sizeM);
Snaps a geometry to a grid defined by its cell size and origin. This method has four versions:
First version snaps every coordinate by the same grid size.
Second version just snaps X and Y coordinates.
Third version defines the origin and size of the grid. It just snaps X and Y coordinates.
Fourth version defines the origin by a Point. It snaps X,Y,Z and M coordinates.
![]() | |
GeometryCollection supported |
| 2D | 3D | M |
|---|---|---|
![]() | ![]() | ![]() |
| OGC SFS for SQL. 1.1 (1999) | OGC SFS for SQL. 1.1.0 (2005) | OGC SFS for SQL. 1.2.0 (2006) | SQL-MM Part 3 |
|---|---|---|---|
| - | - | - | - |
SELECT ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('MULTIPOINT (0 0, 1.7 1.2, 0 10)'),0.5));
--Result
MULTIPOINT (0 0, 1.5 1, 0 10)
--3D MultiPoint
SELECT ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('MULTIPOINT (0 0 2, 0 10 6)'),5));
--Result
MULTIPOINT (0 0 0, 0 10 5)
--4D
SELECT ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('MULTIPOINT (0 0 2 2, 0 10 6 8)'),3));
--Result
MULTIPOINT (0 0 3 3, 0 9 6 9)
SELECT ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('LINESTRING (0 0, 0 1, 0 2, 0 10)'),5));
--Result
LINESTRING (0 0, 0 10)
--ST_SnapToGrid(geomA,sizeX,sizeY)
SELECT ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('MULTIPOINT (0 0 2, 1.7 1.2 4, 0 10 4)'),0.5,1));
--Result
MULTIPOINT ((0 0 2), (1.5 1 4), (0 10 4))
--ST_SnapToGrid(geom, originX, originY, sizeX, sizeY)
SELECT ST_AsEWKT(ST_SnapToGrid(ST_GeomFromEWKT('MULTIPOINT (0 0 2, 1.7 1.2 4, 0 10 4)'), 1,1.4,0.5,1));
--Result
MULTIPOINT ((0 0.4 2), (1.5 1.4 4), (0 10.4 4))
--ST_SnapToGrid(geomA, Origin_Point, sizeX, sizeY, sizeZ, sizeM)
SELECT ST_AsEWKT(
ST_SnapToGrid(
ST_GeomFromEWKT('POINT (0.1111 0.2222 2.5555 3.5555)'),
ST_GeomFromEWKT('POINT(1 1 1 1)'),
0.1,0.01,0.001,1));
--Result
POINT (0.1 0.22 2.555 4)