Skip to main content

GridX Function Reference

Every GridX function runs on every compute. GeoBrix picks the implementation level for the compute. See Surfaces & Levels. Each function section shows the surfaces it runs on.

Complete reference for all GridX discrete-global-grid functions — CARTO Quadbin v0 and British National Grid (BNG).

Scala tags

A Scala tag on a function means the GeoBrix JAR has a faster JVM form of it. On dedicated compute with the JAR, GeoBrix runs that form for you. You still call the SQL or Python function. See Surfaces & Levels and Functions the JAR speeds up.

Overview​

GridX is GeoBrix's discrete-global-grid indexing package. It ships CARTO quadbin v0 for web-mercator-aligned global indexing, British National Grid (BNG) for Great Britain workloads, and custom user-defined grids. It pairs with the product's native H3 for global hex indexing.

  • Quadbin (CARTO v0) — a global zoom-indexed tile addressing scheme aligned with web-mercator slippy maps, compatible with CARTO's CDB_QuadKey IDs. Cell (z, x, y) coordinates align with the same XYZ tile grid that PMTiles / MVT readers consume — natural for slippy-map heatmaps and global analytics.
  • BNG (British National Grid) — the Ordnance Survey National Grid (OSGB36) used in Great Britain for spatial indexing and location-based services. Specialized for UK-based spatial data.
  • Custom user-defined grids — define a regional or equal-area grid and get cell math, polyfill, and tessellation on it.
Import paths
  • Quadbin: databricks.labs.gbx.gridx.quadbin
  • BNG: databricks.labs.gbx.gridx.bng
  • Custom grids: databricks.labs.gbx.gridx.custom
  • H3: databricks.labs.gbx.gridx.h3
  • Helpers: databricks.labs.gbx.gridx.helpers

One call registers all grids: register(spark).

Key Features​

  • Grid Cell Operations: Create, manipulate, and query BNG grid cells
  • Area Calculations: Calculate areas of grid cells at different precisions (returns square kilometres)
  • Coordinate Conversion: Convert between grid references and coordinates
  • Spatial Indexing: Use BNG or quadbin for efficient spatial indexing
  • Multi-Resolution Support: Work with different grid resolutions (BNG: 1–6 integer indices; Quadbin: zoom 0..26)
  • K-Ring / K-Loop Neighbourhoods: Filled rings and hollow rings for both grid systems
  • Polyfill and Tessellation: Cover geometries with cells; tessellation returns per-cell clipped chip geometries
  • Custom Grids: Define your own regional or equal-area grid systems (cell math, polyfill, tessellation)
Coordinate reference systems

GridX functions operate in their grid's fixed native CRS and do not take an explicit CRS parameter for cell operations. BNG (gbx_bng_*) functions work in EPSG:27700 (OSGB36); output geometries are plain WKB in EPSG:27700 — assign the SRID when reading them back (e.g. ST_GeomFromWKB(bng_geom, 27700)). H3 and quadbin functions work in EPSG:4326 (WGS84 lon/lat). Raster-to-grid functions (gbx_rst_*_rastertogrid*) auto-reproject the input raster to the grid's native CRS when the raster carries a different CRS. See Coordinate reference systems for the full CRS model.

Setup​

With GeoBrix already installed, register the functions once per session. One call registers every GridX grid:

from databricks.labs.gbx import register

register(spark) # every gbx_bng_*, gbx_quadbin_*, gbx_custom_* and gbx_h3_* SQL function

The Python API also registers a function on its first call.

The examples on this page read from ten canonical DataFrames, one per fixture group. Each is available as a temp view for SQL examples. Point the placeholders at your own data to reproduce the examples:

View / DataFrameSchemaBacked byBacks
bng_cellscellid STRINGInline literal 'TQ3080' (1km cell, central London, EPSG:27700)BNG scalar ops: bng_aswkb, bng_aswkt, bng_cellarea, bng_centroid, bng_kring, bng_kloop
bng_cell_pairscellid1 STRING, cellid2 STRINGInline literals 'TQ3080', 'TQ3081' (adjacent cells, distance = 1)bng_distance, bng_euclideandistance
bng_pointseasting INT, northing INT, geom STRINGEasting 530000, northing 180000, WKT point (EPSG:27700)bng_pointascell, bng_eastnorthasbng
bng_polygonsgeom STRING3km × 3km BNG polygon in EPSG:27700 (London area)bng_geomkring, bng_geomkloop, bng_polyfill, bng_tessellate, and the 5 *explode generators
bng_chipschip STRUCT<cellid STRING, core BOOLEAN, chip BINARY>9 tessellation chips from the BNG polygon at resolution 3bng_cellintersection, bng_cellunion, bng_cellintersection_agg, bng_cellunion_agg
quadbin_cellscell LONGInline literal 5233961839712272383 (San Francisco, zoom 10)quadbin_aswkb, quadbin_centroid, quadbin_resolution, quadbin_kring, quadbin_cellunion
quadbin_cell_pairscell1 LONG, cell2 LONGTwo cells at (0.0, 0.0, z10) and (0.0, 0.1, z10) — distance = 1quadbin_distance
quadbin_polygonsgeom STRINGWGS84 polygon (-1,-1) → (1,1)quadbin_polyfill, quadbin_tessellate
quadbin_kring_cellscell LONG9 cells from kring(SF-z10, k=1)quadbin_cellunion_agg
custom_gridsgrid STRUCT, cell LONG, point STRINGBNG-like custom grid; cell at POINT(530000 180000), resolution 5All 7 custom_* functions

All ten views are built from inline literals in the doc-test fixture helpers (no external files or /Volumes dependency, so the examples run anywhere).

Map any GridX cell set with plot_static

vizx.plot_static renders a column of cell ids straight from a Spark DataFrame as a static, GitHub-renderable map over a basemap. It is the one helper that covers every GridX grid: pass grid_system="quadbin", "bng", or "custom" (as well as "h3"). Each grid's native CRS is handled for you (e.g. BNG's EPSG:27700 is reprojected for the basemap), and "custom" takes the same grid spec via grid_conf=. It's the quickest way to eyeball the cells the functions below produce. See plot_static for the full parameter list.


Examples — Conventions​

How to read the tabs​

Every function on this page shows one example on each surface it runs on:

TabSurface
SQLSession SQL, after register(spark)
Unity Catalog SQLA <catalog>.<schema>.gbx_* function, where the function exists in Unity Catalog
PythonThe gridx.<grid> Python API

The Scala tag has no tab: it links to Functions the JAR speeds up.

The tabs operate on the same input fixture with the same arguments. Where the output differs in form, the affected tab carries a labeled :::note. A difference without a label is a documentation error.

In each Python example, df = spark.table("<view>") or an equivalent inline spark.sql(...) call loads the canonical fixture. Each SQL example reads FROM <view> or uses an inline literal — no separate CREATE TEMP VIEW step is shown.

Output representation​

The output cells in every function table follow a uniform convention so readers can compare tabs at a glance.

Cell-id strings — BNG cell-ids (e.g. 'TQ3080') and quadbin cells (e.g. 5233961839712272383) are shown in full when short.

Binary geometry ([E]WKB) — geometry returned as BINARY is elided with one token and a format annotation. Use (WKB binary) when no SRID is embedded; (EWKB binary) when an SRID is embedded (quadbin functions embed SRID 4326):

... (WKB binary)
... (EWKB binary)

WKT strings — short WKT strings are shown in full; longer strings are truncated with a type annotation:

