The grid model is a discrete, multi-scale location identification system built on GeoSOT. It assigns a globally unique grid code to every object within earth space—from the earth's core to the surface—so that the object can be associated with any data collected from the same location.
Quick start
This section walks through the full workflow: install the extension, create a table, generate grid codes, index them, and run spatial queries.
Step 1: Install the extension
CREATE EXTENSION Ganos_GeomGrid WITH SCHEMA public CASCADE;Step 2: Create a table with grid code columns
Grid codes are stored as geomgrid[] arrays alongside the source geometry. Define one column per precision level you plan to use.
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
);Step 3: Insert data and compute grid codes
Insert rows with source geometries, then populate the grid code columns using ST_AsGrid(geom, precision).
-- Insert source geometries (SRID 4490)
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));
-- Generate 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);Step 4: Create indexes on grid code columns
Grid codes are stored as arrays (geomgrid[]). Create GIN (Generalized Inverted Index) indexes on each grid code column to support array containment and overlap queries.
-- Create GIN indexes on each precision-level column
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);Step 5: Query data by grid code
The following examples cover the three most common query patterns.
Which objects fall within a specific grid cell?
Use the = operator to match rows whose grid code array exactly equals the target cell.
SELECT id
FROM t_grid
WHERE grid2 = ARRAY[ST_GridFromText('G001310322230230')];Which objects intersect a given grid cell?
Use the @> operator to find rows whose grid code array contains the target cell.
SELECT id
FROM t_grid
WHERE grid3 @> ARRAY[ST_GridFromText('G00131032223023031031033223')];Which objects intersect any cell in a set of grid cells?
Use the && operator to find rows that overlap with at least one cell in the array.
SELECT id
FROM t_grid
WHERE grid3 && ARRAY[ST_GridFromText('G00131032223023031031211001'),
ST_GridFromText('G00131032223023031031211111')];Which objects intersect the grid cells covered by a geometry?
Pass a geometry directly to ST_AsGrid to generate the target grid array on the fly, then use && to find overlapping rows.
SELECT id
FROM t_grid
WHERE grid3 &&
ST_AsGrid(
ST_GeomFromText('LINESTRING(116.31522216796875 39.910277777777778, 116.31522217797875 39.910277776787778)', 4490),
26
);Step 6: Remove the extension
DROP EXTENSION Ganos_GeomGrid CASCADE;