When you work with location-based data from multiple sources, joining datasets on spatial predicates is slow and complex. The grid model solves this by assigning globally unique grid codes to objects in earth space—from the earth's core to the surface. Because any two objects at the same location share the same grid code, you can join disparate datasets on a simple equality predicate, aggregate spatial data into uniform cells, and run efficient multi-precision spatial queries.
The grid model is based on GeoSOT, a discrete, multi-scale spatial subdivision standard.
Use cases
Cross-dataset joins: Assign the same grid code to objects at the same location. Join disparate datasets on that code without complex spatial predicates.
Location aggregation: Group spatial data into uniform grid cells for density analysis or heatmaps.
Multi-precision indexing: Store grid codes at different precision levels on the same row and query at the precision that matches your use case—coarser for region-level queries, finer for point-level queries.
Quick start
This section walks through a complete workflow: install the extension, create a table with grid code columns, compute grid codes, index them, run spatial queries, and uninstall the extension.
Install the extension
CREATE EXTENSION Ganos_GeomGrid CASCADE;Create a table with grid code columns
The geomgrid[] array type stores grid codes. Define one column per precision level to support multi-precision queries on the same row.
CREATE TABLE t_grid(
id integer,
geom geometry, -- source geometry
grid1 geomgrid[], -- grid codes at precision 10
grid2 geomgrid[], -- grid codes at precision 15
grid3 geomgrid[] -- grid codes at precision 26
);Compute grid codes
Insert source geometries, then use ST_AsGrid(geom, precision) to compute grid codes at each precision level.
-- Insert sample points
INSERT INTO t_grid(id, geom)
VALUES (1, ST_GeomFromText('POINT(116.31522216796875 39.910277777777778)', 4490)),
(2, ST_GeomFromText('POINT(116.31522217796875 39.910277776777778)', 4490)),
(3, ST_GeomFromText('POINT(116.31522217797875 39.910277776787778)', 4490)),
(4, ST_GeomFromText('POINT(116.31522227796875 39.910277776775778)', 4490));
-- Compute grid codes at three precision levels
UPDATE t_grid
SET grid1 = ST_AsGrid(geom, 10),
grid2 = ST_AsGrid(geom, 15),
grid3 = ST_AsGrid(geom, 26);Create GIN indexes
GIN (Generalized Inverted Index) indexes are optimized for geomgrid[] array columns. Use GIN rather than GiST because geomgrid[] is an array type—GIN is designed for efficient lookups over array elements and accelerates the =, @>, and && operators used in grid queries.
CREATE INDEX idx_grid_gin1 ON t_grid USING GIN(grid1);
CREATE INDEX idx_grid_gin2 ON t_grid USING GIN(grid2);
CREATE INDEX idx_grid_gin3 ON t_grid USING GIN(grid3);Create one index per precision column so the query planner can use the index that matches the precision level in your WHERE clause.
Query data by grid code
The following table summarizes the available query operators:
| Operator | Meaning | Typical use case |
|---|---|---|
= | Exact match—row's grid code equals the target grid | Point-in-grid lookups |
@> | Intersects with a grid | Intersection queries |
&& | Overlap—row's grid codes overlap with any grid in an array | Multi-grid or geometry-based intersection |
Find rows that reside in a specific grid
Use = to match rows whose grid code exactly equals the target grid.
SELECT id
FROM t_grid
WHERE grid2 = ARRAY[ST_GridFromText('G001310322230230')];Find rows that intersect a grid
Use @> to find rows that intersect with a grid.
SELECT id
FROM t_grid
WHERE grid3 @> ARRAY[ST_GridFromText('G00131032223023031031033223')];Find rows that intersect any of multiple grids
Use && to find rows that overlap with at least one grid in an array.
SELECT id
FROM t_grid
WHERE grid3 && ARRAY[ST_GridFromText('G00131032223023031031211001'),
ST_GridFromText('G00131032223023031031211111')];Find rows that intersect a geometry
Combine ST_AsGrid and && to query using a geometry directly. The geometry is converted to grid codes at query time.
SELECT id
FROM t_grid
WHERE grid3 &&
ST_AsGrid(
ST_GeomFromText('LINESTRING(116.31522216796875 39.910277777777778, 116.31522217797875 39.910277776787778)', 4490),
26
);Uninstall the extension
DROP EXTENSION Ganos_GeomGrid CASCADE;