POLYGON ((530000 180000, ...))
POLYGON (... (WKT)

Cell-id arrays — arrays of cell-ids show the first few entries followed by ... when the array is long:

['TQ2979', 'TQ2980', 'TQ2981', ..., 'TQ3181'] (9 cells)

Doubles / numeric scalars — shown as the real value (e.g. 1.0 for bng_cellarea).

Cell width — output cells are capped at approximately 60 characters. A longer value is truncated with ... and annotated with its type.

Identical values — when all tabs produce the same result (e.g. 'TQ3080' from bng_pointascell), each tab shows that value identically with the same annotation. Real differences are called out in a labeled :::note.

BNG domain rules​

The following rules apply to all BNG functions (gbx_bng_*):

  • Coordinate system — BNG geometry inputs (points and polygons) must be in EPSG:27700 eastings/northings. WGS84 lon/lat coordinates (e.g. POINT(-0.1278 51.5074)) yield empty arrays or null results — this is the most common source of confusion.
  • Resolution — accepts integer indices ±1..±6 or string keys from the resolution map (e.g. '1km', '100m', '10m'). The indices are 1 = 100km, 2 = 10km, 3 = 1km, 4 = 100m, 5 = 10m, 6 = 1m, and negative values are quadrants. Never pass metres-as-integer (e.g. 1000) — that interpretation is not supported by BNG.getResolution and raises an error.
  • bng_cellarea returns square kilometres — not square metres. A 1km cell returns 1.0.
  • Cell-id format — standard BNG grid references (e.g. 'TQ3080'). At resolution 3 (1km), the format is two letters + four digits; the resolution level is encoded in the string length.

Explode generators​

The explode functions return one row per cell. They are table functions: call them in the FROM clause with LATERAL, at every implementation level.

SELECT t.*
FROM (SELECT 'TQ3080' AS cell, 1 AS k) src,
LATERAL gbx_bng_kringexplode(src.cell, src.k) t

The Python entry takes the input DataFrame first and returns it joined with the generated rows:

from databricks.labs.gbx.gridx.bng import functions as bx

bx.bng_kringexplode(df, "cell", 1) # one row per cell, beside the input columns

The cell ids stay BIGINT (quadbin, custom grid, H3) or STRING (BNG) end to end. A NULL argument and a geometry that cannot be read give no rows. A resolution the grid does not define, or an unknown mode, raises and names the value.


Quadbin (CARTO v0)​

Quadbin runs on the Python level, with identical gbx_quadbin_* SQL names on every compute. It uses the quadbin library and shapely. Geometry outputs are EWKB with SRID 4326.

GeoBrix v0.4.0 adds a gridx/quadbin subpackage implementing the CARTO quadbin v0 64-bit packed (z, x, y) tile encoding used by Snowflake, dbt, Felt, and CARTO. Coordinates are EPSG:4326 lon/lat on the user-facing API; cells are encoded as web-mercator XYZ tiles internally. Resolutions range from 0 (whole world) to 26 (sub-metre).

Registration

Quadbin functions are under gridx.quadbin, independent of gridx.bng. register(spark) installs the gbx_quadbin_* SQL functions.

CARTO Quadbin v0 cells encode (z, x, y) web-mercator tile coordinates as a single BIGINT. Cell IDs are interoperable with CARTO's CDB_QuadKey and align with the slippy-map tile grid used by gbx_rst_xyzpyramid and gbx_st_asmvt_pyramid.

Resolution (zoom) range:

  • gbx_quadbin_pointascell, gbx_quadbin_resolution, gbx_quadbin_kring, gbx_quadbin_distance accept zoom 0..26.
  • gbx_quadbin_polyfill and gbx_quadbin_tessellate accept zoom 0..20.

quadbin_pointascell​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_pointascell(longitude, latitude, resolution)BIGINT
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_pointascell(longitude, latitude, resolution)BIGINT
Pythongridx.quadbin.functions.quadbin_pointascell(longitude, latitude, resolution)Column
ParameterSQL typeUC typeDefaultDescription
longitudeDOUBLEDOUBLELongitude in EPSG:4326 degrees.
latitudeDOUBLEDOUBLELatitude in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 26.

Convert a lon/lat coordinate (EPSG:4326) to the quadbin cell containing it at the given zoom.

Implementation

Powered by the quadbin package (numpy-vectorized encoder). Encodes WGS84 lon/lat to a quadbin cell at the given zoom; includes the antimeridian and pole tile clamp.

Returns:

  • BIGINT quadbin cell ID
Since v0.4.0
SELECT gbx_quadbin_pointascell(-122.4194, 37.7749, 10) as sf_cell;
Example output
+-------------------+
|sf_cell |
+-------------------+
|5233961839712272383|
+-------------------+

quadbin_aswkb​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_aswkb(cell)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_aswkb(cell)BINARY
Pythongridx.quadbin.functions.quadbin_aswkb(cell)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe quadbin cell id.

Return the quadbin cell footprint as EWKB (SRID=4326) — the four-corner polygon of the tile in lon/lat.

Implementation

Powered by the quadbin package + shapely. Cell boundary polygon as EWKB (SRID 4326).

Returns:

  • Binary EWKB polygon (SRID-tagged 4326)
Since v0.4.0
SELECT gbx_quadbin_aswkb(gbx_quadbin_pointascell(-122.4194, 37.7749, 10)) AS wkb;
Example output
+--------+
|wkb |
+--------+
|[binary]|
+--------+
... (EWKB binary — quadbin cell footprint polygon, SRID 4326)

quadbin_centroid​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_centroid(cell)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_centroid(cell)BINARY
Pythongridx.quadbin.functions.quadbin_centroid(cell)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe quadbin cell id.

Return the quadbin cell centroid as an EWKB POINT (SRID=4326).

Implementation

Powered by the quadbin package + shapely. Cell centroid (bbox-corner mean) as EWKB point (SRID 4326).

Returns:

  • Binary EWKB point (SRID-tagged 4326)
Since v0.4.0
SELECT gbx_quadbin_centroid(gbx_quadbin_pointascell(-122.4194, 37.7749, 10)) AS centroid;
Example output
+--------+
|centroid|
+--------+
|[binary]|
+--------+
... (EWKB binary — POINT at SF z10 cell centroid, SRID 4326)

quadbin_resolution​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_resolution(cell)INT
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_resolution(cell)INT
Pythongridx.quadbin.functions.quadbin_resolution(cell)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe quadbin cell id.

Return the resolution (zoom) of a quadbin cell.

Implementation

Powered by the quadbin package (numpy-vectorized). Extracts the zoom level from a cell id.

Returns:

  • INT zoom level (0..26)
Since v0.4.0
SELECT gbx_quadbin_resolution(gbx_quadbin_pointascell(-122.4194, 37.7749, 10)) AS z;
Example output
+--+
|z |
+--+
|10|
+--+

quadbin_polyfill​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_polyfill(geom, resolution)array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_polyfill(geom, resolution)array<bigint>
Pythongridx.quadbin.functions.quadbin_polyfill(geom, resolution)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 20.

Polyfill a geometry's bounding box with all quadbin cells at the given zoom.

Implementation

Powered by the quadbin package + shapely. Enumerates the cells covering the geometry's bounding box at the resolution.

Returns:

  • ARRAY<BIGINT> of cell IDs covering the bbox
Since v0.4.0
SELECT gbx_quadbin_polyfill('POLYGON((-1 -1, 1 -1, 1 1, -1 1, -1 -1))', 5) AS cells;
Example output
+--------------------------+
|cells |
+--------------------------+
|[5211790668774506495, ...]|
+--------------------------+
... (4 cells covering the WGS84 polygon at zoom 5)

quadbin_kring​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_kring(cell, k)array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_kring(cell, k)array<bigint>
Pythongridx.quadbin.functions.quadbin_kring(cell, k)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe quadbin cell id.
kINTINTThe ring distance: 0 is the cell alone, 1 adds its eight neighbours.

Return all cells within Chebyshev distance k of a quadbin cell (inclusive of the center cell).

Implementation

Powered by the quadbin package. All cells within Chebyshev distance k (inclusive).

Returns:

  • ARRAY<BIGINT> of cell IDs (length (2k+1)^2)
Since v0.4.0
SELECT gbx_quadbin_kring(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1) AS kring;
Example output
+-------------------------------------+
|kring |
+-------------------------------------+
|[5233961839712272383, ..., (9 cells)]|
+-------------------------------------+
... (9 cells: SF z10 center plus 8 surrounding cells at k=1)

quadbin_kloop​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_kloop(cell, k)array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_kloop(cell, k)array<bigint>
Pythongridx.quadbin.functions.quadbin_kloop(cell, k)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe quadbin cell id.
kINTINTThe loop distance: only the cells at exactly this distance are returned.

Return the hollow ring of quadbin cells at exactly Chebyshev distance k from the center cell. At k=1 the ring contains the 8 surrounding cells (center excluded); at k=0 the result is a single-element array containing the center cell.

Implementation

Powered by the quadbin package. The cells at exactly Chebyshev distance k (center excluded for k ≥ 1).

Returns:

  • ARRAY<BIGINT> of cell IDs at exactly distance k (up to (2k+1)^2 - (2(k-1)+1)^2 cells)
Since v0.5.1
SELECT gbx_quadbin_kloop(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1) AS kloop;
Example output
+----------------+
|kloop |
+----------------+
|[..., (8 cells)]|
+----------------+
... (8 cells: hollow ring at k=1, SF z10 center excluded)

quadbin_tessellate​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_tessellate(geom, resolution)array<struct<cell:bigint,geom:binary>>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_tessellate(geom, resolution)array<struct<cell:bigint,geom:binary>>
Pythongridx.quadbin.functions.quadbin_tessellate(geom, resolution)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 20.

Tessellate a geometry into quadbin cells. Like quadbin_polyfill but returns the per-cell geometry chip alongside the cell ID, suitable for chip-based join patterns.

Implementation

Powered by the quadbin package + shapely. Bbox polyfill then per-cell intersection with the input geometry; one STRUCT<cell, geom> per chip (EWKB, SRID 4326).

Returns:

  • ARRAY<STRUCT<cell: BIGINT, geom: BINARY>>
Since v0.4.0
SELECT gbx_quadbin_tessellate('POLYGON((-1 -1, 1 -1, 1 1, -1 1, -1 -1))', 5) AS chips;
Example output
+----------------------------------------------+
|chips |
+----------------------------------------------+
|[{5211790668774506495, [binary]}, {5212..., ..|
+----------------------------------------------+
... (4 chips: each quadbin cell paired with its clipped geometry WKB (SRID 4326))

quadbin_cellunion​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_cellunion(cells)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_cellunion(cells)BINARY
Pythongridx.quadbin.functions.quadbin_cellunion(cells)Column
ParameterSQL typeUC typeDefaultDescription
cellsARRAY<BIGINT>ARRAY<BIGINT>The quadbin cell ids to merge.

Union an ARRAY<BIGINT> of quadbin cells into a single MultiPolygon EWKB.

Implementation

Powered by shapely (union_all of the cell polygons). Dissolves an ARRAY<LONG> of cells into one EWKB MultiPolygon (SRID 4326).

Returns:

  • Binary EWKB multipolygon (SRID-tagged 4326)
Since v0.4.0
SELECT gbx_quadbin_cellunion(
gbx_quadbin_kring(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1)
) AS union_geom;
Example output
+----------+
|union_geom|
+----------+
|[binary] |
+----------+
... (EWKB binary — MultiPolygon dissolving the SF z10 kring, SRID 4326)

quadbin_cellunion_agg​

SQLPython
SurfaceCallReturns
SQL (session)gbx_quadbin_cellunion_agg(cellid)BINARY
Pythongridx.quadbin.functions.quadbin_cellunion_agg(cellid)Column
ParameterSQL typeDefaultDescription
cellidBIGINTThe quadbin cell id. A NULL id is skipped; a repeated cell counts once.
Grouped-agg UDF

Aggregate-level union: dissolve a column of quadbin cell IDs (grouped per partition) into a single MultiPolygon EWKB. Use this instead of gbx_quadbin_cellunion when your cell IDs are spread across rows rather than already collected into an array.

Implementation

Powered by shapely. Grouped aggregate — groupBy(...).agg(gx.quadbin_cellunion_agg("cell")) dissolves a group's cell ids into one EWKB MultiPolygon (SRID 4326).

Returns:

  • BINARY EWKB multipolygon (SRID-tagged 4326) representing the dissolved coverage
Since v0.4.0
-- Spark refuses a SQL function directly under explode, so the ring is built in a
-- subquery and the column is exploded.
SELECT region, gbx_quadbin_cellunion_agg(cell) AS coverage
FROM (
SELECT 'R1' AS region, explode(ring) AS cell
FROM (
SELECT gbx_quadbin_kring(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1) AS ring
)
) t
GROUP BY region;
Example output
+------+--------+
|region|coverage|
+------+--------+
|R1 |[binary]|
+------+--------+
... (BINARY EWKB — dissolved coverage of all 9 kring cells around SF z10, SRID 4326)

quadbin_distance​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_distance(cellid1, cellid2)INT
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_distance(cellid1, cellid2)INT
Pythongridx.quadbin.functions.quadbin_distance(cellid1, cellid2)Column
ParameterSQL typeUC typeDefaultDescription
cellid1BIGINTBIGINTThe first quadbin cell id.
cellid2BIGINTBIGINTThe second quadbin cell id, at the same zoom level.

Chebyshev (king-move) distance between two quadbin cells at the same resolution.

Implementation

Powered by the quadbin package. Chebyshev distance on tile coordinates. It raises if the two cells differ in resolution.

Returns:

  • INT cell-step distance
Since v0.4.0
SELECT gbx_quadbin_distance(
gbx_quadbin_pointascell(0.0, 0.0, 10),
gbx_quadbin_pointascell(0.0, 0.1, 10)
) AS d;
Example output
+-+
|d|
+-+
|1|
+-+

quadbin_cellfill​

SQLPython
SurfaceCallReturns
SQL (session)gbx_quadbin_cellfill(cellid, value, [k], [method], [power])BINARY
Pythongridx.quadbin.functions.quadbin_cellfill(cellid, value, k=None, method=None, power=None)Column
ParameterSQL typeDefaultDescription
cellidBIGINTThe quadbin cell id.
valueDOUBLEThe cell's value; NULL marks a covered cell with no value, which is filled.
kINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.
Grouped-agg UDF

See Grid raster tessellation for the coverage / assignment / fill model shared by all grids.

Fill NULL quadbin cells from valid neighbours within k rings using nearest-neighbour interpolation. A grouped aggregator: the call must appear inside a GROUP BY query. Cell IDs are BIGINT. With k=1 and method='mean', each NULL cell receives the unweighted mean of its ring-1 neighbours.

The cell-fill aggregate returns BINARY

gbx_quadbin_cellfill returns BINARY in Python and SQL at every level. The payload is the serialized fill: a 4-byte count, then per cell its id, a present flag and the value (big-endian, ascending id order). A cell id that repeats in a group is filled from its largest non-null value, whatever order the rows arrive in.

Implementation

Powered by the pure-Python quadbin interpolation port.

Returns: BINARY, the serialized fill described above.

Since v0.5.1
-- The London z=10 cell has no value; its 8 ring-1 neighbours (kloop) carry 5.0.
-- Spark refuses a SQL function directly under explode, so the ring is built in a
-- CTE first and the column is exploded.
WITH r AS (
SELECT gbx_quadbin_pointascell(-0.1, 51.5, 10) AS centre,
gbx_quadbin_kloop(gbx_quadbin_pointascell(-0.1, 51.5, 10), 1) AS around
)
SELECT region,
gbx_quadbin_cellfill(cellid, value, 1, 'mean', 2.0) AS filled
FROM (
SELECT 1 AS region, centre AS cellid, CAST(NULL AS DOUBLE) AS value FROM r
UNION ALL
SELECT 1 AS region, explode(around) AS cellid, 5.0 AS value FROM r
) t
GROUP BY region;
Example output
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: 9 cells; the London z10 center cell is filled to 5.0, the 8 ring-1 neighbours stay 5.0)

quadbin_cellfill_array​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_cellfill_array(cells, [k], [method], [power])array<struct<cellid:bigint,value:double>>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_cellfill_array(cells, [k], [method], [power])array<struct<cellid:bigint,value:double>>
Pythongridx.quadbin.functions.quadbin_cellfill_array(cells, k=None, method=None, power=None)Column
ParameterSQL typeUC typeDefaultDescription
cellsarray<struct<cellid:bigint,value:double>>array<struct<cellid:bigint,value:double>>The group's cells as an array of structs (cellid, value), for example collect_list(struct(cellid, value)). A NULL value marks a covered cell to fill; a repeated cell keeps its largest value; a NULL cell id is skipped.
kINTINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRINGSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLEDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.

Fill missing quadbin cell values from one array of (cellid, value) structs. It is the array form of gbx_quadbin_cellfill: the same fill, one call per group. On Unity Catalog the array is limited to 64 MiB; a larger one is refused with a Fix.

Since v0.6.0
SELECT region,
gbx_quadbin_cellfill_array(collect_list(struct(cellid, value)), 1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 5234158540624494591L, CAST(NULL AS DOUBLE)),
(1, 5234158544919461887L, 5.0),
(1, 5234158536329527295L, 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+------------------------------------------------------------------------------------+
|region|filled |
+------+------------------------------------------------------------------------------------+
|1 |[{5234158536329527295, 5.0}, {5234158540624494591, 5.0}, {5234158544919461887, 5.0}]|
+------+------------------------------------------------------------------------------------+
... (array of (cellid, value) structs in ascending id order; the London z10 center cell is filled to 5.0)

Geometry-Aware Expansion (Quadbin)​

Geometry-aware functions polyfill a geometry into the grid and then expand outward (or inward at holes) using the dilation engine. They generalise the cell-centric quadbin_kring / quadbin_kloop to operate on arbitrary geometry shapes — polygons (with holes), lines, and points. The optional mode parameter selects the expansion direction and hole handling; the optional coverage parameter (after mode) selects how a cell is judged to belong to a region. This section is the canonical description; the BNG, custom, and H3 geometry-aware functions share the same mode/coverage contract.

Dilation modes​

The mode argument (default 'boundary-out') takes one of these exact hyphenated values — each produces a k-thick band; polygon holes are handled only by the hole-* modes, never by boundary-*:

ModeBehaviour
'boundary-out'Default. The geometry's boundary band (k=0) plus the outward band — cells within k steps outside the boundary; the fully-interior cells are excluded. k=0 follows the coverage: coveras/core = the whole straddling band, polyfill = the centroid-outside cells. The outward k≥1 rings are identical across coverages. Works on aligned and non-aligned geometries alike.
'boundary-in'Inward setback band from the outer boundary, stopping at holes.
'boundary-in-ignore-holes'Like 'boundary-in' but treats the polygon as solid — the band can cross holes.
'hole-in'Fills each hole inward from its edge.
'hole-out'Band in the solid around each hole, bounded by the outer geometry.
'hole-out-ignore-geom'Band outward from each hole, not bounded by the outer geometry.

Coverage​

The coverage argument (default 'coveras') takes one of these exact values — it selects how a cell is judged to belong to a region:

CoverageA cell belongs to a region if it…
'coveras'Default. …overlaps the region (inclusive; boundary cells kept — no false negatives).
'polyfill'…has its centre inside the region (standard polyfill).
'core'…is entirely inside the region (strictest; no false positives).

Nesting: coveras ⊇ polyfill ⊇ core. Point and line geometries are covered only under 'coveras'. They have no cell whose centre is inside them or that fully contains them, so 'polyfill'/'core' return empty for them.

quadbin_geomkring​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_geomkring(geom, resolution, k, [mode], [coverage])array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_geomkring(geom, resolution, k, [mode], [coverage])array<bigint>
Pythongridx.quadbin.functions.quadbin_geomkring(geom, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 20.
kINTINTThe ring distance: 0 is the covering cells alone, 1 adds the cells around them.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Polyfill a geometry at a given quadbin zoom level then expand by k ring steps. Returns all cells within Chebyshev distance k of the covering set.

Returns:

  • ARRAY<BIGINT> of quadbin cell IDs
Since v0.5.1
SELECT gbx_quadbin_geomkring(
'POLYGON((-73.99 40.71, -73.95 40.71, -73.95 40.75, -73.99 40.75, -73.99 40.71))',
12, 1, 'boundary-out'
) AS kring;
Example output
+----------------------------------------+
|kring |
+----------------------------------------+
|[..., (cells within k=1 ring at res 12)]|
+----------------------------------------+
... (ARRAY<BIGINT> — quadbin covering set plus one outer ring)

quadbin_geomkloop​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_geomkloop(geom, resolution, k, [mode], [coverage])array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_geomkloop(geom, resolution, k, [mode], [coverage])array<bigint>
Pythongridx.quadbin.functions.quadbin_geomkloop(geom, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 20.
kINTINTThe loop distance: only the cells at exactly this ring distance from the covering cells are returned.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Polyfill a geometry at a given quadbin zoom level then return only the hollow outer ring at exactly k steps.

Returns:

  • ARRAY<BIGINT> of quadbin cell IDs at exactly distance k
Since v0.5.1
SELECT gbx_quadbin_geomkloop(
'POLYGON((-73.99 40.71, -73.95 40.71, -73.95 40.75, -73.99 40.75, -73.99 40.71))',
12, 1, 'boundary-out'
) AS kloop;
Example output
+--------------------------------------------+
|kloop |
+--------------------------------------------+
|[..., (outer ring cells, polyfill excluded)]|
+--------------------------------------------+
... (ARRAY<BIGINT> — hollow quadbin outer ring at k=1)

quadbin_geomkringexplode​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_geomkringexplode(geom, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_geomkringexplode(geom, resolution, k, [mode], [coverage])rows
Pythongridx.quadbin.functions.quadbin_geomkringexplode(df, geom, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid BIGINT.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 20.
kINTINTThe ring distance: 0 is the covering cells alone, 1 adds the cells around them.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Streaming version of quadbin_geomkring — one row per cell. Call it with FROM t, LATERAL gbx_...(...), at every implementation level.

Returns:

  • One row per BIGINT cell ID in the k-ring
Since v0.5.1
SELECT t.cellid
FROM (SELECT 'POLYGON((-73.99 40.71, -73.95 40.71, -73.95 40.75, -73.99 40.75, -73.99 40.71))' AS geom) src,
LATERAL gbx_quadbin_geomkringexplode(src.geom, 12, 1, 'boundary-out') t;
Example output
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 ring of the geometry)

quadbin_geomkloopexplode​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_quadbin_geomkloopexplode(geom, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_quadbin_geomkloopexplode(geom, resolution, k, [mode], [coverage])rows
Pythongridx.quadbin.functions.quadbin_geomkloopexplode(df, geom, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid BIGINT.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGQuadbin zoom level, a whole number from 0 to 20.
kINTINTThe loop distance: only the cells at exactly this ring distance from the covering cells are returned.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Streaming version of quadbin_geomkloop — one row per cell at exactly k steps. Call it with FROM t, LATERAL gbx_...(...), at every implementation level.

Returns:

  • One row per BIGINT cell ID at exactly distance k
Since v0.5.1
SELECT t.cellid
FROM (SELECT 'POLYGON((-73.99 40.71, -73.95 40.71, -73.95 40.75, -73.99 40.75, -73.99 40.71))' AS geom) src,
LATERAL gbx_quadbin_geomkloopexplode(src.geom, 12, 1, 'boundary-out') t;
Example output
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 hollow outer ring)

British National Grid (BNG)​

BNG runs on every compute with identical gbx_bng_* SQL names. The Python level is a pure-Python port of BNG.scala plus shapely. Cell IDs are STRING references. Geometry outputs are plain WKB in EPSG:27700 coordinates with no SRID, unlike quadbin's EWKB SRID 4326. Cell-id math has exact parity with the JVM level.

The British National Grid is the national coordinate system for Great Britain, based on the Ordnance Survey National Grid (OSGB36). It divides Great Britain into grid squares identified by letter-based prefixes and numeric coordinates.

BNG Structure​

  • Grid Squares: 100km x 100km squares identified by two letters (e.g., "TQ" for London, "NT" for Edinburgh)
  • Eastings and Northings: Numeric coordinates within each grid square (EPSG:27700)
  • Resolution Indices: Integer indices 1..6 (1=100km, 2=10km, 3=1km, 4=100m, 5=10m, 6=1m); negative indices select quadrant sub-cells. String keys (e.g. "1km", "100m") are also accepted via BNG.resolutionMap.

BNG Grid Reference Format​

BNG references follow the format: [Letters][Eastings][Northings]

Examples:

  • TQ 38 80 — 1km precision (Tower of London area)
  • TQ 3800 8000 — 100m precision
  • TQ 38000 80000 — 10m precision
  • SU 12 34 — Different grid square

Precision Levels​

PrecisionGrid SizeExampleUse Case
100000m100km x 100kmTQRegional analysis
10000m10km x 10kmTQ38District-level
1000m1km x 1kmTQ3080Local area analysis
100m100m x 100mTQ308808Neighborhood level
10m10m x 10mTQ30808080Building level
1m1m x 1mTQ3080080800Precise location

Major Grid Squares​

Major 100km grid squares in Great Britain:

  • TQ — London area
  • SU — South Hampshire
  • NT — Edinburgh area
  • SD — Lake District
  • ST — Bristol area

Conversion Functions​

Functions to convert BNG cell IDs to standard geometry formats.

bng_aswkb​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_aswkb(cellid)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_aswkb(cellid)BINARY
Pythongridx.bng.functions.bng_aswkb(cellid)Column
ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.

Convert a BNG cell ID to Well-Known Binary (WKB) format.

Implementation

Powered by a pure-Python port of BNG.scala + shapely. The cell footprint polygon as plain WKB in EPSG:27700 (no SRID).

Returns:

  • Binary WKB geometry representation
Since v0.2.0
SELECT gbx_bng_aswkb('TQ3080') as wkb_geom;
Example output
+--------+
|wkb_geom|
+--------+
|[binary]|
+--------+
... (WKB binary)

bng_aswkt​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_aswkt(cellid)STRING
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_aswkt(cellid)STRING
Pythongridx.bng.functions.bng_aswkt(cellid)Column
ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.

Convert a BNG cell ID to Well-Known Text (WKT) format.

Implementation

Powered by a pure-Python port of BNG.scala + shapely. The cell footprint as WKT in EPSG:27700 coordinates.

Returns:

  • String WKT geometry representation
Since v0.2.0
SELECT gbx_bng_aswkt('TQ3080') as wkt_geom;
Example output
+----------------------------------------------------------+
|wkt_geom |
+----------------------------------------------------------+
|POLYGON ((530000 180000, 531000 180000, 531000 181000, ...|
+----------------------------------------------------------+
POLYGON ((530000 180000, 531000 180000, 531000 181000, 530000 181000, 530000 180000))

Core Functions​

Fundamental operations on BNG cells.

bng_cellarea​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_cellarea(cellid)DOUBLE
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_cellarea(cellid)DOUBLE
Pythongridx.bng.functions.bng_cellarea(cellid)Column
ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.

Calculate the area of a BNG grid cell.

Implementation

Powered by the pure-Python port of BNG.scala. Returns the cell area in square kilometres, matching the JVM level.

Returns:

  • Double representing the cell area in square kilometres
Since v0.2.0
SELECT gbx_bng_cellarea('TQ3080') AS area_km2;
Example output
+--------+
|area_km2|
+--------+
|1.0 |
+--------+

bng_centroid​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_centroid(cellid)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_centroid(cellid)BINARY
Pythongridx.bng.functions.bng_centroid(cellid)Column
ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.

Get the centroid (center point) of a BNG cell.

Implementation

Powered by the pure-Python port of BNG.scala + shapely. Cell-center POINT as WKB in EPSG:27700 (no SRID).

Returns:

  • Point geometry at cell center
Since v0.2.0
SELECT gbx_bng_centroid('TQ3080') as centroid;
Example output
+--------+
|centroid|
+--------+
|[binary]|
+--------+
... (WKB binary — POINT(530500 180500) in EPSG:27700)

bng_distance​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_distance(cellid1, cellid2)BIGINT
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_distance(cellid1, cellid2)BIGINT
Pythongridx.bng.functions.bng_distance(cellid1, cellid2)Column
ParameterSQL typeUC typeDefaultDescription
cellid1STRINGSTRINGThe first BNG cell id.
cellid2STRINGSTRINGThe second BNG cell id.

Return the grid-step distance between two BNG cells.

Implementation

Powered by the pure-Python port of BNG.scala — Chebyshev grid-step count between two cells, exact-parity with the JVM level. Returns LONG (grid steps, not metres).

Returns:

  • LONG — Chebyshev grid-step distance. Adjacent cells (sharing an edge or corner) return 1; the cell itself returns 0.
Since v0.2.0
SELECT gbx_bng_distance('TQ3080', 'TQ3081') AS dist_steps;
Example output
+----------+
|dist_steps|
+----------+
|1 |
+----------+

bng_euclideandistance​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_euclideandistance(cellid1, cellid2)BIGINT
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_euclideandistance(cellid1, cellid2)BIGINT
Pythongridx.bng.functions.bng_euclideandistance(cellid1, cellid2)Column
ParameterSQL typeUC typeDefaultDescription
cellid1STRINGSTRINGThe first BNG cell id.
cellid2STRINGSTRINGThe second BNG cell id.

Return the Chebyshev grid-unit distance between two BNG cells.

Implementation

Powered by the pure-Python port of BNG.scala — Chebyshev distance in grid units, exact-parity with the JVM level. Returns LONG (grid units, not metres).

Returns:

  • LONG — Chebyshev distance in grid units. Adjacent cells return 1; diagonal cells also return 1.
Since v0.2.0
SELECT gbx_bng_euclideandistance('TQ3080', 'TQ3081') AS euclidean_dist;
Example output
+--------------+
|euclidean_dist|
+--------------+
|1 |
+--------------+

Cell Operations​

Operations combining multiple BNG chip structs.

Chip-struct inputs required

bng_cellintersection and bng_cellunion take STRUCT<cellid STRING, core BOOLEAN, chip BINARY> chip inputs — the same struct produced by bng_tessellate / bng_tessellateexplode. Passing plain STRING cell IDs throws ClassCastException. Use bng_tessellate(geom, resolution) to produce chip inputs.

bng_cellintersection​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_cellintersection(left_chip, right_chip)struct<cellid:string,core:boolean,chip:binary>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_cellintersection(left_chip, right_chip)struct<cellid:string,core:boolean,chip:binary>
Pythongridx.bng.functions.bng_cellintersection(left_chip, right_chip)Column
ParameterSQL typeUC typeDefaultDescription
left_chipstruct<cellid:string,core:boolean,chip:binary>struct<cellid:string,core:boolean,chip:binary>A chip: a struct of cellid, core and chip (WKB), read by position.
right_chipstruct<cellid:string,core:boolean,chip:binary>struct<cellid:string,core:boolean,chip:binary>The second chip, in the same form.

Intersect two BNG chip structs and return the dissolved intersection chip.

Implementation

Powered by the pure-Python port of BNG.scala + shapely. Returns the same STRUCT<cellid, core, chip> as the JVM level (AGREE).

Returns:

  • STRUCT<cellid STRING, core BOOLEAN, chip BINARY> — dissolved intersection chip. A fully interior chip (core=true) intersected with itself returns {cellid, true, null}.
Since v0.2.0
SELECT gbx_bng_cellintersection(chip, chip) AS intersection_chip
FROM (
SELECT explode(
gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))', 3
)
) AS chip
) t
WHERE t.chip.cellid = 'TQ3080';
Example output
+--------------------+
|intersection_chip |
+--------------------+
|{TQ3080, true, null}|
+--------------------+

bng_cellunion​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_cellunion(left_chip, right_chip)struct<cellid:string,core:boolean,chip:binary>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_cellunion(left_chip, right_chip)struct<cellid:string,core:boolean,chip:binary>
Pythongridx.bng.functions.bng_cellunion(left_chip, right_chip)Column
ParameterSQL typeUC typeDefaultDescription
left_chipstruct<cellid:string,core:boolean,chip:binary>struct<cellid:string,core:boolean,chip:binary>A chip: a struct of cellid, core and chip (WKB), read by position.
right_chipstruct<cellid:string,core:boolean,chip:binary>struct<cellid:string,core:boolean,chip:binary>The second chip, in the same form.

Union two BNG chip structs and return the dissolved union chip.

Implementation

Powered by the pure-Python port of BNG.scala + shapely. Returns the same STRUCT<cellid, core, chip> as the JVM level (AGREE).

Returns:

  • STRUCT<cellid STRING, core BOOLEAN, chip BINARY> — dissolved union chip. A fully interior chip (core=true) unioned with itself returns {cellid, true, null}.
Since v0.2.0
SELECT gbx_bng_cellunion(chip, chip) AS union_chip
FROM (
SELECT explode(
gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))', 3
)
) AS chip
) t
WHERE t.chip.cellid = 'TQ3080';
Example output
+--------------------+
|union_chip |
+--------------------+
|{TQ3080, true, null}|
+--------------------+

Coordinate Conversion Functions​

Convert coordinates or geometries to BNG cells.

bng_eastnorthasbng​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_eastnorthasbng(easting, northing, resolution)STRING
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_eastnorthasbng(easting, northing, resolution)STRING
Pythongridx.bng.functions.bng_eastnorthasbng(easting, northing, resolution)Column
ParameterSQL typeUC typeDefaultDescription
eastingDOUBLEDOUBLEEasting in EPSG:27700 metres.
northingDOUBLEDOUBLENorthing in EPSG:27700 metres.
resolutionSTRING or INTSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.

Convert easting/northing coordinates to a BNG cell reference.

Implementation

Runs as a SQL expression that needs no JAR and no Python, and falls back to a numpy-vectorized encoder at the Python level. The env wheel adds a Rust kernel there. Eastings/northings must be EPSG:27700 (e.g. 530000, 180000 for London).

Returns:

  • String BNG cell reference, or NULL when the easting or northing is NaN or infinite (no cell exists for it)
Since v0.2.0
-- Convert OS Grid Reference coordinates (easting, northing); resolution '1km' or integer 3
SELECT gbx_bng_eastnorthasbng(530000, 180000, '1km') as bng_cell;
Example output
+--------+
|bng_cell|
+--------+
|TQ3080 |
+--------+

bng_pointascell​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_pointascell(geom, resolution)STRING
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_pointascell(geom, resolution)STRING
Pythongridx.bng.functions.bng_pointascell(geom, resolution)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.

Convert a point geometry to a BNG grid cell. The point must be supplied as WKT or WKB (GeoBrix does not accept native Databricks geometry types).

Implementation

Powered by the pure-Python port of BNG.scala + shapely. The point coordinates must be EPSG:27700 eastings/northings (e.g. POINT(530000 180000) for London), not WGS84 lon/lat. Exact-parity cell IDs with the JVM level.

Returns:

  • String BNG cell reference, or NULL when the point's coordinates are NaN or infinite (an empty point, for example)
Since v0.2.0
-- Point in BNG coordinates (eastings, northings); resolution '1km' for 1 km cell
SELECT gbx_bng_pointascell('POINT(530000 180000)', '1km') AS bng_cell;
Example output
+--------+
|bng_cell|
+--------+
|TQ3080 |
+--------+

K-Ring Functions​

Generate neighboring cells using k-ring patterns.

bng_kring​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_kring(cellid, k)array<string>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_kring(cellid, k)array<string>
Pythongridx.bng.functions.bng_kring(cellid, k)Column
ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.
kINTINTThe ring distance: 0 is the cell alone, 1 adds its eight neighbours.

Generate a k-ring of cells around a center cell (filled disk).

Implementation

Powered by the pure-Python port of BNG.scala. All cells within ring distance k (inclusive of the center). Exact cell-set parity with the JVM level.

Returns:

  • Array of BNG cell references in the k-ring
Since v0.2.0
SELECT gbx_bng_kring('TQ3080', 1) AS kring;
Example output
+-----------------------------+
|kring |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (9 cells: center TQ3080 plus 8 surrounding cells at k=1)

bng_kloop​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_kloop(cellid, k)array<string>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_kloop(cellid, k)array<string>
Pythongridx.bng.functions.bng_kloop(cellid, k)Column
ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.
kINTINTThe ring distance: 1 is the eight neighbours of the cell.

Generate a k-loop of cells around a center cell (hollow ring).

Implementation

Powered by pure Python. The cells at exactly ring distance k.

Returns:

  • Array of BNG cell references at exactly distance k
Since v0.2.0
SELECT gbx_bng_kloop('TQ3080', 1) AS kloop;
Example output
+-----------------------------+
|kloop |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (8 cells: hollow ring at k=1, center TQ3080 excluded)

bng_geomkring​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_geomkring(geom, resolution, k, [mode], [coverage])array<string>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_geomkring(geom, resolution, k, [mode], [coverage])array<string>
Pythongridx.bng.functions.bng_geomkring(geom, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.
kINTINTThe number of rings of dilation around the geometry's cells.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Generate a k-ring of cells around a geometry at specified resolution.

Implementation

Powered by pure Python + shapely. Polyfills the geometry then expands by ring distance k.

Returns:

  • ARRAY<STRING> of BNG cell references
Since v0.2.0
SELECT gbx_bng_geomkring(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3, 1, 'boundary-out'
) AS kring;
Example output
+-----------------------------+
|kring |
+-----------------------------+
|[TQ2878, TQ2879, TQ2880, ...]|
+-----------------------------+
... (24 cells: boundary band expanded by k=1 ring; interior centre not filled)

bng_geomkloop​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_geomkloop(geom, resolution, k, [mode], [coverage])array<string>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_geomkloop(geom, resolution, k, [mode], [coverage])array<string>
Pythongridx.bng.functions.bng_geomkloop(geom, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.
kINTINTThe number of rings of dilation around the geometry's cells.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Generate a k-loop of cells around a geometry at specified resolution. The loop is the hollow outer ring: cells in kring(k) but not in kring(k-1).

Implementation

Powered by pure Python + shapely. Polyfills the geometry then returns only the cells at exactly ring distance k.

Returns:

  • ARRAY<STRING> of BNG cell references at exactly distance k
Since v0.2.0
SELECT gbx_bng_geomkloop(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3, 1, 'boundary-out'
) AS kloop;
Example output
+-----------------------------+
|kloop |
+-----------------------------+
|[TQ2878, TQ2879, TQ2880, ...]|
+-----------------------------+
... (16 cells: outer ring at k=1 around the BNG polygon polyfill)

Tessellation Functions​

Fill geometries with grid cells.

bng_polyfill​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_polyfill(geom, resolution)array<string>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_polyfill(geom, resolution)array<string>
Pythongridx.bng.functions.bng_polyfill(geom, resolution)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.

Fill a geometry with BNG cells at specified resolution.

Implementation

Powered by pure Python + shapely. Enumerates the cells covering the geometry, by seed-and-flood-fill.

Returns:

  • Array of BNG cell IDs covering the geometry
Since v0.2.0
SELECT gbx_bng_polyfill(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3
) AS cells;
Example output
+-----------------------------+
|cells |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (9 cells covering the 3km × 3km BNG polygon at 1km resolution)

bng_tessellate​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_tessellate(geom, resolution, [keep_core_geom])array<struct<cellid:string,core:boolean,chip:binary>>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_tessellate(geom, resolution, [keep_core_geom])array<struct<cellid:string,core:boolean,chip:binary>>
Pythongridx.bng.functions.bng_tessellate(geom, resolution, keep_core_geom=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.
keep_core_geomBOOLEANBOOLEANfalseWhen true, a core cell's chip is its cell geometry; when false it is NULL. Border cells always carry their clipped geometry.

Tessellate a geometry into BNG cells with their geometries.

Implementation

Powered by the pure-Python port of BNG.scala + shapely. Polyfill plus a per-cell core/border split, returning one STRUCT<cellid, core, chip> per cell (chip geometry as WKB in EPSG:27700, no SRID). Border chips are filtered to the input geometry's dimension, matching the JVM level. A Polygon can yield MultiPolygon chips, and the reverse. A clip that is a mixed collection keeps its polygonal part. The boundary-only points and lines of a grid-aligned input are dropped.

Returns:

  • ARRAY<STRUCT<cellid STRING, core BOOLEAN, chip BINARY>>, one struct per cell (chip as WKB in EPSG:27700)
Since v0.2.0
SELECT gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3
) AS chips;
Example output
+--------------------------------------------+
|chips |
+--------------------------------------------+
|[{TQ2979, false, [binary]}, {TQ3080, true,..|
+--------------------------------------------+
... (9 chips; TQ3080 is core (core=true, chip=null); border cells carry WKB clip geometry)

Aggregator Functions​

Aggregate multiple cells into a single result.

bng_cellintersection_agg​

SQLPython
SurfaceCallReturns
SQL (session)gbx_bng_cellintersection_agg(input_chip)BINARY
Pythongridx.bng.functions.bng_cellintersection_agg(input_chip)Column
ParameterSQL typeDefaultDescription
input_chipstruct<cellid:string,core:boolean,chip:binary>A chip of the cell: its id, whether it is a core chip, and its geometry (NULL for a core chip).
Grouped-agg UDF

Aggregate intersection of multiple BNG cells.

Implementation

Powered by pure Python + shapely. A grouped aggregate — groupBy(cellid).agg(gx.bng_cellintersection_agg("chip")) intersects a group's chips (all belonging to one cell) into the dissolved chip geometry.

The aggregate returns BINARY

gbx_bng_cellintersection_agg returns BINARY: the dissolved chip geometry, as WKB in EPSG:27700. It does this in both Python and SQL. A grouped aggregate cannot return a STRUCT.

The aggregate combines chips from a single cell. The cellid is the group key you GROUP BY. The core flag is recoverable from whether the chip equals the full cell. To rebuild the (cellid, chip) pair in SQL, project the group key as cellid:

-- Rebuild the (cellid, chip) pair
SELECT
group_key AS cellid,
gbx_bng_cellintersection_agg(chip) AS chip
FROM cells
GROUP BY group_key

Returns:

  • BNG cell ID representing common intersection
Since v0.2.0
SELECT t.chip.cellid, gbx_bng_cellintersection_agg(t.chip) AS common_chip
FROM (
SELECT explode(gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))', 3
)) AS chip
) t
WHERE t.chip.cellid = 'TQ3080'
GROUP BY t.chip.cellid;
Example output
# Heavyweight SQL (active tier = heavy/Scala) — STRUCT<cellid, core, chip>:
+------+--------------------+
|cellid|common_chip |
+------+--------------------+
|TQ3080|{TQ3080, true, null}|
+------+--------------------+
... (core cell TQ3080: chip=null means the full cell polygon)

# Lightweight SQL (active tier = pygx) — BINARY (dissolved chip WKB in EPSG:27700):
+------+-----------+
|cellid|common_chip|
+------+-----------+
|TQ3080|[binary] |
+------+-----------+
... (core cell TQ3080: [binary] is the WKB of the full TQ3080 polygon)

bng_cellintersection_array​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_cellintersection_array(chips)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_cellintersection_array(chips)BINARY
Pythongridx.bng.functions.bng_cellintersection_array(chips)Column
ParameterSQL typeUC typeDefaultDescription
chipsarray<struct<cellid:string,core:boolean,chip:binary>>array<struct<cellid:string,core:boolean,chip:binary>>The chips of one cell as an array of structs (cellid, core, chip), for example collect_list(struct(cellid, core, chip)). A core chip has no geometry and stands for the whole cell; a NULL element is skipped.

Intersect the chips of one BNG cell from one array of (cellid, core, chip) structs. It is the array form of gbx_bng_cellintersection_agg, including its rule that a core chip stands for the whole cell. On Unity Catalog it is a SQL function that calls the product st_intersection, so it needs no Python; chips of more than one cell give NULL.

Since v0.6.0
SELECT gbx_bng_cellintersection_array(collect_list(t.chip)) AS common_chip
FROM (
SELECT explode(gbx_bng_tessellate(
'POLYGON((530000 180000, 530000 181000, 530600 181000, 530600 180000, 530000 180000))', 3
)) AS chip
UNION ALL
SELECT explode(gbx_bng_tessellate(
'POLYGON((530400 180000, 530400 181000, 531000 181000, 531000 180000, 530400 180000))', 3
)) AS chip
) t
WHERE t.chip.cellid = 'TQ3080';
Example output
+-----------+
|common_chip|
+-----------+
|[binary] |
+-----------+
... (BINARY — WKB of the strip x 530400-530600 of cell TQ3080, in EPSG:27700)

bng_cellunion_agg​

SQLPython
SurfaceCallReturns
SQL (session)gbx_bng_cellunion_agg(input_chip)BINARY
Pythongridx.bng.functions.bng_cellunion_agg(input_chip)Column
ParameterSQL typeDefaultDescription
input_chipstruct<cellid:string,core:boolean,chip:binary>A chip of the cell: its id, whether it is a core chip, and its geometry (NULL for a core chip).
Grouped-agg UDF

Aggregate union of multiple BNG cells.

Implementation

Powered by pure Python + shapely. A grouped aggregate — groupBy(cellid).agg(gx.bng_cellunion_agg("chip")) dissolves a group's chips (all belonging to one cell) into the unioned chip geometry.

The aggregate returns BINARY

gbx_bng_cellunion_agg returns BINARY: the dissolved chip geometry, as WKB in EPSG:27700. It does this in both Python and SQL. A grouped aggregate cannot return a STRUCT.

The aggregate combines chips from a single cell. The cellid is the group key you GROUP BY. The core flag is recoverable from whether the chip equals the full cell. To rebuild the (cellid, chip) pair in SQL, project the group key as cellid:

-- Rebuild the (cellid, chip) pair
SELECT
group_key AS cellid,
gbx_bng_cellunion_agg(chip) AS chip
FROM cells
GROUP BY group_key

Returns:

  • BNG cell ID representing bounding union
Since v0.2.0
SELECT t.chip.cellid, gbx_bng_cellunion_agg(t.chip) AS union_chip
FROM (
SELECT explode(gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))', 3
)) AS chip
) t
WHERE t.chip.cellid = 'TQ3080'
GROUP BY t.chip.cellid;
Example output
# Heavyweight SQL (active tier = heavy/Scala) — STRUCT<cellid, core, chip>:
+------+--------------------+
|cellid|union_chip |
+------+--------------------+
|TQ3080|{TQ3080, true, null}|
+------+--------------------+
... (core cell TQ3080: chip=null means the full cell polygon)

# Lightweight SQL (active tier = pygx) — BINARY (dissolved chip WKB in EPSG:27700):
+------+----------+
|cellid|union_chip|
+------+----------+
|TQ3080|[binary] |
+------+----------+
... (core cell TQ3080: [binary] is the WKB of the full TQ3080 polygon)

bng_cellunion_array​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_cellunion_array(chips)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_cellunion_array(chips)BINARY
Pythongridx.bng.functions.bng_cellunion_array(chips)Column
ParameterSQL typeUC typeDefaultDescription
chipsarray<struct<cellid:string,core:boolean,chip:binary>>array<struct<cellid:string,core:boolean,chip:binary>>The chips of one cell as an array of structs (cellid, core, chip), for example collect_list(struct(cellid, core, chip)). A core chip has no geometry and stands for the whole cell; a NULL element is skipped.

Union the chips of one BNG cell from one array of (cellid, core, chip) structs, for example collect_list(struct(cellid, core, chip)). It is the array form of gbx_bng_cellunion_agg. A core chip has no geometry and stands for the whole cell, so the answer is then the cell polygon. On Unity Catalog it is a SQL function that calls the product st_union, so it needs no Python; chips of more than one cell give NULL.

Since v0.6.0
SELECT gbx_bng_cellunion_array(collect_list(t.chip)) AS union_chip
FROM (
SELECT explode(gbx_bng_tessellate(
'POLYGON((530000 180000, 530000 181000, 530600 181000, 530600 180000, 530000 180000))', 3
)) AS chip
UNION ALL
SELECT explode(gbx_bng_tessellate(
'POLYGON((530400 180000, 530400 181000, 531000 181000, 531000 180000, 530400 180000))', 3
)) AS chip
) t
WHERE t.chip.cellid = 'TQ3080';
Example output
+----------+
|union_chip|
+----------+
|[binary] |
+----------+
... (BINARY — WKB of the union, the whole cell TQ3080 (x 530000-531000), in EPSG:27700)

bng_cellfill​

SQLPythonScala
SurfaceCallReturns
SQL (session)gbx_bng_cellfill(cellid, value, [k], [method], [power])BINARY
Pythongridx.bng.functions.bng_cellfill(cellid, value, k=None, method=None, power=None)Column
ParameterSQL typeDefaultDescription
cellidSTRINGThe BNG cell id, for example 'TQ3080'.
valueDOUBLEThe cell's value; NULL marks a covered cell with no value, which is filled.
kINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.
Grouped-agg UDF

See Grid raster tessellation for the coverage / assignment / fill model shared by all grids.

Fill NULL BNG cells from valid neighbours within k rings using nearest-neighbour interpolation. A grouped aggregator: the call must appear inside a GROUP BY query. BNG cell IDs are STRING. With k=1 and method='mean', each NULL cell receives the unweighted mean of its ring-1 neighbours.

The cell-fill aggregate returns BINARY

gbx_bng_cellfill returns BINARY in Python and SQL at every level. The payload is the serialized fill: a 4-byte count, then per cell its id, a present flag and the value (big-endian, ascending id order). A cell id that repeats in a group is filled from its largest non-null value, whatever order the rows arrive in.

Implementation

Powered by the pure-Python port of BNG.scala. Cell-set and value parity with the JVM level for mean interpolation.

Returns: BINARY, the serialized fill described above.

Since v0.5.1
SELECT region,
gbx_bng_cellfill(cellid, value, 1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 'TQ300800', CAST(NULL AS DOUBLE)),
(1, 'TQ299800', 5.0),
(1, 'TQ301800', 5.0),
(1, 'TQ300799', 5.0),
(1, 'TQ300801', 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: TQ300800 filled to 5.0; ring-1 neighbours unchanged)

bng_cellfill_array​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_cellfill_array(cells, [k], [method], [power])array<struct<cellid:string,value:double>>
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_cellfill_array(cells, [k], [method], [power])array<struct<cellid:string,value:double>>
Pythongridx.bng.functions.bng_cellfill_array(cells, k=None, method=None, power=None)Column
ParameterSQL typeUC typeDefaultDescription
cellsarray<struct<cellid:string,value:double>>array<struct<cellid:string,value:double>>The group's cells as an array of structs (cellid, value), for example collect_list(struct(cellid, value)). A NULL value marks a covered cell to fill; a repeated cell keeps its largest value; a NULL cell id is skipped.
kINTINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRINGSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLEDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.

Fill missing BNG cell values from one array of (cellid, value) structs with BNG cell ids such as 'TQ3080'. It is the array form of gbx_bng_cellfill: the same fill, one call per group. A string that is not a cell is skipped. On Unity Catalog the array is limited to 64 MiB; a larger one is refused with a Fix.

Since v0.6.0
SELECT region,
gbx_bng_cellfill_array(collect_list(struct(cellid, value)), 1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 'TQ300800', CAST(NULL AS DOUBLE)),
(1, 'TQ299800', 5.0),
(1, 'TQ301800', 5.0),
(1, 'TQ300799', 5.0),
(1, 'TQ300801', 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+-------------------------------------------------------------------------------------+
|region|filled |
+------+-------------------------------------------------------------------------------------+
|1 |[{TQ299800, 5.0}, {TQ300799, 5.0}, {TQ300800, 5.0}, {TQ300801, 5.0}, {TQ301800, 5.0}]|
+------+-------------------------------------------------------------------------------------+
... (array of (cellid, value) structs in ascending id order; the NULL cell TQ300800 is filled to 5.0)

Generator Functions​

Explode array results into individual rows.

bng_kringexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_kringexplode(cellid, k)rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_kringexplode(cellid, k)rows
Pythongridx.bng.functions.bng_kringexplode(df, cellid, k)DataFrame

Rows: cellid STRING.

ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.
kINTINTThe ring distance: 0 is the cell alone, 1 adds its eight neighbours.
Streaming UDTF

Explode k-ring cells into separate rows.

Implementation

Powered by the pure-Python port of BNG.scala as a streaming UDTF — one output row per k-ring cell. Exact cell-set parity with the JVM level.

Returns:

  • Exploded rows, one per cell in k-ring
Since v0.2.0
SELECT t.cellid
FROM (SELECT 'TQ3080' AS cell) src,
LATERAL gbx_bng_kringexplode(src.cell, 1) t;
Example output
+------+
|cellid|
+------+
|TQ3080|
|TQ2979|
|TQ2981|
|TQ3181|
|TQ3179|
|TQ2980|
|TQ3180|
|TQ3081|
|TQ3079|
+------+
... (9 rows: center TQ3080 plus its 8 neighbours at k=1)

bng_kloopexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_kloopexplode(cellid, k)rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_kloopexplode(cellid, k)rows
Pythongridx.bng.functions.bng_kloopexplode(df, cellid, k)DataFrame

Rows: cellid STRING.

ParameterSQL typeUC typeDefaultDescription
cellidSTRINGSTRINGThe BNG cell id, for example 'TQ3080'.
kINTINTThe ring distance: 1 is the eight neighbours of the cell.
Streaming UDTF

Explode k-loop cells into separate rows.

Implementation

Powered by the pure-Python port of BNG.scala as a streaming UDTF — one output row per k-loop cell. Exact cell-set parity with the JVM level.

Returns:

  • Exploded rows, one per cell in k-loop
Since v0.2.0
SELECT t.cellid
FROM (SELECT 'TQ3080' AS cell) src,
LATERAL gbx_bng_kloopexplode(src.cell, 1) t;
Example output
+------+
|cellid|
+------+
|TQ2979|
|TQ2981|
|TQ3181|
|TQ3179|
|TQ2980|
|TQ3180|
|TQ3081|
|TQ3079|
+------+
... (8 rows: hollow ring at k=1, center TQ3080 excluded)

bng_geomkringexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_geomkringexplode(geom, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_geomkringexplode(geom, resolution, k, [mode], [coverage])rows
Pythongridx.bng.functions.bng_geomkringexplode(df, geom, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid STRING.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.
kINTINTThe number of rings of dilation around the geometry's cells.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Explode geometry k-ring cells into separate rows.

Implementation

Powered by the pure-Python port of BNG.scala + shapely as a streaming UDTF — one output row per geometry k-ring cell. Exact cell-set parity with the JVM level.

Returns:

  • Exploded rows, one per cell (STRING BNG cell reference)
Since v0.2.0
SELECT t.cellid
FROM (SELECT 'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))' AS geom) src,
LATERAL gbx_bng_geomkringexplode(src.geom, 3, 1, 'boundary-out') t;
Example output
+-----------+
|cellid |
+-----------+
|...(STRING)|
+-----------+
... (one row per STRING BNG cell ID in the k=1 ring of the geometry)

bng_geomkloopexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_geomkloopexplode(geom, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_geomkloopexplode(geom, resolution, k, [mode], [coverage])rows
Pythongridx.bng.functions.bng_geomkloopexplode(df, geom, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid STRING.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.
kINTINTThe number of rings of dilation around the geometry's cells.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Explode geometry k-loop cells into separate rows.

Implementation

Powered by the pure-Python port of BNG.scala + shapely as a streaming UDTF — one output row per geometry k-loop cell. Exact cell-set parity with the JVM level.

Returns:

  • Exploded rows, one per cell at distance k (STRING BNG cell reference)
Since v0.2.0
SELECT t.cellid
FROM (SELECT 'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))' AS geom) src,
LATERAL gbx_bng_geomkloopexplode(src.geom, 3, 1, 'boundary-out') t;
Example output
+-----------+
|cellid |
+-----------+
|...(STRING)|
+-----------+
... (one row per STRING BNG cell ID in the k=1 hollow outer ring)

bng_tessellateexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_bng_tessellateexplode(geom, resolution, [keep_core_geom])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_bng_tessellateexplode(geom, resolution, [keep_core_geom])rows
Pythongridx.bng.functions.bng_tessellateexplode(df, geom, resolution, keep_core_geom=None)DataFrame

Rows: cellid STRING, core BOOLEAN, chip BINARY.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT.
resolutionINT or STRINGSTRINGBNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'.
keep_core_geomBOOLEANBOOLEANfalseWhen true, a core cell's chip is its cell geometry; when false it is NULL. Border cells always carry their clipped geometry.
Streaming UDTF

Explode tessellated cells into separate rows.

Implementation

It is powered by the pure-Python port of BNG.scala + shapely as a streaming UDTF. It emits one output row per tessellated cell, each carrying the cell ID and chip geometry (WKB in EPSG:27700, no SRID).

Returns:

  • One row per cell with the columns cellid (STRING), core (BOOLEAN) and chip (BINARY, WKB in EPSG:27700), at every level and for WKB or WKT input
Since v0.2.0
SELECT t.cellid, t.core, t.chip
FROM (SELECT 'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))' AS geom) src,
LATERAL gbx_bng_tessellateexplode(src.geom, 3) t;
Example output
+------+-----+--------------------------------------------------------+
|cellid|core |chip |
+------+-----+--------------------------------------------------------+
|TQ3080|true |NULL |
|TQ2979|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 2C A0 ...]|
|TQ3179|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 3C 40 ...]|
|TQ3181|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 3C 40 ...]|
|TQ2981|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 2C A0 ...]|
|TQ3079|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 34 70 ...]|
|TQ2980|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 24 D0 ...]|
|TQ3081|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 34 70 ...]|
|TQ3180|false|[00 00 00 00 03 00 00 00 01 00 00 00 05 41 20 3C 40 ...]|
+------+-----+--------------------------------------------------------+
... (9 rows: TQ3080 is core (core=true, chip=null); border cells carry a WKB clip polygon, cut here)

