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).
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.
- 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)
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 / DataFrame | Schema | Backed by | Backs |
|---|---|---|---|
bng_cells | cellid STRING | Inline 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_pairs | cellid1 STRING, cellid2 STRING | Inline literals 'TQ3080', 'TQ3081' (adjacent cells, distance = 1) | bng_distance, bng_euclideandistance |
bng_points | easting INT, northing INT, geom STRING | Easting 530000, northing 180000, WKT point (EPSG:27700) | bng_pointascell, bng_eastnorthasbng |
bng_polygons | geom STRING | 3km × 3km BNG polygon in EPSG:27700 (London area) | bng_geomkring, bng_geomkloop, bng_polyfill, bng_tessellate, and the 5 *explode generators |
bng_chips | chip STRUCT<cellid STRING, core BOOLEAN, chip BINARY> | 9 tessellation chips from the BNG polygon at resolution 3 | bng_cellintersection, bng_cellunion, bng_cellintersection_agg, bng_cellunion_agg |
quadbin_cells | cell LONG | Inline literal 5233961839712272383 (San Francisco, zoom 10) | quadbin_aswkb, quadbin_centroid, quadbin_resolution, quadbin_kring, quadbin_cellunion |
quadbin_cell_pairs | cell1 LONG, cell2 LONG | Two cells at (0.0, 0.0, z10) and (0.0, 0.1, z10) — distance = 1 | quadbin_distance |
quadbin_polygons | geom STRING | WGS84 polygon (-1,-1) → (1,1) | quadbin_polyfill, quadbin_tessellate |
quadbin_kring_cells | cell LONG | 9 cells from kring(SF-z10, k=1) | quadbin_cellunion_agg |
custom_grids | grid STRUCT, cell LONG, point STRING | BNG-like custom grid; cell at POINT(530000 180000), resolution 5 | All 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).
plot_staticvizx.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:
| Tab | Surface |
|---|---|
| SQL | Session SQL, after register(spark) |
| Unity Catalog SQL | A <catalog>.<schema>.gbx_* function, where the function exists in Unity Catalog |
| Python | The 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 byBNG.getResolutionand raises an error. bng_cellareareturns square kilometres — not square metres. A 1km cell returns1.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).
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_distanceaccept zoom0..26.gbx_quadbin_polyfillandgbx_quadbin_tessellateaccept zoom0..20.
quadbin_pointascell
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_pointascell(longitude, latitude, resolution) | BIGINT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_pointascell(longitude, latitude, resolution) | BIGINT |
| Python | gridx.quadbin.functions.quadbin_pointascell(longitude, latitude, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
longitude | DOUBLE | DOUBLE | Longitude in EPSG:4326 degrees. | |
latitude | DOUBLE | DOUBLE | Latitude in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin 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.
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:
BIGINTquadbin cell ID
- SQL
- Python
SELECT gbx_quadbin_pointascell(-122.4194, 37.7749, 10) as sf_cell;
+-------------------+
|sf_cell |
+-------------------+
|5233961839712272383|
+-------------------+
from pyspark.sql import functions as f
df = spark.sql("SELECT -122.4194 AS lon, 37.7749 AS lat, 10 AS zoom")
result = df.select(
qx.quadbin_pointascell(f.col("lon"), f.col("lat"), f.col("zoom")).alias(
"sf_cell"
)
).first()
+-------------------+
|sf_cell |
+-------------------+
|5233961839712272383|
+-------------------+
quadbin_aswkb
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_aswkb(cell) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_aswkb(cell) | BINARY |
| Python | gridx.quadbin.functions.quadbin_aswkb(cell) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The quadbin cell id. |
Return the quadbin cell footprint as EWKB (SRID=4326) — the four-corner polygon of the tile in lon/lat.
Powered by the quadbin package + shapely. Cell boundary polygon as EWKB (SRID 4326).
Returns:
- Binary EWKB polygon (SRID-tagged 4326)
- SQL
- Python
SELECT gbx_quadbin_aswkb(gbx_quadbin_pointascell(-122.4194, 37.7749, 10)) AS wkb;
+--------+
|wkb |
+--------+
|[binary]|
+--------+
... (EWKB binary — quadbin cell footprint polygon, SRID 4326)
from pyspark.sql import functions as f
df = spark.table("quadbin_cells")
result = df.select(qx.quadbin_aswkb(f.col("cell")).alias("wkb")).first()
+--------+
|wkb |
+--------+
|[binary]|
+--------+
... (EWKB binary — quadbin cell footprint polygon, SRID 4326)
quadbin_centroid
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_centroid(cell) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_centroid(cell) | BINARY |
| Python | gridx.quadbin.functions.quadbin_centroid(cell) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The quadbin cell id. |
Return the quadbin cell centroid as an EWKB POINT (SRID=4326).
Powered by the quadbin package + shapely. Cell centroid (bbox-corner mean) as EWKB point (SRID 4326).
Returns:
- Binary EWKB point (SRID-tagged 4326)
- SQL
- Python
SELECT gbx_quadbin_centroid(gbx_quadbin_pointascell(-122.4194, 37.7749, 10)) AS centroid;
+--------+
|centroid|
+--------+
|[binary]|
+--------+
... (EWKB binary — POINT at SF z10 cell centroid, SRID 4326)
from pyspark.sql import functions as f
df = spark.table("quadbin_cells")
result = df.select(qx.quadbin_centroid(f.col("cell")).alias("centroid")).first()
+-----------+
|centroid |
+-----------+
|[binary] |
+-----------+
... (EWKB binary — POINT at SF z10 cell centroid, SRID 4326)
quadbin_resolution
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_resolution(cell) | INT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_resolution(cell) | INT |
| Python | gridx.quadbin.functions.quadbin_resolution(cell) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The quadbin cell id. |
Return the resolution (zoom) of a quadbin cell.
Powered by the quadbin package (numpy-vectorized). Extracts the zoom level from a cell id.
Returns:
INTzoom level (0..26)
- SQL
- Python
SELECT gbx_quadbin_resolution(gbx_quadbin_pointascell(-122.4194, 37.7749, 10)) AS z;
+--+
|z |
+--+
|10|
+--+
from pyspark.sql import functions as f
df = spark.table("quadbin_cells")
result = df.select(qx.quadbin_resolution(f.col("cell")).alias("z")).first()
+--+
|z |
+--+
|10|
+--+
quadbin_polyfill
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_polyfill(geom, resolution) | array<bigint> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_polyfill(geom, resolution) | array<bigint> |
| Python | gridx.quadbin.functions.quadbin_polyfill(geom, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin zoom level, a whole number from 0 to 20. |
Polyfill a geometry's bounding box with all quadbin cells at the given zoom.
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
- SQL
- Python
SELECT gbx_quadbin_polyfill('POLYGON((-1 -1, 1 -1, 1 1, -1 1, -1 -1))', 5) AS cells;
+--------------------------+
|cells |
+--------------------------+
|[5211790668774506495, ...]|
+--------------------------+
... (4 cells covering the WGS84 polygon at zoom 5)
from pyspark.sql import functions as f
df = spark.table("quadbin_polygons")
result = df.select(
qx.quadbin_polyfill(f.col("geom"), f.lit(5)).alias("cells")
).first()
+--------------------------+
|cells |
+--------------------------+
|[5211790668774506495, ...]|
+--------------------------+
... (4 cells covering the WGS84 polygon at zoom 5)
quadbin_kring
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_kring(cell, k) | array<bigint> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_kring(cell, k) | array<bigint> |
| Python | gridx.quadbin.functions.quadbin_kring(cell, k) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The quadbin cell id. | |
k | INT | INT | The 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).
Powered by the quadbin package. All cells within Chebyshev distance k (inclusive).
Returns:
ARRAY<BIGINT>of cell IDs (length(2k+1)^2)
- SQL
- Python
SELECT gbx_quadbin_kring(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1) AS kring;
+-------------------------------------+
|kring |
+-------------------------------------+
|[5233961839712272383, ..., (9 cells)]|
+-------------------------------------+
... (9 cells: SF z10 center plus 8 surrounding cells at k=1)
from pyspark.sql import functions as f
df = spark.table("quadbin_cells")
result = df.select(qx.quadbin_kring(f.col("cell"), f.lit(1)).alias("kring")).first()
+-------------------------------------+
|kring |
+-------------------------------------+
|[5233961839712272383, ..., (9 cells)]|
+-------------------------------------+
... (9 cells: SF z10 center plus 8 surrounding cells at k=1)
quadbin_kloop
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_kloop(cell, k) | array<bigint> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_kloop(cell, k) | array<bigint> |
| Python | gridx.quadbin.functions.quadbin_kloop(cell, k) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The quadbin cell id. | |
k | INT | INT | The 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.
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 distancek(up to(2k+1)^2 - (2(k-1)+1)^2cells)
- SQL
- Python
SELECT gbx_quadbin_kloop(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1) AS kloop;
+----------------+
|kloop |
+----------------+
|[..., (8 cells)]|
+----------------+
... (8 cells: hollow ring at k=1, SF z10 center excluded)
from pyspark.sql import functions as f
df = _get_quadbin_cells_df(spark)
result = df.select(
qx.quadbin_kloop(f.col("cell"), f.lit(1)).alias("kloop")
).first()
+-------------------------------+
|kloop |
+-------------------------------+
|[..., (8 cells at k=1)] |
+-------------------------------+
... (8 BIGINT cell IDs — hollow ring at k=1; center cell 5233961839712272383 excluded)
quadbin_tessellate
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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>> |
| Python | gridx.quadbin.functions.quadbin_tessellate(geom, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin 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.
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>>
- SQL
- Python
SELECT gbx_quadbin_tessellate('POLYGON((-1 -1, 1 -1, 1 1, -1 1, -1 -1))', 5) AS chips;
+----------------------------------------------+
|chips |
+----------------------------------------------+
|[{5211790668774506495, [binary]}, {5212..., ..|
+----------------------------------------------+
... (4 chips: each quadbin cell paired with its clipped geometry WKB (SRID 4326))
from pyspark.sql import functions as f
df = spark.table("quadbin_polygons")
result = df.select(
qx.quadbin_tessellate(f.col("geom"), f.lit(5)).alias("chips")
).first()
+----------------------------------------------+
|chips |
+----------------------------------------------+
|[{5211790668774506495, [binary]}, {5212..., ..|
+----------------------------------------------+
... (4 chips: each quadbin cell paired with its clipped geometry WKB (SRID 4326))
quadbin_cellunion
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_cellunion(cells) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_cellunion(cells) | BINARY |
| Python | gridx.quadbin.functions.quadbin_cellunion(cells) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cells | ARRAY<BIGINT> | ARRAY<BIGINT> | The quadbin cell ids to merge. |
Union an ARRAY<BIGINT> of quadbin cells into a single MultiPolygon EWKB.
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)
- SQL
- Python
SELECT gbx_quadbin_cellunion(
gbx_quadbin_kring(gbx_quadbin_pointascell(-122.4194, 37.7749, 10), 1)
) AS union_geom;
+----------+
|union_geom|
+----------+
|[binary] |
+----------+
... (EWKB binary — MultiPolygon dissolving the SF z10 kring, SRID 4326)
from pyspark.sql import functions as f
df = spark.table("quadbin_cells")
result = df.select(
qx.quadbin_cellunion(qx.quadbin_kring(f.col("cell"), f.lit(1))).alias(
"union_geom"
)
).first()
+-----------+
|union_geom |
+-----------+
|[binary] |
+-----------+
... (EWKB binary — MultiPolygon dissolving the SF z10 kring, SRID 4326)
quadbin_cellunion_agg
SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_cellunion_agg(cellid) | BINARY |
| Python | gridx.quadbin.functions.quadbin_cellunion_agg(cellid) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
cellid | BIGINT | The quadbin cell id. A NULL id is skipped; a repeated cell counts once. |
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.
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:
BINARYEWKB multipolygon (SRID-tagged 4326) representing the dissolved coverage
- SQL
- Python
-- 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;
+------+--------+
|region|coverage|
+------+--------+
|R1 |[binary]|
+------+--------+
... (BINARY EWKB — dissolved coverage of all 9 kring cells around SF z10, SRID 4326)
from pyspark.sql import functions as f
df = spark.table("quadbin_kring_cells")
df_keyed = df.withColumn("region", f.lit("R1"))
result = (
df_keyed.groupBy("region")
.agg(qx.quadbin_cellunion_agg("cell").alias("coverage"))
.first()
)
+------+--------+
|region|coverage|
+------+--------+
|R1 |[binary]|
+------+--------+
... (BINARY EWKB — dissolved coverage of all 9 kring cells around SF z10, SRID 4326)
quadbin_distance
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_distance(cellid1, cellid2) | INT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_quadbin_distance(cellid1, cellid2) | INT |
| Python | gridx.quadbin.functions.quadbin_distance(cellid1, cellid2) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid1 | BIGINT | BIGINT | The first quadbin cell id. | |
cellid2 | BIGINT | BIGINT | The second quadbin cell id, at the same zoom level. |
Chebyshev (king-move) distance between two quadbin cells at the same resolution.
Powered by the quadbin package. Chebyshev distance on tile coordinates. It raises if the two cells differ in resolution.
Returns:
INTcell-step distance
- SQL
- Python
SELECT gbx_quadbin_distance(
gbx_quadbin_pointascell(0.0, 0.0, 10),
gbx_quadbin_pointascell(0.0, 0.1, 10)
) AS d;
+-+
|d|
+-+
|1|
+-+
from pyspark.sql import functions as f
df = spark.table("quadbin_cell_pairs")
result = df.select(
qx.quadbin_distance(f.col("cell1"), f.col("cell2")).alias("d")
).first()
+--+
|d |
+--+
|1 |
+--+
quadbin_cellfill
SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_quadbin_cellfill(cellid, value, [k], [method], [power]) | BINARY |
| Python | gridx.quadbin.functions.quadbin_cellfill(cellid, value, k=None, method=None, power=None) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
cellid | BIGINT | The quadbin cell id. | |
value | DOUBLE | The cell's value; NULL marks a covered cell with no value, which is filled. | |
k | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | 2.0 | The distance exponent of 'idw'; unused by 'mean'. |
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.
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.
Powered by the pure-Python quadbin interpolation port.
Returns: BINARY, the serialized fill described above.
- SQL
- Python
-- 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;
+------+--------+
|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)
from databricks.labs.gbx import manager as gbx_manager
from pyspark.sql import functions as f
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
# Hardcoded London z10 cell and two neighbours; tier-agnostic.
df = spark.createDataFrame(
[
(1, 5234158540624494591, None),
(1, 5234158544919461887, 5.0),
(1, 5234158536329527295, 5.0),
],
["region", "cellid", "value"],
)
result = (
df.groupBy("region")
.agg(
qx.quadbin_cellfill(
f.col("cellid"), f.col("value"), f.lit(1), f.lit("mean"), f.lit(2.0)
).alias("filled")
)
.first()
)
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: 3 cells; the London z10 center cell is filled to 5.0, the 2 neighbours stay 5.0)
quadbin_cellfill_array
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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>> |
| Python | gridx.quadbin.functions.quadbin_cellfill_array(cells, k=None, method=None, power=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cells | array<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. | |
k | INT | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | DOUBLE | 2.0 | The 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.
- SQL
- Python
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;
+------+------------------------------------------------------------------------------------+
|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)
from pyspark.sql import functions as f
_register_grids(spark)
df = spark.createDataFrame(
[
(1, 5234158540624494591, None),
(1, 5234158544919461887, 5.0),
(1, 5234158536329527295, 5.0),
],
"region INT, cellid BIGINT, value DOUBLE",
)
result = (
df.groupBy("region")
.agg(
qx.quadbin_cellfill_array(
f.collect_list(f.struct("cellid", "value")), 1, "mean", 2.0
).alias("filled")
)
.first()
)
+------+------------------------------------------------------------------------------------+
|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-*:
| Mode | Behaviour |
|---|---|
'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:
| Coverage | A 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| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.quadbin.functions.quadbin_geomkring(geom, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin zoom level, a whole number from 0 to 20. | |
k | INT | INT | The ring distance: 0 is the covering cells alone, 1 adds the cells around them. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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
- SQL
- Python
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;
+----------------------------------------+
|kring |
+----------------------------------------+
|[..., (cells within k=1 ring at res 12)]|
+----------------------------------------+
... (ARRAY<BIGINT> — quadbin covering set plus one outer ring)
from pyspark.sql import functions as f
df = _get_quadbin_polygons_df(spark)
result = df.select(
qx.quadbin_geomkring(f.col("geom"), f.lit(12), f.lit(1)).alias("kring")
).first()
+-------------------------------------+
|kring |
+-------------------------------------+
|[5211790602025803775, ..., (n cells)]|
+-------------------------------------+
... (ARRAY<BIGINT> — covering cells of WGS84 polygon at z12 expanded by k=1 ring)
quadbin_geomkloop
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.quadbin.functions.quadbin_geomkloop(geom, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin zoom level, a whole number from 0 to 20. | |
k | INT | INT | The loop distance: only the cells at exactly this ring distance from the covering cells are returned. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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
- SQL
- Python
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;
+--------------------------------------------+
|kloop |
+--------------------------------------------+
|[..., (outer ring cells, polyfill excluded)]|
+--------------------------------------------+
... (ARRAY<BIGINT> — hollow quadbin outer ring at k=1)
from pyspark.sql import functions as f
df = _get_quadbin_polygons_df(spark)
result = df.select(
qx.quadbin_geomkloop(f.col("geom"), f.lit(12), f.lit(1)).alias("kloop")
).first()
+-------------------------------------+
|kloop |
+-------------------------------------+
|[5211790602025803775, ..., (n cells)]|
+-------------------------------------+
... (ARRAY<BIGINT> — outer ring at k=1, interior polyfill cells excluded)
quadbin_geomkringexplode
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.quadbin.functions.quadbin_geomkringexplode(df, geom, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid BIGINT.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin zoom level, a whole number from 0 to 20. | |
k | INT | INT | The ring distance: 0 is the covering cells alone, 1 adds the cells around them. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
Streaming version of quadbin_geomkring — one row per cell. Call it with FROM t, LATERAL gbx_...(...), at every implementation level.
Returns:
- One row per
BIGINTcell ID in the k-ring
- SQL
- Python
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;
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 ring of the geometry)
from databricks.labs.gbx import manager as gbx_manager
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
_get_quadbin_polygons_df(spark).createOrReplaceTempView("_qb_geomk_src")
result = spark.sql(
"SELECT t.cellid FROM _qb_geomk_src src, "
"LATERAL gbx_quadbin_geomkringexplode(src.geom, 12, 1, 'boundary-out') t"
).collect()
spark.catalog.dropTempView("_qb_geomk_src")
+------------------+
|cellid |
+------------------+
|5211790602025803775|
|... |
+------------------+
... (one BIGINT row per cell in the k=1 ring around the WGS84 polygon polyfill at z12)
quadbin_geomkloopexplode
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.quadbin.functions.quadbin_geomkloopexplode(df, geom, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid BIGINT.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | Quadbin zoom level, a whole number from 0 to 20. | |
k | INT | INT | The loop distance: only the cells at exactly this ring distance from the covering cells are returned. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
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
BIGINTcell ID at exactly distance k
- SQL
- Python
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;
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 hollow outer ring)
from databricks.labs.gbx import manager as gbx_manager
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
_get_quadbin_polygons_df(spark).createOrReplaceTempView("_qb_geomk_src")
result = spark.sql(
"SELECT t.cellid FROM _qb_geomk_src src, "
"LATERAL gbx_quadbin_geomkloopexplode(src.geom, 12, 1, 'boundary-out') t"
).collect()
spark.catalog.dropTempView("_qb_geomk_src")
+------------------+
|cellid |
+------------------+
|5211790602025803775|
|... |
+------------------+
... (one BIGINT row per cell in the hollow outer ring at k=1 around the polygon polyfill at z12)
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 viaBNG.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 precisionTQ 38000 80000— 10m precisionSU 12 34— Different grid square
Precision Levels
| Precision | Grid Size | Example | Use Case |
|---|---|---|---|
| 100000m | 100km x 100km | TQ | Regional analysis |
| 10000m | 10km x 10km | TQ38 | District-level |
| 1000m | 1km x 1km | TQ3080 | Local area analysis |
| 100m | 100m x 100m | TQ308808 | Neighborhood level |
| 10m | 10m x 10m | TQ30808080 | Building level |
| 1m | 1m x 1m | TQ3080080800 | Precise 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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_aswkb(cellid) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_aswkb(cellid) | BINARY |
| Python | gridx.bng.functions.bng_aswkb(cellid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. |
Convert a BNG cell ID to Well-Known Binary (WKB) format.
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
- SQL
- Python
SELECT gbx_bng_aswkb('TQ3080') as wkb_geom;
+--------+
|wkb_geom|
+--------+
|[binary]|
+--------+
... (WKB binary)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cells_df(spark)
result = df.select(bx.bng_aswkb(f.col("cellid")).alias("wkb")).first()
+--------+
|wkb |
+--------+
|[binary]|
+--------+
... (WKB binary)
bng_aswkt
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_aswkt(cellid) | STRING |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_aswkt(cellid) | STRING |
| Python | gridx.bng.functions.bng_aswkt(cellid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. |
Convert a BNG cell ID to Well-Known Text (WKT) format.
Powered by a pure-Python port of BNG.scala + shapely. The cell footprint as WKT in EPSG:27700 coordinates.
Returns:
- String WKT geometry representation
- SQL
- Python
SELECT gbx_bng_aswkt('TQ3080') as wkt_geom;
+----------------------------------------------------------+
|wkt_geom |
+----------------------------------------------------------+
|POLYGON ((530000 180000, 531000 180000, 531000 181000, ...|
+----------------------------------------------------------+
POLYGON ((530000 180000, 531000 180000, 531000 181000, 530000 181000, 530000 180000))
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cells_df(spark)
result = df.select(bx.bng_aswkt(f.col("cellid")).alias("wkt")).first()
+----------------------------------------------------------+
|wkt |
+----------------------------------------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_cellarea(cellid) | DOUBLE |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_cellarea(cellid) | DOUBLE |
| Python | gridx.bng.functions.bng_cellarea(cellid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. |
Calculate the area of a BNG grid cell.
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
- SQL
- Python
SELECT gbx_bng_cellarea('TQ3080') AS area_km2;
+--------+
|area_km2|
+--------+
|1.0 |
+--------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cells_df(spark)
result = df.select(bx.bng_cellarea(f.col("cellid")).alias("area_km2")).first()
+---------+
|area_km2 |
+---------+
|1.0 |
+---------+
bng_centroid
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_centroid(cellid) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_centroid(cellid) | BINARY |
| Python | gridx.bng.functions.bng_centroid(cellid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. |
Get the centroid (center point) of a BNG cell.
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
- SQL
- Python
SELECT gbx_bng_centroid('TQ3080') as centroid;
+--------+
|centroid|
+--------+
|[binary]|
+--------+
... (WKB binary — POINT(530500 180500) in EPSG:27700)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cells_df(spark)
result = df.select(bx.bng_centroid(f.col("cellid")).alias("centroid")).first()
+-----------+
|centroid |
+-----------+
|[binary] |
+-----------+
... (WKB binary — POINT(530500 180500) in EPSG:27700)
bng_distance
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_distance(cellid1, cellid2) | BIGINT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_distance(cellid1, cellid2) | BIGINT |
| Python | gridx.bng.functions.bng_distance(cellid1, cellid2) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid1 | STRING | STRING | The first BNG cell id. | |
cellid2 | STRING | STRING | The second BNG cell id. |
Return the grid-step distance between two BNG cells.
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.
- SQL
- Python
SELECT gbx_bng_distance('TQ3080', 'TQ3081') AS dist_steps;
+----------+
|dist_steps|
+----------+
|1 |
+----------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cell_pairs_df(spark)
result = df.select(
bx.bng_distance(f.col("cellid1"), f.col("cellid2")).alias("dist_steps")
).first()
+----------+
|dist_steps|
+----------+
|1 |
+----------+
bng_euclideandistance
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_euclideandistance(cellid1, cellid2) | BIGINT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_euclideandistance(cellid1, cellid2) | BIGINT |
| Python | gridx.bng.functions.bng_euclideandistance(cellid1, cellid2) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid1 | STRING | STRING | The first BNG cell id. | |
cellid2 | STRING | STRING | The second BNG cell id. |
Return the Chebyshev grid-unit distance between two BNG cells.
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.
- SQL
- Python
SELECT gbx_bng_euclideandistance('TQ3080', 'TQ3081') AS euclidean_dist;
+--------------+
|euclidean_dist|
+--------------+
|1 |
+--------------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cell_pairs_df(spark)
result = df.select(
bx.bng_euclideandistance(f.col("cellid1"), f.col("cellid2")).alias(
"euclidean_dist"
)
).first()
+--------------+
|euclidean_dist|
+--------------+
|1 |
+--------------+
Cell Operations
Operations combining multiple BNG chip structs.
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| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.bng.functions.bng_cellintersection(left_chip, right_chip) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
left_chip | struct<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_chip | struct<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.
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}.
- SQL
- Python
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';
+--------------------+
|intersection_chip |
+--------------------+
|{TQ3080, true, null}|
+--------------------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_chips_df(spark)
result = (
df.filter(f.col("chip.cellid") == "TQ3080")
.select(
bx.bng_cellintersection(f.col("chip"), f.col("chip")).alias(
"intersection_chip"
)
)
.first()
)
+--------------------+
|intersection_chip |
+--------------------+
|{TQ3080, true, null}|
+--------------------+
bng_cellunion
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.bng.functions.bng_cellunion(left_chip, right_chip) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
left_chip | struct<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_chip | struct<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.
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}.
- SQL
- Python
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';
+--------------------+
|union_chip |
+--------------------+
|{TQ3080, true, null}|
+--------------------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_chips_df(spark)
result = (
df.filter(f.col("chip.cellid") == "TQ3080")
.select(bx.bng_cellunion(f.col("chip"), f.col("chip")).alias("union_chip"))
.first()
)
+--------------------+
|union_chip |
+--------------------+
|{TQ3080, true, null}|
+--------------------+
Coordinate Conversion Functions
Convert coordinates or geometries to BNG cells.
bng_eastnorthasbng
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_eastnorthasbng(easting, northing, resolution) | STRING |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_eastnorthasbng(easting, northing, resolution) | STRING |
| Python | gridx.bng.functions.bng_eastnorthasbng(easting, northing, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
easting | DOUBLE | DOUBLE | Easting in EPSG:27700 metres. | |
northing | DOUBLE | DOUBLE | Northing in EPSG:27700 metres. | |
resolution | STRING or INT | STRING | BNG 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.
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)
- SQL
- Python
-- Convert OS Grid Reference coordinates (easting, northing); resolution '1km' or integer 3
SELECT gbx_bng_eastnorthasbng(530000, 180000, '1km') as bng_cell;
+--------+
|bng_cell|
+--------+
|TQ3080 |
+--------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_coordinates_df(spark)
result = df.select(
bx.bng_eastnorthasbng(f.col("easting"), f.col("northing"), f.lit("1km")).alias(
"bng_cell"
)
).first()
+--------+
|bng_cell|
+--------+
|TQ3080 |
+--------+
bng_pointascell
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_pointascell(geom, resolution) | STRING |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_pointascell(geom, resolution) | STRING |
| Python | gridx.bng.functions.bng_pointascell(geom, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres. | |
resolution | INT or STRING | STRING | BNG 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).
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)
- SQL
- Python
-- Point in BNG coordinates (eastings, northings); resolution '1km' for 1 km cell
SELECT gbx_bng_pointascell('POINT(530000 180000)', '1km') AS bng_cell;
+--------+
|bng_cell|
+--------+
|TQ3080 |
+--------+
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_coordinates_df(spark)
result = df.select(
bx.bng_pointascell(f.col("geom"), f.lit("1km")).alias("bng_cell")
).first()
+--------+
|bng_cell|
+--------+
|TQ3080 |
+--------+
K-Ring Functions
Generate neighboring cells using k-ring patterns.
bng_kring
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_kring(cellid, k) | array<string> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_kring(cellid, k) | array<string> |
| Python | gridx.bng.functions.bng_kring(cellid, k) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. | |
k | INT | INT | The ring distance: 0 is the cell alone, 1 adds its eight neighbours. |
Generate a k-ring of cells around a center cell (filled disk).
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
- SQL
- Python
SELECT gbx_bng_kring('TQ3080', 1) AS kring;
+-----------------------------+
|kring |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (9 cells: center TQ3080 plus 8 surrounding cells at k=1)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cells_df(spark)
result = df.select(bx.bng_kring(f.col("cellid"), f.lit(1)).alias("kring")).first()
+-----------------------------+
|kring |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (9 cells: center TQ3080 plus 8 surrounding cells at k=1)
bng_kloop
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_kloop(cellid, k) | array<string> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_kloop(cellid, k) | array<string> |
| Python | gridx.bng.functions.bng_kloop(cellid, k) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. | |
k | INT | INT | The ring distance: 1 is the eight neighbours of the cell. |
Generate a k-loop of cells around a center cell (hollow ring).
Powered by pure Python. The cells at exactly ring distance k.
Returns:
- Array of BNG cell references at exactly distance k
- SQL
- Python
SELECT gbx_bng_kloop('TQ3080', 1) AS kloop;
+-----------------------------+
|kloop |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (8 cells: hollow ring at k=1, center TQ3080 excluded)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_cells_df(spark)
result = df.select(bx.bng_kloop(f.col("cellid"), f.lit(1)).alias("kloop")).first()
+-----------------------------+
|kloop |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (8 cells: hollow ring at k=1, center TQ3080 excluded)
bng_geomkring
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.bng.functions.bng_geomkring(geom, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres. | |
resolution | INT or STRING | STRING | BNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'. | |
k | INT | INT | The number of rings of dilation around the geometry's cells. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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.
Powered by pure Python + shapely. Polyfills the geometry then expands by ring distance k.
Returns:
ARRAY<STRING>of BNG cell references
- SQL
- Python
SELECT gbx_bng_geomkring(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3, 1, 'boundary-out'
) AS kring;
+-----------------------------+
|kring |
+-----------------------------+
|[TQ2878, TQ2879, TQ2880, ...]|
+-----------------------------+
... (24 cells: boundary band expanded by k=1 ring; interior centre not filled)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_polygons_df(spark)
result = df.select(
bx.bng_geomkring(f.col("geom"), f.lit(3), f.lit(1)).alias("kring")
).first()
+-----------------------------+
|kring |
+-----------------------------+
|[TQ2878, TQ2879, TQ2880, ...]|
+-----------------------------+
... (24 cells: boundary band expanded by k=1 ring; interior centre not filled)
bng_geomkloop
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.bng.functions.bng_geomkloop(geom, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres. | |
resolution | INT or STRING | STRING | BNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'. | |
k | INT | INT | The number of rings of dilation around the geometry's cells. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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).
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
- SQL
- Python
SELECT gbx_bng_geomkloop(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3, 1, 'boundary-out'
) AS kloop;
+-----------------------------+
|kloop |
+-----------------------------+
|[TQ2878, TQ2879, TQ2880, ...]|
+-----------------------------+
... (16 cells: outer ring at k=1 around the BNG polygon polyfill)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_polygons_df(spark)
result = df.select(
bx.bng_geomkloop(f.col("geom"), f.lit(3), f.lit(1)).alias("kloop")
).first()
+-----------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_polyfill(geom, resolution) | array<string> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_polyfill(geom, resolution) | array<string> |
| Python | gridx.bng.functions.bng_polyfill(geom, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres. | |
resolution | INT or STRING | STRING | BNG 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.
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
- SQL
- Python
SELECT gbx_bng_polyfill(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3
) AS cells;
+-----------------------------+
|cells |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (9 cells covering the 3km × 3km BNG polygon at 1km resolution)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_polygons_df(spark)
result = df.select(bx.bng_polyfill(f.col("geom"), f.lit(3)).alias("cells")).first()
+-----------------------------+
|cells |
+-----------------------------+
|[TQ2979, TQ2980, TQ2981, ...]|
+-----------------------------+
... (9 cells covering the 3km × 3km BNG polygon at 1km resolution)
bng_tessellate
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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>> |
| Python | gridx.bng.functions.bng_tessellate(geom, resolution, keep_core_geom=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT. | |
resolution | INT or STRING | STRING | BNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'. | |
keep_core_geom | BOOLEAN | BOOLEAN | false | When 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.
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)
- SQL
- Python
SELECT gbx_bng_tessellate(
'POLYGON((529000 179000, 529000 182000, 532000 182000, 532000 179000, 529000 179000))',
3
) AS chips;
+--------------------------------------------+
|chips |
+--------------------------------------------+
|[{TQ2979, false, [binary]}, {TQ3080, true,..|
+--------------------------------------------+
... (9 chips; TQ3080 is core (core=true, chip=null); border cells carry WKB clip geometry)
from pyspark.sql import functions as f
from databricks.labs.gbx.gridx.bng import functions as bx
df = _get_bng_polygons_df(spark)
result = df.select(
bx.bng_tessellate(f.col("geom"), f.lit(3)).alias("chips")
).first()
+--------------------------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_cellintersection_agg(input_chip) | BINARY |
| Python | gridx.bng.functions.bng_cellintersection_agg(input_chip) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
input_chip | struct<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). |
Aggregate intersection of multiple BNG cells.
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.
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
- SQL
- Python
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;
# 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)
from pyspark.sql import functions as f
df = _get_bng_chips_df(spark)
df_keyed = df.select(
f.col("chip"),
f.col("chip.cellid").alias("cellid"),
)
result = (
df_keyed.groupBy("cellid")
.agg(bx.bng_cellintersection_agg("chip").alias("common_chip"))
.filter(f.col("cellid") == "TQ3080")
.first()
)
+-------+------------+
|cellid |common_chip |
+-------+------------+
|TQ3080 |[binary] |
+-------+------------+
... (BINARY — dissolved chip WKB for TQ3080; core cell → full cell polygon in EPSG:27700)
bng_cellintersection_array
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_cellintersection_array(chips) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_cellintersection_array(chips) | BINARY |
| Python | gridx.bng.functions.bng_cellintersection_array(chips) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
chips | array<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.
- SQL
- Python
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';
+-----------+
|common_chip|
+-----------+
|[binary] |
+-----------+
... (BINARY — WKB of the strip x 530400-530600 of cell TQ3080, in EPSG:27700)
from pyspark.sql import functions as f
chips = _tq3080_chips(spark)
result = chips.agg(
bx.bng_cellintersection_array(f.collect_list("chip")).alias("common_chip")
).first()
+-----------+
|common_chip|
+-----------+
|[binary] |
+-----------+
... (BINARY — WKB of the strip x 530400-530600 of cell TQ3080, in EPSG:27700)
bng_cellunion_agg
SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_cellunion_agg(input_chip) | BINARY |
| Python | gridx.bng.functions.bng_cellunion_agg(input_chip) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
input_chip | struct<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). |
Aggregate union of multiple BNG cells.
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.
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
- SQL
- Python
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;
# 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)
from pyspark.sql import functions as f
df = _get_bng_chips_df(spark)
df_keyed = df.select(
f.col("chip"),
f.col("chip.cellid").alias("cellid"),
)
result = (
df_keyed.groupBy("cellid")
.agg(bx.bng_cellunion_agg("chip").alias("union_chip"))
.filter(f.col("cellid") == "TQ3080")
.first()
)
+-------+----------+
|cellid |union_chip|
+-------+----------+
|TQ3080 |[binary] |
+-------+----------+
... (BINARY — dissolved chip WKB for TQ3080; core cell → full cell polygon in EPSG:27700)
bng_cellunion_array
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_cellunion_array(chips) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_cellunion_array(chips) | BINARY |
| Python | gridx.bng.functions.bng_cellunion_array(chips) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
chips | array<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.
- SQL
- Python
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';
+----------+
|union_chip|
+----------+
|[binary] |
+----------+
... (BINARY — WKB of the union, the whole cell TQ3080 (x 530000-531000), in EPSG:27700)
from pyspark.sql import functions as f
chips = _tq3080_chips(spark)
result = chips.agg(
bx.bng_cellunion_array(f.collect_list("chip")).alias("union_chip")
).first()
+----------+
|union_chip|
+----------+
|[binary] |
+----------+
... (BINARY — WKB of the union, the whole cell TQ3080 (x 530000-531000), in EPSG:27700)
bng_cellfill
SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_cellfill(cellid, value, [k], [method], [power]) | BINARY |
| Python | gridx.bng.functions.bng_cellfill(cellid, value, k=None, method=None, power=None) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
cellid | STRING | The BNG cell id, for example 'TQ3080'. | |
value | DOUBLE | The cell's value; NULL marks a covered cell with no value, which is filled. | |
k | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | 2.0 | The distance exponent of 'idw'; unused by 'mean'. |
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.
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.
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.
- SQL
- Python
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;
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: TQ300800 filled to 5.0; ring-1 neighbours unchanged)
from databricks.labs.gbx import manager as gbx_manager
from pyspark.sql import functions as f
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
df = spark.createDataFrame(
[
(1, "TQ300800", None),
(1, "TQ299800", 5.0),
(1, "TQ301800", 5.0),
(1, "TQ300799", 5.0),
(1, "TQ300801", 5.0),
],
["region", "cellid", "value"],
)
result = (
df.groupBy("region")
.agg(
bx.bng_cellfill(
f.col("cellid"), f.col("value"), f.lit(1), f.lit("mean"), f.lit(2.0)
).alias("filled")
)
.first()
)
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: TQ300800 filled to 5.0; ring-1 neighbours unchanged)
bng_cellfill_array
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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>> |
| Python | gridx.bng.functions.bng_cellfill_array(cells, k=None, method=None, power=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cells | array<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. | |
k | INT | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | DOUBLE | 2.0 | The 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.
- SQL
- Python
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;
+------+-------------------------------------------------------------------------------------+
|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)
from pyspark.sql import functions as f
_register_grids(spark)
df = spark.createDataFrame(
[
(1, "TQ300800", None),
(1, "TQ299800", 5.0),
(1, "TQ301800", 5.0),
(1, "TQ300799", 5.0),
(1, "TQ300801", 5.0),
],
"region INT, cellid STRING, value DOUBLE",
)
result = (
df.groupBy("region")
.agg(
bx.bng_cellfill_array(
f.collect_list(f.struct("cellid", "value")), 1, "mean", 2.0
).alias("filled")
)
.first()
)
+------+-------------------------------------------------------------------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_kringexplode(cellid, k) | rows |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_kringexplode(cellid, k) | rows |
| Python | gridx.bng.functions.bng_kringexplode(df, cellid, k) | DataFrame |
Rows: cellid STRING.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. | |
k | INT | INT | The ring distance: 0 is the cell alone, 1 adds its eight neighbours. |
Explode k-ring cells into separate rows.
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
- SQL
- Python
SELECT t.cellid
FROM (SELECT 'TQ3080' AS cell) src,
LATERAL gbx_bng_kringexplode(src.cell, 1) t;
+------+
|cellid|
+------+
|TQ3080|
|TQ2979|
|TQ2981|
|TQ3181|
|TQ3179|
|TQ2980|
|TQ3180|
|TQ3081|
|TQ3079|
+------+
... (9 rows: center TQ3080 plus its 8 neighbours at k=1)
result = spark.sql("""
SELECT t.*
FROM (SELECT 'TQ3080' AS cellid) src,
LATERAL gbx_bng_kringexplode(src.cellid, 1) t
""")
+------+
|cellid|
+------+
|TQ2979|
|TQ2980|
|TQ2981|
|TQ3079|
|TQ3080|
|TQ3081|
|TQ3179|
|TQ3180|
|TQ3181|
+------+
... (9 rows: center TQ3080 plus 8 surrounding cells at k=1)
bng_kloopexplode
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_bng_kloopexplode(cellid, k) | rows |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_bng_kloopexplode(cellid, k) | rows |
| Python | gridx.bng.functions.bng_kloopexplode(df, cellid, k) | DataFrame |
Rows: cellid STRING.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid | STRING | STRING | The BNG cell id, for example 'TQ3080'. | |
k | INT | INT | The ring distance: 1 is the eight neighbours of the cell. |
Explode k-loop cells into separate rows.
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
- SQL
- Python
SELECT t.cellid
FROM (SELECT 'TQ3080' AS cell) src,
LATERAL gbx_bng_kloopexplode(src.cell, 1) t;
+------+
|cellid|
+------+
|TQ2979|
|TQ2981|
|TQ3181|
|TQ3179|
|TQ2980|
|TQ3180|
|TQ3081|
|TQ3079|
+------+
... (8 rows: hollow ring at k=1, center TQ3080 excluded)
result = spark.sql("""
SELECT t.*
FROM (SELECT 'TQ3080' AS cellid) src,
LATERAL gbx_bng_kloopexplode(src.cellid, 1) t
""")
+------+
|cellid|
+------+
|TQ2979|
|TQ2980|
|TQ2981|
|TQ3079|
|TQ3081|
|TQ3179|
|TQ3180|
|TQ3181|
+------+
... (8 rows: hollow ring at k=1, center TQ3080 excluded)
bng_geomkringexplode
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.bng.functions.bng_geomkringexplode(df, geom, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid STRING.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres. | |
resolution | INT or STRING | STRING | BNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'. | |
k | INT | INT | The number of rings of dilation around the geometry's cells. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
Explode geometry k-ring cells into separate rows.
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 (
STRINGBNG cell reference)
- SQL
- Python
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;
+-----------+
|cellid |
+-----------+
|...(STRING)|
+-----------+
... (one row per STRING BNG cell ID in the k=1 ring of the geometry)
df = _get_bng_polygons_df(spark)
df.createOrReplaceTempView("_bng_polygons_tmp")
result = spark.sql(
"SELECT t.* FROM _bng_polygons_tmp src, "
"LATERAL gbx_bng_geomkringexplode(src.geom, 3, 1) t"
)
+------+
|cellid|
+------+
|TQ2878|
|TQ2879|
|TQ2880|
|... |
+------+
... (24 rows: boundary band at res=3 expanded by k=1 ring; interior centre not filled)
bng_geomkloopexplode
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.bng.functions.bng_geomkloopexplode(df, geom, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid STRING.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:27700 metres. | |
resolution | INT or STRING | STRING | BNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'. | |
k | INT | INT | The number of rings of dilation around the geometry's cells. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
Explode geometry k-loop cells into separate rows.
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 (
STRINGBNG cell reference)
- SQL
- Python
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;
+-----------+
|cellid |
+-----------+
|...(STRING)|
+-----------+
... (one row per STRING BNG cell ID in the k=1 hollow outer ring)
df = _get_bng_polygons_df(spark)
df.createOrReplaceTempView("_bng_polygons_tmp")
result = spark.sql(
"SELECT t.* FROM _bng_polygons_tmp src, "
"LATERAL gbx_bng_geomkloopexplode(src.geom, 3, 1) t"
)
+------+
|cellid|
+------+
|TQ2878|
|TQ2879|
|TQ2880|
|... |
+------+
... (16 rows: outer ring at k=1 around the BNG polygon polyfill)
bng_tessellateexplode
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.bng.functions.bng_tessellateexplode(df, geom, resolution, keep_core_geom=None) | DataFrame |
Rows: cellid STRING, core BOOLEAN, chip BINARY.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT. | |
resolution | INT or STRING | STRING | BNG resolution: an index (1 to 6, negative for a quadrant) or a size such as '1km' or '100m'. | |
keep_core_geom | BOOLEAN | BOOLEAN | false | When true, a core cell's chip is its cell geometry; when false it is NULL. Border cells always carry their clipped geometry. |
Explode tessellated cells into separate rows.
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) andchip(BINARY, WKB in EPSG:27700), at every level and for WKB or WKT input
- SQL
- Python
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;
+------+-----+--------------------------------------------------------+
|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)
df = _get_bng_polygons_df(spark)
df.createOrReplaceTempView("_bng_polygons_tmp")
result = spark.sql(
"SELECT t.* FROM _bng_polygons_tmp src, "
"LATERAL gbx_bng_tessellateexplode(src.geom, 3) t"
)
+------+-----+--------+
|cellid|core |chip |
+------+-----+--------+
|TQ2979|false|[binary]|
|TQ2980|false|[binary]|
|TQ2981|false|[binary]|
|TQ3079|false|[binary]|
|TQ3080|true |null |
|... |... |... |
+------+-----+--------+
... (9 rows; TQ3080 is core (core=true, chip=null); border cells carry WKB clip geometry)
BNG Reference Format
Standard Format
BNG references follow: [Letters][Eastings][Northings]
Common Resolutions
| Resolution | Grid Size | Example | Use Case |
|---|---|---|---|
| 100000m | 100km × 100km | TQ | Regional |
| 10000m | 10km × 10km | TQ38 | District |
| 1000m | 1km × 1km | TQ3080 | Local area |
| 100m | 100m × 100m | TQ308808 | Neighborhood |
| 10m | 10m × 10m | TQ30808080 | Building |
| 1m | 1m × 1m | TQ3080080800 | Precise 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| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.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 |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
bound_x_min | BIGINT | BIGINT | The west edge of the grid. | |
bound_x_max | BIGINT | BIGINT | The east edge of the grid; it must be greater than the west edge. | |
bound_y_min | BIGINT | BIGINT | The south edge of the grid. | |
bound_y_max | BIGINT | BIGINT | The north edge of the grid; it must be greater than the south edge. | |
cell_splits | INT | INT | How many parts each cell splits into along each axis at the next resolution; at least 2. | |
root_cell_size_x | INT | INT | The width of a resolution-0 cell; greater than 0. | |
root_cell_size_y | INT | INT | The height of a resolution-0 cell; greater than 0. | |
srid | INT | INT | -1 | The 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 othergbx_custom_*functions.
- SQL
- Python
SELECT gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700) AS grid;
+----------------------------------------------+
|grid |
+----------------------------------------------+
|{0, 1000000, 0, 1000000, 2, 1000, 1000, 27700}|
+----------------------------------------------+
from pyspark.sql import functions as f
base = spark.sql("SELECT 1 AS dummy")
result = base.select(
cx.custom_grid(
f.lit(0),
f.lit(1000000),
f.lit(0),
f.lit(1000000),
f.lit(2),
f.lit(1000),
f.lit(1000),
f.lit(27700),
).alias("grid")
).first()
+----------------------------------------------+
|grid |
+----------------------------------------------+
|{0, 1000000, 0, 1000000, 2, 1000, 1000, 27700}|
+----------------------------------------------+
custom_pointascell
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_pointascell(point, grid, resolution) | BIGINT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_pointascell(point, grid, resolution) | BIGINT |
| Python | gridx.custom.functions.custom_pointascell(point, grid, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
point | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates. | |
grid | 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> | 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. | |
resolution | INT or STRING | STRING | The 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:
BIGINTcell ID encoding the grid position at the given resolution
- SQL
- Python
SELECT gbx_custom_pointascell('POINT(530000 180000)', gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 5) AS cell;
+------------------+
|cell |
+------------------+
|360287970373976640|
+------------------+
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_pointascell(f.col("point"), f.col("grid"), f.lit(5)).alias("cell")
).first()
+--------------------+
|cell |
+--------------------+
|360287970373976640 |
+--------------------+
custom_cellaswkb
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_cellaswkb(cell, grid) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_cellaswkb(cell, grid) | BINARY |
| Python | gridx.custom.functions.custom_cellaswkb(cell, grid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The custom-grid cell id. | |
grid | 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> | 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:
BINARYWKB polygon representing the cell boundary (geometry... (WKB binary))
- SQL
- Python
SELECT gbx_custom_cellaswkb(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700)) AS geom;
+--------+
|geom |
+--------+
|[binary]|
+--------+
... (WKB binary — 31.25m × 31.25m custom grid cell footprint polygon)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_cellaswkb(f.col("cell"), f.col("grid")).alias("geom")
).first()
+--------+
|geom |
+--------+
|[binary]|
+--------+
... (WKB binary — 31.25m × 31.25m custom grid cell footprint polygon)
custom_cellaswkt
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_cellaswkt(cell, grid) | STRING |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_cellaswkt(cell, grid) | STRING |
| Python | gridx.custom.functions.custom_cellaswkt(cell, grid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The custom-grid cell id. | |
grid | 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> | 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:
STRINGWKT polygon representing the cell boundary
- SQL
- Python
SELECT gbx_custom_cellaswkt(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700)) AS wkt;
+-------------------------------------------------------------------------------------------------+
|wkt |
+-------------------------------------------------------------------------------------------------+
|POLYGON ((530000 180000, 530031.25 180000, 530031.25 180031.25, 530000 180031.25, 530000 180000))|
+-------------------------------------------------------------------------------------------------+
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_cellaswkt(f.col("cell"), f.col("grid")).alias("wkt")
).first()
+-------------------------------------------------------------------------------------------------+
|wkt |
+-------------------------------------------------------------------------------------------------+
|POLYGON ((530000 180000, 530031.25 180000, 530031.25 180031.25, 530000 180031.25, 530000 180000))|
+-------------------------------------------------------------------------------------------------+
custom_centroid
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_centroid(cell, grid) | BINARY |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_centroid(cell, grid) | BINARY |
| Python | gridx.custom.functions.custom_centroid(cell, grid) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The custom-grid cell id. | |
grid | 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> | 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:
BINARYWKB point at the cell center (geometry... (WKB binary))
- SQL
- Python
SELECT gbx_custom_centroid(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700)) AS centroid;
+--------+
|centroid|
+--------+
|[binary]|
+--------+
... (WKB binary — POINT at the center of the 31.25m × 31.25m custom grid cell)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_centroid(f.col("cell"), f.col("grid")).alias("centroid")
).first()
+-----------+
|centroid |
+-----------+
|[binary] |
+-----------+
... (WKB binary — POINT at the center of the 31.25m × 31.25m custom grid cell)
custom_polyfill
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_polyfill(geom, grid, resolution) | array<bigint> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_polyfill(geom, grid, resolution) | array<bigint> |
| Python | gridx.custom.functions.custom_polyfill(geom, grid, resolution) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates. | |
grid | 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> | 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. | |
resolution | INT or STRING | STRING | The 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
- SQL
- Python
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;
+---------------------------------------------+
|cells |
+---------------------------------------------+
|[72057594038644994, ..., (36 cells at res=1)]|
+---------------------------------------------+
... (36 BIGINT cell IDs — 500m cells covering the 3km × 3km BNG polygon at resolution 1)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_polyfill(f.lit(_BNG_POLY_WKT), f.col("grid"), f.lit(1)).alias("cells")
).first()
+---------------------------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_kring(cell, grid, k) | array<bigint> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_kring(cell, grid, k) | array<bigint> |
| Python | gridx.custom.functions.custom_kring(cell, grid, k) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The custom-grid cell id. | |
grid | 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> | 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. | |
k | INT | INT | The 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 distancek(up to(2k+1)^2cells)
- SQL
- Python
SELECT gbx_custom_kring(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 1) AS ring;
+-------------------------------------------+
|ring |
+-------------------------------------------+
|[360287970373976640, ..., (9 cells at k=1)]|
+-------------------------------------------+
... (9 BIGINT cell IDs — the 3×3 neighbourhood including center cell at resolution 5)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_kring(f.col("cell"), f.col("grid"), f.lit(1)).alias("ring")
).first()
+-------------------------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_kloop(cell, grid, k) | array<bigint> |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_kloop(cell, grid, k) | array<bigint> |
| Python | gridx.custom.functions.custom_kloop(cell, grid, k) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cell | BIGINT | BIGINT | The custom-grid cell id. | |
grid | 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> | 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. | |
k | INT | INT | The 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 distancek
- SQL
- Python
SELECT gbx_custom_kloop(360287970373976640, gbx_custom_grid(0, 1000000, 0, 1000000, 2, 1000, 1000, 27700), 1) AS kloop;
+-----------------------+
|kloop |
+-----------------------+
|[..., (8 cells at k=1)]|
+-----------------------+
... (8 BIGINT cell IDs — hollow ring at k=1, center cell excluded)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_kloop(f.col("cell"), f.col("grid"), f.lit(1)).alias("kloop")
).first()
+-----------------------+
|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| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.custom.functions.custom_geomkring(geom, grid, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates. | |
grid | 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> | 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. | |
resolution | INT or STRING | STRING | The grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits. | |
k | INT | INT | The ring distance: 0 is the covering cells alone, 1 adds the cells around them. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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
- SQL
- Python
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;
+--------------------------------------------+
|kring |
+--------------------------------------------+
|[..., (cells within k=1 ring of 4.6 km box)]|
+--------------------------------------------+
... (ARRAY<BIGINT> — polyfill covering set plus one outer ring at resolution 0)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_geomkring(f.lit(_CUSTOM_GEOMK_OFFSET_WKT), f.col("grid"), f.lit(1), f.lit(1)).alias("kring")
).first()
+-------------------------------------------+
|kring |
+-------------------------------------------+
|[72057594038779906, ..., (56 cells at k=1)]|
+-------------------------------------------+
... (56 BIGINT cell IDs — boundary band of the offset 3km polygon at res=1 (500m) expanded by k=1 ring)
custom_geomkloop
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.custom.functions.custom_geomkloop(geom, grid, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates. | |
grid | 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> | 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. | |
resolution | INT or STRING | STRING | The grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits. | |
k | INT | INT | The loop distance: only the cells at exactly this ring distance from the covering cells are returned. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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
- SQL
- Python
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;
+--------------------------------------------+
|kloop |
+--------------------------------------------+
|[..., (outer ring cells, polyfill excluded)]|
+--------------------------------------------+
... (ARRAY<BIGINT> — hollow outer ring at k=1, polyfill cells excluded)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_geomkloop(f.lit(_CUSTOM_GEOMK_OFFSET_WKT), f.col("grid"), f.lit(1), f.lit(1)).alias("kloop")
).first()
+-------------------------------------------+
|kloop |
+-------------------------------------------+
|[72057594038779906, ..., (32 cells at k=1)]|
+-------------------------------------------+
... (32 BIGINT cell IDs — hollow outer band at k=1 around the offset polygon at res=1 (500m))
custom_geomkringexplode
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.custom.functions.custom_geomkringexplode(df, geom, grid, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid BIGINT.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates. | |
grid | 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> | 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. | |
resolution | INT or STRING | STRING | The grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits. | |
k | INT | INT | The ring distance: 0 is the covering cells alone, 1 adds the cells around them. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
Streaming version of custom_geomkring — one row per cell. Call it with FROM t, LATERAL gbx_...(...), at every implementation level.
Returns:
- One row per
BIGINTcell ID in the k-ring
- SQL
- Python
-- 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;
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 ring of the geometry)
from databricks.labs.gbx import manager as gbx_manager
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
grid_col = _get_custom_grid_df(spark).select("grid").first()["grid"]
df = spark.createDataFrame([(grid_col,)], ["grid"])
df.createOrReplaceTempView("_cx_geomk_src")
result = spark.sql(
f"SELECT t.cellid FROM _cx_geomk_src src, "
f"LATERAL gbx_custom_geomkringexplode('{_CUSTOM_GEOMK_OFFSET_WKT}', src.grid, 1, 1, 'boundary-out') t"
).collect()
spark.catalog.dropTempView("_cx_geomk_src")
+------------------+
|cellid |
+------------------+
|72057594038779906 |
|... |
+------------------+
... (one BIGINT row per cell in the k=1 ring around the offset 3km polygon at res=1 (500m))
custom_geomkloopexplode
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.custom.functions.custom_geomkloopexplode(df, geom, grid, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid BIGINT.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in the grid's coordinates. | |
grid | 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> | 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. | |
resolution | INT or STRING | STRING | The grid resolution: 0 is the root cells, each level splits a cell into cell_splits by cell_splits. | |
k | INT | INT | The loop distance: only the cells at exactly this ring distance from the covering cells are returned. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
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
BIGINTcell ID at exactly distance k
- SQL
- Python
-- 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;
+-----------+
|cellid |
+-----------+
|...(BIGINT)|
+-----------+
... (one row per BIGINT cell ID in the k=1 hollow outer ring)
from databricks.labs.gbx import manager as gbx_manager
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
grid_col = _get_custom_grid_df(spark).select("grid").first()["grid"]
df = spark.createDataFrame([(grid_col,)], ["grid"])
df.createOrReplaceTempView("_cx_geomk_src")
result = spark.sql(
f"SELECT t.cellid FROM _cx_geomk_src src, "
f"LATERAL gbx_custom_geomkloopexplode('{_CUSTOM_GEOMK_OFFSET_WKT}', src.grid, 1, 1, 'boundary-out') t"
).collect()
spark.catalog.dropTempView("_cx_geomk_src")
+------------------+
|cellid |
+------------------+
|72057594038779906 |
|... |
+------------------+
... (one BIGINT row per cell in the hollow outer ring at k=1 around the offset 3km polygon at res=1 (500m))
custom_distance
SQLUnity Catalog SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_distance(cellid1, grid, cellid2) | BIGINT |
| SQL (Unity Catalog) | <catalog>.<schema>.gbx_custom_distance(cellid1, grid, cellid2) | BIGINT |
| Python | gridx.custom.functions.custom_distance(cellid1, grid, cellid2) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cellid1 | BIGINT | BIGINT | The first custom-grid cell id. | |
grid | 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> | 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. | |
cellid2 | BIGINT | BIGINT | The 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:
BIGINTChebyshev grid distance in cell steps
- SQL
- Python
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;
+----+
|dist|
+----+
|1 |
+----+
... (Chebyshev grid distance between two cells 1 step apart in X at resolution 0)
from pyspark.sql import functions as f
df = _get_custom_grid_df(spark)
result = df.select(
cx.custom_distance(
cx.custom_pointascell(f.lit("POINT(530000 180000)"), f.col("grid"), f.lit(0)),
f.col("grid"),
cx.custom_pointascell(f.lit("POINT(531000 180000)"), f.col("grid"), f.lit(0)),
).alias("dist")
).first()
+----+
|dist|
+----+
|1 |
+----+
... (Chebyshev grid distance between two cells 1 step apart in X at resolution 0)
custom_cellfill
SQLPythonScala| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_custom_cellfill(cellid, value, grid, [k], [method], [power]) | BINARY |
| Python | gridx.custom.functions.custom_cellfill(cellid, value, grid, k=None, method=None, power=None) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
cellid | BIGINT | The custom-grid cell id. | |
value | DOUBLE | The cell's value; NULL marks a covered cell with no value, which is filled. | |
grid | 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. The same for every row of the group. | |
k | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | 2.0 | The distance exponent of 'idw'; unused by 'mean'. |
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.
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.
- SQL
- Python
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;
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: center cell 216172782113787048 filled to 5.0; neighbours unchanged)
from pyspark.sql import functions as f
df_grid = _get_custom_grid_df(spark)
# Derive valid cell IDs from the fixture grid at resolution 0 (1000m cells)
center_row = df_grid.select(
cx.custom_pointascell(f.lit("POINT(530000 180000)"), f.col("grid"), f.lit(0)).alias("center"),
cx.custom_pointascell(f.lit("POINT(531000 180000)"), f.col("grid"), f.lit(0)).alias("nbr1"),
cx.custom_pointascell(f.lit("POINT(531000 181000)"), f.col("grid"), f.lit(0)).alias("nbr2"),
f.col("grid"),
).first()
center_id = center_row["center"]
nbr1_id = center_row["nbr1"]
nbr2_id = center_row["nbr2"]
df = spark.createDataFrame(
[
(1, center_id, None),
(1, nbr1_id, 5.0),
(1, nbr2_id, 5.0),
],
["region", "cellid", "value"],
)
df = df.crossJoin(df_grid.select("grid"))
result = (
df.groupBy("region")
.agg(
cx.custom_cellfill(
f.col("cellid"),
f.col("value"),
f.col("grid"),
f.lit(1),
f.lit("mean"),
f.lit(2.0),
).alias("filled")
)
.first()
)
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: center cell 216172782113787048 filled to 5.0; neighbours unchanged)
custom_cellfill_array
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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>> |
| Python | gridx.custom.functions.custom_cellfill_array(cells, grid, k=None, method=None, power=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cells | array<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. | |
grid | 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> | 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. | |
k | INT | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | DOUBLE | 2.0 | The 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.
- SQL
- Python
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;
+------+---------------------------------------------------------------------------------+
|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)
from pyspark.sql import functions as f
_register_grids(spark)
df_grid = _get_custom_grid_df(spark)
ids = df_grid.select(
cx.custom_pointascell(f.lit("POINT(530000 180000)"), f.col("grid"), f.lit(0)).alias("center"),
cx.custom_pointascell(f.lit("POINT(531000 180000)"), f.col("grid"), f.lit(0)).alias("nbr1"),
cx.custom_pointascell(f.lit("POINT(531000 181000)"), f.col("grid"), f.lit(0)).alias("nbr2"),
).first()
df = spark.createDataFrame(
[
(1, ids["center"], None),
(1, ids["nbr1"], 5.0),
(1, ids["nbr2"], 5.0),
],
"region INT, cellid BIGINT, value DOUBLE",
).crossJoin(df_grid.select("grid"))
result = (
df.groupBy("region", "grid")
.agg(
cx.custom_cellfill_array(
f.collect_list(f.struct("cellid", "value")), f.col("grid"), 1, "mean", 2.0
).alias("filled")
)
.first()
)
+------+---------------------------------------------+
|region|filled |
+------+---------------------------------------------+
|1 |[{180530, 5.0}, {180531, 5.0}, {181531, 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.
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| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_h3_cellfill(cellid, value, [k], [method], [power]) | BINARY |
| Python | gridx.h3.functions.h3_cellfill(cellid, value, k=None, method=None, power=None) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
cellid | BIGINT | The H3 cell id. | |
value | DOUBLE | The cell's value; NULL marks a covered cell with no value, which is filled. | |
k | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | 2.0 | The 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).
- SQL
- Python
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;
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: center H3 cell 612934495919669247 filled to 5.0; neighbours unchanged)
from databricks.labs.gbx import manager as gbx_manager
from pyspark.sql import functions as f
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
df = spark.createDataFrame(
[
(1, 612934495919669247, None),
(1, 612934495863046143, 5.0),
(1, 612934495900794879, 5.0),
],
["region", "cellid", "value"],
)
result = (
df.groupBy("region")
.agg(
hx.h3_cellfill(
f.col("cellid"), f.col("value"), f.lit(1), f.lit("mean"), f.lit(2.0)
).alias("filled")
)
.first()
)
+------+--------+
|region|filled |
+------+--------+
|1 |[binary]|
+------+--------+
... (BINARY — decoded: H3 center cell 612934495919669247 filled to 5.0; neighbours unchanged)
h3_cellfill_array
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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>> |
| Python | gridx.h3.functions.h3_cellfill_array(cells, k=None, method=None, power=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
cells | array<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. | |
k | INT | INT | 1 | The neighbourhood radius in rings, at least 0; 0 fills nothing. |
method | STRING | STRING | 'mean' | 'mean' (the mean of the valid neighbours) or 'idw' (weighted by ring distance). Any other word raises. |
power | DOUBLE | DOUBLE | 2.0 | The 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.
- SQL
- Python
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;
+------+---------------------------------------------------------------------------------+
|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)
from pyspark.sql import functions as f
_register_grids(spark)
df = spark.createDataFrame(
[
(1, 612934495919669247, None),
(1, 612934495863046143, 5.0),
(1, 612934495900794879, 5.0),
],
"region INT, cellid BIGINT, value DOUBLE",
)
result = (
df.groupBy("region")
.agg(
hx.h3_cellfill_array(
f.collect_list(f.struct("cellid", "value")), 1, "mean", 2.0
).alias("filled")
)
.first()
)
+------+---------------------------------------------------------------------------------+
|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| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.h3.functions.h3_geomkring(geom, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | H3 resolution, a whole number from 0 to 15. | |
k | INT | INT | The ring distance: 0 is the covering cells alone, 1 adds the cells around them. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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
- SQL
- Python
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;
+------------------------------------+
|kring |
+------------------------------------+
|[617733151020810239, ..., (n cells)]|
+------------------------------------+
... (ARRAY<BIGINT> — H3 res-9 covering cells of NYC box expanded by k=1 ring)
from pyspark.sql import functions as f
from shapely import to_wkb
from shapely.geometry import box
geom_wkb = to_wkb(box(-73.99, 40.71, -73.95, 40.75))
df = spark.createDataFrame([(bytearray(geom_wkb),)], ["geom"])
result = df.select(
hx.h3_geomkring(f.col("geom"), 9, 1).alias("kring")
).first()
+------------------------------------+
|kring |
+------------------------------------+
|[617733151020810239, ..., (n cells)]|
+------------------------------------+
... (ARRAY<BIGINT> — H3 res-9 k=1 covering ring of the NYC WKB geometry)
h3_geomkloop
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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> |
| Python | gridx.h3.functions.h3_geomkloop(geom, resolution, k, mode=None, coverage=None) | Column |
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | H3 resolution, a whole number from 0 to 15. | |
k | INT | INT | The loop distance: only the cells at exactly this ring distance from the covering cells are returned. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | '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
- SQL
- Python
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;
+------------------------------------+
|kloop |
+------------------------------------+
|[617733151020810239, ..., (n cells)]|
+------------------------------------+
... (ARRAY<BIGINT> — outer hollow ring at k=1, interior covering cells excluded)
from pyspark.sql import functions as f
from shapely import to_wkb
from shapely.geometry import box
geom_wkb = to_wkb(box(-73.99, 40.71, -73.95, 40.75))
df = spark.createDataFrame([(bytearray(geom_wkb),)], ["geom"])
result = df.select(
hx.h3_geomkloop(f.col("geom"), 9, 1).alias("kloop")
).first()
+------------------------------------+
|kloop |
+------------------------------------+
|[617733151020810239, ..., (n cells)]|
+------------------------------------+
... (ARRAY<BIGINT> — hollow outer ring at k=1, interior covering cells excluded)
h3_geomkringexplode
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.h3.functions.h3_geomkringexplode(df, geom, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid BIGINT.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | H3 resolution, a whole number from 0 to 15. | |
k | INT | INT | The ring distance: 0 is the covering cells alone, 1 adds the cells around them. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
Streaming version of h3_geomkring — one row per cell. Call it with FROM t, LATERAL gbx_h3_geomkringexplode(...).
Returns:
- One row per
BIGINTH3 cell ID
- SQL
- Python
-- 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;
+------------------+
|cellid |
+------------------+
|617733151020810239|
|... |
+------------------+
... (one row per BIGINT H3 cell ID in the k=1 ring around the NYC polygon at res 9)
from databricks.labs.gbx import manager as gbx_manager
from shapely import to_wkb
from shapely.geometry import box
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
geom_wkb = to_wkb(box(-73.99, 40.71, -73.95, 40.75))
df = spark.createDataFrame([(bytearray(geom_wkb),)], ["geom"])
df.createOrReplaceTempView("_h3_geomk_src")
result = spark.sql(
"SELECT t.cellid FROM _h3_geomk_src src, "
"LATERAL gbx_h3_geomkringexplode(src.geom, 9, 1, 'boundary-out') t"
).collect()
spark.catalog.dropTempView("_h3_geomk_src")
+------------------+
|cellid |
+------------------+
|617733151020810239|
|... |
+------------------+
... (one BIGINT row per H3 cell in the k=1 ring around the NYC geometry at res 9)
h3_geomkloopexplode
SQLUnity Catalog SQLPython| Surface | Call | Returns |
|---|---|---|
| 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 |
| Python | gridx.h3.functions.h3_geomkloopexplode(df, geom, resolution, k, mode=None, coverage=None) | DataFrame |
Rows: cellid BIGINT.
| Parameter | SQL type | UC type | Default | Description |
|---|---|---|---|---|
geom | BINARY or STRING | GEOMETRY | The geometry: WKB, EWKB, WKT or EWKT, in EPSG:4326 degrees. | |
resolution | INT or STRING | STRING | H3 resolution, a whole number from 0 to 15. | |
k | INT | INT | The loop distance: only the cells at exactly this ring distance from the covering cells are returned. | |
mode | STRING | STRING | 'boundary-out' | The dilation mode: 'boundary-out', 'boundary-in', 'boundary-in-ignore-holes', 'hole-in', 'hole-out' or 'hole-out-ignore-geom'. |
coverage | STRING | STRING | 'coveras' | How a cell belongs to the geometry: 'coveras' (it overlaps), 'polyfill' (its centre is inside) or 'core' (it is inside). |
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
BIGINTH3 cell ID at exactly distance k
- SQL
- Python
-- 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;
+------------------+
|cellid |
+------------------+
|617733151020810239|
|... |
+------------------+
... (one row per BIGINT H3 cell ID in the k=1 hollow outer ring around the NYC polygon)
from databricks.labs.gbx import manager as gbx_manager
from shapely import to_wkb
from shapely.geometry import box
gbx_manager.register_family(spark, ('gbx_bng_', 'gbx_custom_', 'gbx_quadbin_', 'gbx_h3_'), level="L3")
geom_wkb = to_wkb(box(-73.99, 40.71, -73.95, 40.75))
df = spark.createDataFrame([(bytearray(geom_wkb),)], ["geom"])
df.createOrReplaceTempView("_h3_geomk_src")
result = spark.sql(
"SELECT t.cellid FROM _h3_geomk_src src, "
"LATERAL gbx_h3_geomkloopexplode(src.geom, 9, 1, 'boundary-out') t"
).collect()
spark.catalog.dropTempView("_h3_geomk_src")
+------------------+
|cellid |
+------------------+
|617733151020810239|
|... |
+------------------+
... (one BIGINT row per H3 cell in the outer ring at k=1, interior cells excluded)
H3 Viewshed (Python Helper)
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 carrytower_id_col(BIGINTH3 cell) andobserver_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 atviewshed_res; must carrycell_col(BIGINTH3 cell) andsurface_z_col(metres). A missing or NULL surface is transparent (no blocker at that cell).ground_df— bare-earth (DTM) map atviewshed_res; must carrycell_colandground_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 H3grid_diskbuffer. 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 to1.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 bysurface_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 (repartitionbytower_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;
# 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
- Raster Function Reference
- VectorX Function Reference
- PMTiles Function Reference — Aggregator (
gbx_pmtiles_agg) for publishing tile pyramids