All Products
Search
Document Center

ApsaraDB RDS:ST_AsGrid

Last Updated:Mar 28, 2026

Returns an array of geographic grid codes that intersect with a given geometry.

Syntax

geomgrid[] ST_AsGrid(geometry geom, integer precision, bool degenerated default false);

Parameters

ParameterTypeDescription
geomgeometryThe input geometry.
precisionintegerThe precision level. Valid values: 1 to 32. A larger value indicates a more accurate query result.
degeneratedbooleanSpecifies whether to use a degenerated grid. Default: false. See Usage notes for behavioral differences.

Usage notes

Coordinate system requirement: The input geometry must use the CGC2000 spatial reference system (SRID 4490). If the geometry uses a different spatial reference system, ST_Transform is called automatically to convert its coordinates to CGC2000.

Standard grid vs. degenerated grid: The degenerated parameter controls whether a degenerated grid is used:

  • false (default): Uses the standard grid.

  • true: Uses a degenerated grid.

The following figure illustrates the difference between a degenerated grid and a standard grid.

退化网格

The following figure shows how geographic grids intersect with a point, a line, and a polygon.

网格编码图

Examples

All examples wrap ST_AsGrid in ST_AsText to display the geomgrid[] result as text.

Point at precision level 15

The following example returns the grid codes for a point at precision level 15.

SELECT ST_AsText(ST_AsGrid(
    ST_GeomFromText('POINT(116.31522216796875 39.910277777777778)', 4490), 15));
     st_astext
--------------------
 {G001310322230230}

Line at precision level 18

The following example returns the grid codes for a line segment at precision level 18. The line intersects multiple grid cells.

SELECT ST_AsText(ST_AsGrid(
    ST_GeomFromText('LINESTRING(122.48077 51.72814,122.47416 51.73714)', 4490), 18));
                  st_astext
--------------------------------------------------------------------------------
 {G001331032213300011,G001331032213300013,G001331032213122320,G001331032213122322,
G001331032213300100,G001331032213122303,G001331032213122321,G001331032213122312}

The line intersects 8 grid cells at precision level 18.

Polygon — standard grid (degenerated = false)

The following example returns the grid codes for a polygon using the standard grid (degenerated = false), which includes all cells that intersect the polygon's boundary or interior.

SELECT ST_AsText(ST_AsGrid(
    ST_GeomFromText('POLYGON((-0.08077 -0.02814, 0.0482 -0.03, 0.07426 0.03724, -0.08077 -0.02814))', 4490), 15));
                          st_astext
--------------------------------------------------------------------------------
 {G000000000000000,G000000000000001,G000000000000003,G000000000000010,G000000000
000011,G000000000000012,G000000000000013,G000000000000031,G000000000000102,G0000
00000000120,G100000000000000,G200000000000000,G200000000000001,G200000000000002,
G200000000000003,G200000000000010,G200000000000011,G200000000000012,G20000000000
0013,G300000000000000,G300000000000001,G300000000000002,G300000000000003,G300000
000000010,G300000000000011,G300000000000012,G300000000000013,G300000000000102}

The standard grid returns 28 grid codes.

Polygon — degenerated grid (degenerated = true)

The following example returns the grid codes for the same polygon using the degenerated grid (degenerated = true), which produces fewer results.

SELECT ST_AsText(ST_AsGrid(
    ST_GeomFromText('POLYGON((-0.08077 -0.02814, 0.0482 -0.03, 0.07426 0.03724, -0.08077 -0.02814))', 4490), 15, true));
                         st_astext
--------------------------------------------------------------------------------
 {G000000000000000,G000000000000001,G000000000000003,G00000000000001,G0000000000
00031,G000000000000102,G000000000000120,G100000000000000,G20000000000000,G200000
00000001,G30000000000000,G30000000000001,G300000000000102}

The degenerated grid returns 13 grid codes — fewer than the standard grid for the same polygon and precision.