BNG Reference Format​

Standard Format​

BNG references follow: [Letters][Eastings][Northings]

Common Resolutions​

ResolutionGrid SizeExampleUse Case
100000m100km × 100kmTQRegional
10000m10km × 10kmTQ38District
1000m1km × 1kmTQ3080Local area
100m100m × 100mTQ308808Neighborhood
10m10m × 10mTQ30808080Building
1m1m × 1mTQ3080080800Precise location

Major Grid Squares​

Major 100km grid squares in Great Britain:

  • TQ - London area
  • SU - South Hampshire
  • NT - Edinburgh area
  • SD - Lake District
  • ST - Bristol area

Custom Grid Functions​

Custom-grid functions run on every compute with identical gbx_custom_* SQL names. The Python level is a pure-Python port of the custom-grid system plus shapely. It needs no external grid library. Cell IDs are BIGINT values. Geometry outputs are plain WKB with no SRID, and the grid's srid is metadata only. Geometry inputs accept WKB, EWKB, WKT, or EWKT. Cell sets have exact parity with the JVM level. gbx_custom_grid returns the same descriptor struct at every level.

A custom grid is a user-defined regular rectangular grid specified by its spatial extent, root cell size, and a recursive split factor. Cell IDs are BIGINT values; hierarchy is controlled by cell_splits (each level subdivides root cells into cell_splits x cell_splits sub-cells). Use custom grids when neither BNG nor quadbin matches your coordinate reference system or cell-size requirements. An example is a national grid in EPSG:27700 with non-standard tile sizes.

