ST_SquareGrid — Returns a set of grid squares and cell indices that completely cover the bounds of the geometry argument.
setof record ST_SquareGrid(
float8 size, geometry bounds)
;
Starts with the concept of a square tiling of the plane. For a given planar SRS, and a given edge size, starting at the origin of the SRS, there is one unique square tiling of the plane, Tiling(SRS, Size). This function answers the question: what grids in a given Tiling(SRS, Size) overlap with a given bounds.
The SRS for the output squares is the SRS provided by the bounds geometry.
Doubling or edge size of the square generates a new parent tiling that perfectly fits with the original tiling. Standard web map tilings in mercator are just powers-of-two square grids in the mercator plane.
Availability: 3.1
To do a point summary against a square tiling, generate a square grid using the extent of the points as the bounds, then spatially join to that grid.
WITH bounds AS ( SELECT ST_SetSRID(ST_EstimatedExtent('pointtable', 'geom'), 3857) AS geom ) SELECT Count(*), squares.geom FROM pointtable pts JOIN ST_SquareGrid(1000, bounds.geom) squares GROUP BY squares.geom