custom_grid​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_grid(bound_x_min, bound_x_max, bound_y_min, bound_y_max, cell_splits, root_cell_size_x, root_cell_size_y, [srid])struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_grid(bound_x_min, bound_x_max, bound_y_min, bound_y_max, cell_splits, root_cell_size_x, root_cell_size_y, [srid])struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>
Pythongridx.custom.functions.custom_grid(bound_x_min, bound_x_max, bound_y_min, bound_y_max, cell_splits, root_cell_size_x, root_cell_size_y, srid=None)Column
ParameterSQL typeUC typeDefaultDescription
bound_x_minBIGINTBIGINTThe west edge of the grid.
bound_x_maxBIGINTBIGINTThe east edge of the grid; it must be greater than the west edge.
bound_y_minBIGINTBIGINTThe south edge of the grid.
bound_y_maxBIGINTBIGINTThe north edge of the grid; it must be greater than the south edge.
cell_splitsINTINTHow many parts each cell splits into along each axis at the next resolution; at least 2.
root_cell_size_xINTINTThe width of a resolution-0 cell; greater than 0.
root_cell_size_yINTINTThe height of a resolution-0 cell; greater than 0.
sridINTINT-1The coordinate system of the grid, as an EPSG code; -1 for none.

Define a user-specified regular grid from an origin, extent, cell size, split factor, and SRID.

Returns:

  • STRUCT<bound_x_min, bound_x_max, bound_y_min, bound_y_max, cell_splits, root_cell_size_x, root_cell_size_y, srid> — a grid descriptor struct passed to all other gbx_custom_* functions.
Since v0.4.0
SELECT gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700) AS grid;
Example output
+----------------------------------------------+
|grid |
+----------------------------------------------+
|{0, 1000000, 0, 1000000, 2, 1000, 1000, 27700}|
+----------------------------------------------+

custom_pointascell​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_pointascell(point, grid, resolution)BIGINT
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_pointascell(point, grid, resolution)BIGINT
Pythongridx.custom.functions.custom_pointascell(point, grid, resolution)Column
ParameterSQL typeUC typeDefaultDescription
pointBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
resolutionINT or STRINGSTRINGThe grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits.

Index a point geometry into a custom grid cell ID at the specified resolution level.

Returns:

  • BIGINT cell ID encoding the grid position at the given resolution
Since v0.4.0
SELECT gbx_custom_pointascell('POINT(530000 180000)', gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 5) AS cell;
Example output
+------------------+
|cell |
+------------------+
|360287970373976640|
+------------------+

custom_cellaswkb​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_cellaswkb(cell, grid)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_cellaswkb(cell, grid)BINARY
Pythongridx.custom.functions.custom_cellaswkb(cell, grid)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe custom-grid cell id.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.

Return the WKB footprint polygon of a custom grid cell.

Returns:

  • BINARY WKB polygon representing the cell boundary (geometry ... (WKB binary))
Since v0.4.0
SELECT gbx_custom_cellaswkb(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700)) AS geom;
Example output
+--------+
|geom |
+--------+
|[binary]|
+--------+
... (WKB binary — 31.25m × 31.25m custom grid cell footprint polygon)

custom_cellaswkt​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_cellaswkt(cell, grid)STRING
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_cellaswkt(cell, grid)STRING
Pythongridx.custom.functions.custom_cellaswkt(cell, grid)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe custom-grid cell id.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.

Return the WKT footprint polygon of a custom grid cell.

Returns:

  • STRING WKT polygon representing the cell boundary
Since v0.4.0
SELECT gbx_custom_cellaswkt(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700)) AS wkt;
Example output
+-------------------------------------------------------------------------------------------------+
|wkt |
+-------------------------------------------------------------------------------------------------+
|POLYGON ((530000 180000, 530031.25 180000, 530031.25 180031.25, 530000 180031.25, 530000 180000))|
+-------------------------------------------------------------------------------------------------+

custom_centroid​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_centroid(cell, grid)BINARY
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_centroid(cell, grid)BINARY
Pythongridx.custom.functions.custom_centroid(cell, grid)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe custom-grid cell id.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.

Return the centroid of a custom grid cell as a WKB point.

Returns:

  • BINARY WKB point at the cell center (geometry ... (WKB binary))
Since v0.4.0
SELECT gbx_custom_centroid(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700)) AS centroid;
Example output
+--------+
|centroid|
+--------+
|[binary]|
+--------+
... (WKB binary — POINT at the center of the 31.25m × 31.25m custom grid cell)

custom_polyfill​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_polyfill(geom, grid, resolution)array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_polyfill(geom, grid, resolution)array<bigint>
Pythongridx.custom.functions.custom_polyfill(geom, grid, resolution)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
resolutionINT or STRINGSTRINGThe grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits.

Fill a geometry with all custom grid cell IDs at the specified resolution.

Returns:

  • ARRAY<BIGINT> of cell IDs whose footprints intersect the geometry
Since v0.4.0
SELECT gbx_custom_polyfill('POLYGON((529000 179000,529000 182000,532000 182000,532000 179000,529000 179000))', gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 1) AS cells;
Example output
+---------------------------------------------+
|cells |
+---------------------------------------------+
|[72057594038644994, ..., (36 cells at res=1)]|
+---------------------------------------------+
... (36 BIGINT cell IDs — 500m cells covering the 3km × 3km BNG polygon at resolution 1)

custom_kring​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_kring(cell, grid, k)array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_kring(cell, grid, k)array<bigint>
Pythongridx.custom.functions.custom_kring(cell, grid, k)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe custom-grid cell id.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
kINTINTThe ring distance: 0 is the cell alone, 1 adds its eight neighbours.

Return all custom grid cells within k steps of a center cell (filled neighborhood, Chebyshev distance).

Returns:

  • ARRAY<BIGINT> of cell IDs within distance k (up to (2k+1)^2 cells)
Since v0.4.0
SELECT gbx_custom_kring(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 1) AS ring;
Example output
+-------------------------------------------+
|ring |
+-------------------------------------------+
|[360287970373976640, ..., (9 cells at k=1)]|
+-------------------------------------------+
... (9 BIGINT cell IDs — the 3×3 neighbourhood including center cell at resolution 5)

custom_kloop​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_kloop(cell, grid, k)array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_kloop(cell, grid, k)array<bigint>
Pythongridx.custom.functions.custom_kloop(cell, grid, k)Column
ParameterSQL typeUC typeDefaultDescription
cellBIGINTBIGINTThe custom-grid cell id.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
kINTINTThe loop distance: only the cells at exactly this distance are returned.

Return the hollow ring of custom grid cells at exactly k steps from the center cell. The ring at distance k is the set of cells in kRing(k) but not in kRing(k-1), clamped to the grid boundary. At k=0 the result contains only the center cell.

Returns:

  • ARRAY<BIGINT> of cell IDs at exactly distance k
Since v0.5.1
SELECT gbx_custom_kloop(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 1) AS kloop;
Example output
+-----------------------+
|kloop |
+-----------------------+
|[..., (8 cells at k=1)]|
+-----------------------+
... (8 BIGINT cell IDs — hollow ring at k=1, center cell excluded)

Geometry-Aware Expansion (Custom Grid)​

Geometry-aware functions polyfill a geometry into the custom grid and then expand using the shared dilation engine. The mode parameter behaves the same as for bng_geomkring / quadbin_geomkring (see the Quadbin section for the full mode table). The grid descriptor is supplied as an additional grid argument (unlike the BNG and quadbin variants where the grid identity is implicit in the system).

custom_geomkring​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_geomkring(geom, grid, resolution, k, [mode], [coverage])array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_geomkring(geom, grid, resolution, k, [mode], [coverage])array<bigint>
Pythongridx.custom.functions.custom_geomkring(geom, grid, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
resolutionINT or STRINGSTRINGThe grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits.
kINTINTThe ring distance: 0 is the covering cells alone, 1 adds the cells around them.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Polyfill a geometry at a given custom grid resolution then expand by k ring steps.

Returns:

  • ARRAY<BIGINT> of custom grid cell IDs
Since v0.5.1
SELECT gbx_custom_geomkring(
'POLYGON((530200 180200, 534800 180200, 534800 184800, 530200 184800, 530200 180200))',
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700),
0, 1, 'boundary-out'
) AS kring;
Example output
+--------------------------------------------+
|kring |
+--------------------------------------------+
|[..., (cells within k=1 ring of 4.6 km box)]|
+--------------------------------------------+
... (ARRAY<BIGINT> — polyfill covering set plus one outer ring at resolution 0)

custom_geomkloop​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_geomkloop(geom, grid, resolution, k, [mode], [coverage])array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_geomkloop(geom, grid, resolution, k, [mode], [coverage])array<bigint>
Pythongridx.custom.functions.custom_geomkloop(geom, grid, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
resolutionINT or STRINGSTRINGThe grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits.
kINTINTThe loop distance: only the cells at exactly this ring distance from the covering cells are returned.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Polyfill a geometry at a given custom grid resolution then return only the hollow outer ring at exactly k steps.

Returns:

  • ARRAY<BIGINT> of custom grid cell IDs at exactly distance k
Since v0.5.1
SELECT gbx_custom_geomkloop(
'POLYGON((530200 180200, 534800 180200, 534800 184800, 530200 184800, 530200 180200))',
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700),
0, 1, 'boundary-out'
) AS kloop;
Example output
+--------------------------------------------+
|kloop |
+--------------------------------------------+
|[..., (outer ring cells, polyfill excluded)]|
+--------------------------------------------+
... (ARRAY<BIGINT> — hollow outer ring at k=1, polyfill cells excluded)

custom_geomkringexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_geomkringexplode(geom, grid, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_geomkringexplode(geom, grid, resolution, k, [mode], [coverage])rows
Pythongridx.custom.functions.custom_geomkringexplode(df, geom, grid, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid BIGINT.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
resolutionINT or STRINGSTRINGThe grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits.
kINTINTThe ring distance: 0 is the covering cells alone, 1 adds the cells around them.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Streaming version of custom_geomkring — one row per cell. Call it with FROM t, LATERAL gbx_...(...), at every implementation level.

Returns:

  • One row per BIGINT cell ID in the k-ring
Since v0.5.1
-- heavyweight tier
SELECT t.cellid
FROM (SELECT 'POLYGON((530200 180200, 534800 180200, 534800 184800, 530200 184800, 530200 180200))' AS geom,
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700) AS grid) src
LATERAL VIEW gbx_custom_geomkringexplode(src.geom, src.grid, 0, 1, 'boundary-out') t AS cellid;
Example output
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 ring of the geometry)

custom_geomkloopexplode​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_geomkloopexplode(geom, grid, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_geomkloopexplode(geom, grid, resolution, k, [mode], [coverage])rows
Pythongridx.custom.functions.custom_geomkloopexplode(df, geom, grid, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid BIGINT.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
resolutionINT or STRINGSTRINGThe grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits.
kINTINTThe loop distance: only the cells at exactly this ring distance from the covering cells are returned.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Streaming version of custom_geomkloop — one row per cell at exactly k steps. Call it with FROM t, LATERAL gbx_...(...), at every implementation level.

Returns:

  • One row per BIGINT cell ID at exactly distance k
Since v0.5.1
-- heavyweight tier
SELECT t.cellid
FROM (SELECT 'POLYGON((530200 180200, 534800 180200, 534800 184800, 530200 184800, 530200 180200))' AS geom,
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700) AS grid) src
LATERAL VIEW gbx_custom_geomkloopexplode(src.geom, src.grid, 0, 1, 'boundary-out') t AS cellid;
Example output
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 hollow outer ring)

custom_distance​

SQLUnity Catalog SQLPythonScala
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_distance(cellid1, grid, cellid2)BIGINT
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_distance(cellid1, grid, cellid2)BIGINT
Pythongridx.custom.functions.custom_distance(cellid1, grid, cellid2)Column
ParameterSQL typeUC typeDefaultDescription
cellid1BIGINTBIGINTThe first custom-grid cell id.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, as built by gbx_custom_grid.
cellid2BIGINTBIGINTThe second custom-grid cell id, at the same resolution.

Chebyshev (king-move) distance in grid steps between two custom grid cells. Returns the minimum k such that the second cell appears in kRing(firstCell, k), i.e. max(|dx|, |dy|) in cell-position units. Both cells must be at the same resolution; the grid spec enforces consistent encoding.

Returns:

  • BIGINT Chebyshev grid distance in cell steps
Since v0.5.1
SELECT gbx_custom_distance(
gbx_custom_pointascell('POINT(530000 180000)', gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 0),
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700),
gbx_custom_pointascell('POINT(531000 180000)', gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 0)
) AS dist;
Example output
+----+
|dist|
+----+
|1 |
+----+
... (Chebyshev grid distance between two cells 1 step apart in X at resolution 0)

custom_cellfill​

SQLPythonScala
SurfaceCallReturns
SQL (session)gbx_custom_cellfill(cellid, value, grid, [k], [method], [power])BINARY
Pythongridx.custom.functions.custom_cellfill(cellid, value, grid, k=None, method=None, power=None)Column
ParameterSQL typeDefaultDescription
cellidBIGINTThe custom-grid cell id.
valueDOUBLEThe cell's value; NULL marks a covered cell with no value, which is filled.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, from gbx_custom_grid. The same for every row of the group.
kINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.
Grouped-agg UDF

See Grid raster tessellation for the coverage / assignment / fill model shared by all grids.

Fill NULL custom grid cells from valid neighbours within k rings using nearest-neighbour interpolation. A grouped aggregator: the call must appear inside a GROUP BY query. Cell IDs are BIGINT. The grid spec is passed as the third argument (unlike the BNG/quadbin/h3 variants where the grid identity is implicit). With k=1 and method='mean', each NULL cell receives the unweighted mean of its ring-1 neighbours.

The cell-fill aggregate returns BINARY

gbx_custom_cellfill returns BINARY in Python and SQL at every level. The payload is the serialized fill: a 4-byte count, then per cell its id, a present flag and the value (big-endian, ascending id order). A cell id that repeats in a group is filled from its largest non-null value, whatever order the rows arrive in.

Returns: BINARY, the serialized fill described above.

Since v0.5.1
SELECT region,
gbx_custom_cellfill(cellid, value,
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 100000, 100000, 27700),
1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 216172782113787048L, CAST(NULL AS DOUBLE)),
(1, 216172782113786967L, 5.0),
(1, 216172782113787127L, 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: center cell 216172782113787048 filled to 5.0; neighbours unchanged)

custom_cellfill_array​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_custom_cellfill_array(cells, grid, [k], [method], [power])array<struct<cellid:bigint,value:double>>
SQL (Unity Catalog)<catalog>.<schema>.gbx_custom_cellfill_array(cells, grid, [k], [method], [power])array<struct<cellid:bigint,value:double>>
Pythongridx.custom.functions.custom_cellfill_array(cells, grid, k=None, method=None, power=None)Column
ParameterSQL typeUC typeDefaultDescription
cellsarray<struct<cellid:bigint,value:double>>array<struct<cellid:bigint,value:double>>The group's cells as an array of structs (cellid, value), for example collect_list(struct(cellid, value)). A NULL value marks a covered cell to fill; a repeated cell keeps its largest value; a NULL cell id is skipped.
gridstruct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>struct<bound_x_min:bigint,bound_x_max:bigint,bound_y_min:bigint,bound_y_max:bigint,cell_splits:int,root_cell_size_x:int,root_cell_size_y:int,srid:int>The grid, from gbx_custom_grid.
kINTINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRINGSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLEDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.

Fill missing custom-grid cell values from one array of (cellid, value) structs and the grid from gbx_custom_grid. It is the array form of gbx_custom_cellfill: the same fill, one call per group. On Unity Catalog the array is limited to 64 MiB; a larger one is refused with a Fix.

Since v0.6.0
SELECT region,
gbx_custom_cellfill_array(
collect_list(struct(cellid, value)),
gbx_custom_grid(0, 1000000, 0, 1000000, 2, 100000, 100000, 27700),
1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 216172782113787048L, CAST(NULL AS DOUBLE)),
(1, 216172782113786967L, 5.0),
(1, 216172782113787127L, 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+---------------------------------------------------------------------------------+
|region|filled |
+------+---------------------------------------------------------------------------------+
|1 |[{216172782113786967, 5.0}, {216172782113787048, 5.0}, {216172782113787127, 5.0}]|
+------+---------------------------------------------------------------------------------+
... (array of (cellid, value) structs in ascending id order; the NULL center cell is filled to 5.0)

H3 Cell Fill​

gbx_h3_cellfill is the grouped aggregator that interpolates missing H3 cell values from valid ring-neighbours, using the same fill algorithm as the BNG, quadbin, and custom-grid variants.

Return-type divergence

gbx_h3_cellfill returns BINARY in Python and SQL on every compute. The payload is the serialized fill: a 4-byte count, then per cell its id, a present flag and the value (big-endian, ascending id order). A cell id that repeats in a group is filled from its largest non-null value, whatever order the rows arrive in.

h3_cellfill​

SQLPythonScala
SurfaceCallReturns
SQL (session)gbx_h3_cellfill(cellid, value, [k], [method], [power])BINARY
Pythongridx.h3.functions.h3_cellfill(cellid, value, k=None, method=None, power=None)Column
ParameterSQL typeDefaultDescription
cellidBIGINTThe H3 cell id.
valueDOUBLEThe cell's value; NULL marks a covered cell with no value, which is filled.
kINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.

Fill missing H3 cell values by interpolating from ring-k neighbours. Designed to work with data produced by h3_pointash3 (Databricks product built-in) or any other source of H3 cell IDs at a fixed resolution.

Returns: BINARY, the serialized fill: a 4-byte count, then per cell its id, a present flag and the value (big-endian, ascending id order).

Since v0.5.1
SELECT region,
gbx_h3_cellfill(cellid, value, 1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 612934495919669247L, CAST(NULL AS DOUBLE)),
(1, 612934495863046143L, 5.0),
(1, 612934495900794879L, 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: center H3 cell 612934495919669247 filled to 5.0; neighbours unchanged)

h3_cellfill_array​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_h3_cellfill_array(cells, [k], [method], [power])array<struct<cellid:bigint,value:double>>
SQL (Unity Catalog)<catalog>.<schema>.gbx_h3_cellfill_array(cells, [k], [method], [power])array<struct<cellid:bigint,value:double>>
Pythongridx.h3.functions.h3_cellfill_array(cells, k=None, method=None, power=None)Column
ParameterSQL typeUC typeDefaultDescription
cellsarray<struct<cellid:bigint,value:double>>array<struct<cellid:bigint,value:double>>The group's cells as an array of structs (cellid, value), for example collect_list(struct(cellid, value)). A NULL value marks a covered cell to fill; a repeated cell keeps its largest value; a NULL cell id is skipped.
kINTINT1The neighbourhood radius in rings, at least 0; 0 fills nothing.
methodSTRINGSTRING'mean''mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises.
powerDOUBLEDOUBLE2.0The distance exponent of 'idw'; unused by 'mean'.

Fill missing H3 cell values from one array of (cellid, value) structs, for example collect_list(struct(cellid, value)). It is the array form of gbx_h3_cellfill: the same fill, the same rules, one call per group. On Unity Catalog it is a SQL function that calls the product h3_kringdistances, so it needs no Python. A repeated cell id keeps its largest value, and a cell id that is NULL is skipped.

Since v0.6.0
SELECT region,
gbx_h3_cellfill_array(collect_list(struct(cellid, value)), 1, 'mean', 2.0) AS filled
FROM (
VALUES
(1, 612934495919669247L, CAST(NULL AS DOUBLE)),
(1, 612934495863046143L, 5.0),
(1, 612934495900794879L, 5.0)
) AS t(region, cellid, value)
GROUP BY region;
Example output
+------+---------------------------------------------------------------------------------+
|region|filled |
+------+---------------------------------------------------------------------------------+
|1 |[{612934495863046143, 5.0}, {612934495900794879, 5.0}, {612934495919669247, 5.0}]|
+------+---------------------------------------------------------------------------------+
... (array of (cellid, value) structs in ascending id order; the NULL center cell is filled to 5.0)

H3 Geometry-Aware Expansion (Python Level Only)​

H3 geometry-aware kring/kloop run on the Python level only. They work like the other grids: one geometry-taking function does the whole job in pure Python with the h3 library. It uses the polygon_to_cells_experimental polyfill for the covering set and grid_disk for the neighbour walk. They run on every compute, and SQL and PySpark call them the same way:

from databricks.labs.gbx import register
from databricks.labs.gbx.gridx.h3 import functions as hx

register(spark)
df.withColumn("kring", hx.h3_geomkring("geom_col", 9, 1))

Dilation modes work the same as for BNG/quadbin/custom, and all six operate on holed geometries. The modes 'hole-in', 'hole-out', and 'hole-out-ignore-geom' expand from the geometry's holes. The mode 'boundary-in-ignore-holes' treats the polygon as solid, expanding inward across holes as if they were filled.

h3_geomkring​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_h3_geomkring(geom, resolution, k, [mode], [coverage])array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_h3_geomkring(geom, resolution, k, [mode], [coverage])array<bigint>
Pythongridx.h3.functions.h3_geomkring(geom, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGH3 resolution, a whole number from 0 to 15.
kINTINTThe ring distance: 0 is the covering cells alone, 1 adds the cells around them.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Geometry-aware H3 k-ring: the geometry's covering set expanded outward by k ring steps — the polyfill and neighbour walk both use the h3 library.

Returns:

  • ARRAY<BIGINT> of H3 cell IDs
Since v0.5.1
SELECT gbx_h3_geomkring(
'POLYGON((-73.99 40.71, -73.99 40.75, -73.95 40.75, -73.95 40.71, -73.99 40.71))',
9, 1, 'boundary-out'
) AS kring;
Example output
+------------------------------------+
|kring |
+------------------------------------+
|[617733151020810239, ..., (n cells)]|
+------------------------------------+
... (ARRAY<BIGINT> — H3 res-9 covering cells of NYC box expanded by k=1 ring)

h3_geomkloop​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_h3_geomkloop(geom, resolution, k, [mode], [coverage])array<bigint>
SQL (Unity Catalog)<catalog>.<schema>.gbx_h3_geomkloop(geom, resolution, k, [mode], [coverage])array<bigint>
Pythongridx.h3.functions.h3_geomkloop(geom, resolution, k, mode=None, coverage=None)Column
ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGH3 resolution, a whole number from 0 to 15.
kINTINTThe loop distance: only the cells at exactly this ring distance from the covering cells are returned.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).

Geometry-aware H3 k-loop (hollow ring at exactly k steps).

Returns:

  • ARRAY<BIGINT> of H3 cell IDs at exactly distance k
Since v0.5.1
SELECT gbx_h3_geomkloop(
'POLYGON((-73.99 40.71, -73.99 40.75, -73.95 40.75, -73.95 40.71, -73.99 40.71))',
9, 1, 'boundary-out'
) AS kloop;
Example output
+------------------------------------+
|kloop |
+------------------------------------+
|[617733151020810239, ..., (n cells)]|
+------------------------------------+
... (ARRAY<BIGINT> — outer hollow ring at k=1, interior covering cells excluded)

h3_geomkringexplode​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_h3_geomkringexplode(geom, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_h3_geomkringexplode(geom, resolution, k, [mode], [coverage])rows
Pythongridx.h3.functions.h3_geomkringexplode(df, geom, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid BIGINT.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGH3 resolution, a whole number from 0 to 15.
kINTINTThe ring distance: 0 is the covering cells alone, 1 adds the cells around them.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Streaming version of h3_geomkring — one row per cell. Call it with FROM t, LATERAL gbx_h3_geomkringexplode(...).

Returns:

  • One row per BIGINT H3 cell ID
Since v0.5.1
-- Python level only (H3 geometry-aware functions have no JVM implementation)
SELECT t.*
FROM (SELECT 'POLYGON((-73.99 40.71, -73.99 40.75, -73.95 40.75, -73.95 40.71, -73.99 40.71))' AS geom) src,
LATERAL gbx_h3_geomkringexplode(src.geom, 9, 1, 'boundary-out') t;
Example output
+------------------+
|cellid |
+------------------+
|617733151020810239|
|... |
+------------------+
... (one row per BIGINT H3 cell ID in the k=1 ring around the NYC polygon at res 9)

h3_geomkloopexplode​

SQLUnity Catalog SQLPython
Also available as a Unity Catalog function for SQL warehouses. See Unity Catalog functions.
SurfaceCallReturns
SQL (session)gbx_h3_geomkloopexplode(geom, resolution, k, [mode], [coverage])rows
SQL (Unity Catalog)<catalog>.<schema>.gbx_h3_geomkloopexplode(geom, resolution, k, [mode], [coverage])rows
Pythongridx.h3.functions.h3_geomkloopexplode(df, geom, resolution, k, mode=None, coverage=None)DataFrame

Rows: cellid BIGINT.

ParameterSQL typeUC typeDefaultDescription
geomBINARY or STRINGGEOMETRYThe geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees.
resolutionINT or STRINGSTRINGH3 resolution, a whole number from 0 to 15.
kINTINTThe loop distance: only the cells at exactly this ring distance from the covering cells are returned.
modeSTRINGSTRING'boundary-out'The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'.
coverageSTRINGSTRING'coveras'How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside).
Streaming UDTF

Streaming version of h3_geomkloop — one row per cell at exactly k steps. Call it with FROM t, LATERAL gbx_h3_geomkloopexplode(...).

Returns:

  • One row per BIGINT H3 cell ID at exactly distance k
Since v0.5.1
-- Python level only (H3 geometry-aware functions have no JVM implementation)
SELECT t.*
FROM (SELECT 'POLYGON((-73.99 40.71, -73.99 40.75, -73.95 40.75, -73.95 40.71, -73.99 40.71))' AS geom) src,
LATERAL gbx_h3_geomkloopexplode(src.geom, 9, 1, 'boundary-out') t;
Example output
+------------------+
|cellid |
+------------------+
|617733151020810239|
|... |
+------------------+
... (one row per BIGINT H3 cell ID in the k=1 hollow outer ring around the NYC polygon)

H3 Viewshed (Python Helper)​

Python helper only

h3_viewshed_towers is not registered as a SQL function. It is a PySpark DataFrame function in databricks.labs.gbx.gridx.helpers. It runs on Serverless and needs no JAR.

h3_viewshed_towers​

h3_viewshed_towers runs a per-tower line-of-sight viewshed entirely in H3 cell space and returns one row per (tower, visible cell). It is the H3-native sibling of the raster rst_viewshed_towers. For each tower it tests the in-buffer H3 cells with the exact h3_los_visible line of sight. The blocker is the surface map (DSM, or DSM + canopy). The observer sits at the tower-top absolute elevation. Each target receiver is the ground (DTM) elevation plus target_height. The two surface/ground maps are collected once to the driver (bounded at viewshed_res). The per-tower work is fanned one-tower-per-task with mapInPandas over the towers frame repartitioned by tower_id_col. The same code path therefore runs on Databricks Serverless and on classic clusters. This is the mechanism that lets the Wireless Coverage tower siting scale across Spark. H3 cell ids flow as BIGINT (the integer H3 representation) in every column, matching the Databricks product H3 convention.

Signature:

from databricks.labs.gbx.gridx.helpers import h3_viewshed_towers

result_df = h3_viewshed_towers(
towers_df, surface_df, ground_df,
radius_m=2500, viewshed_res=12, target_height=1.6,
tower_id_col="tower_cellid", observer_z_col="observer_z",
cell_col="cellid", surface_z_col="z", ground_z_col="z",
num_partitions=None,
)

Parameters:

  • towers_df — DataFrame of towers; must carry tower_id_col (BIGINT H3 cell) and observer_z_col (observer-top absolute elevation in metres, e.g. DTM at the tower plus the mast height).
  • surface_df — blocker-surface (DSM, or DSM + canopy) map at viewshed_res; must carry cell_col (BIGINT H3 cell) and surface_z_col (metres). A missing or NULL surface is transparent (no blocker at that cell).
  • ground_df — bare-earth (DTM) map at viewshed_res; must carry cell_col and ground_z_col (metres). A target whose ground is NULL or missing is skipped, not fabricated.
  • radius_m — analysis radius in metres (> 0); sizes the per-tower H3 grid_disk buffer. The line of sight is still bounded by the surface/ground maps.
  • viewshed_res — H3 resolution of the surface/ground maps and the output cells.
  • target_height — receiver height above the ground at each tested cell, in metres; defaults to 1.6.
  • tower_id_col / observer_z_col — tower identity and observer-elevation column names; defaults "tower_cellid" / "observer_z".
  • cell_col — cell-id column shared by surface_df / ground_df; defaults "cellid".
  • surface_z_col / ground_z_col — elevation column names of the two maps; both default to "z".
  • num_partitions — fan-out for the per-tower stage (repartition by tower_id_col); defaults to the distinct tower count (~one tower per task).

Returns: DataFrame with one row per (tower, visible cell): tower_id_col (BIGINT) and cell_col (BIGINT H3 cell visible from the tower top).


Performance Tips​

1. Use Appropriate Resolution​

Choose resolution based on analysis needs:

  • Coarse (10km) for regional analysis
  • Medium (1km) for local patterns
  • Fine (100m) for detailed studies

2. Leverage Aggregators​

Use aggregator functions for efficient grouping:

SELECT t.chip.cellid, gbx_bng_cellunion_agg(t.chip) AS union_chip
FROM (
SELECT explode(gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))', 3
)) AS chip
) t
WHERE t.chip.cellid = 'TQ3080'
GROUP BY t.chip.cellid;
Example output
# Heavyweight SQL (active tier = heavy/Scala) — STRUCT<cellid, core, chip>:
+------+--------------------+
|cellid|union_chip |
+------+--------------------+
|TQ3080|{TQ3080, true, null}|
+------+--------------------+
... (core cell TQ3080: chip=null means the full cell polygon)

# Lightweight SQL (active tier = pygx) — BINARY (dissolved chip WKB in EPSG:27700):
+------+----------+
|cellid|union_chip|
+------+----------+
|TQ3080|[binary] |
+------+----------+
... (core cell TQ3080: [binary] is the WKB of the full TQ3080 polygon)

3. Use Generators for Expansion​

Generator functions (e.g. bng_kringexplode, bng_kloopexplode) are more efficient than using explode on array results. See the generator examples above.


Next Steps​