Skip to main content

Rasterio Distributed

RasterX at the Python level is, in large part, rasterio plus a best-of-breed Python raster stack: rio-tiler, scipy, scikit-image, shapely, pyproj, h3 and quadbin. They run as Arrow-vectorized Spark UDFs and UDTFs across the cluster. It is not a single-node rasterio.open loop, and it is not a reimplementation. The same rasterio functions you would call locally run inside a vectorized Spark UDF, once per tile, on every executor in parallel. No JAR and no native GDAL install separate from rasterio's bundled GDAL.

If you already know rasterio, you already understand what pyrx does. The coverage matrix below maps the surface, and the side-by-sides show each operation in rasterio next to its distributed pyrx form.


Register​

Register the *_gbx data sources and SQL functions once per session before using any rst_* function or raster_gbx reader:

Register the functions and the *_gbx data sources
# Register the GeoBrix lightweight (pyrx) functions once per session
from databricks.labs.gbx import register
register(spark) # gbx_* SQL functions and the raster_gbx family of data sources

Capability coverage​

The table below maps each RasterX capability to the rasterx function(s) whose Python level uses it, and the backing Python library. Every rst_* function runs at the Python level, on every compute.

See the RasterX Function Reference for runnable per-function code examples.

Capabilitypyrx function(s)Backing library
I/O & metadatarst_fromcontent, rst_fromfile, rst_width, rst_height, rst_srid, rst_numbands, rst_metadata, rst_subdatasetsrasterio
Raster summaryrst_summaryGDAL raster info in the worker
Warp / reprojectrst_transform, rst_to_webmercatorGDAL raster reproject in the worker
Clip / maskrst_cliprasterio + shapely
Resamplerst_resample, rst_resample_to_size, rst_resample_to_resGDAL raster reproject in the worker
Mergerst_merge, rst_merge_aggrasterio + NumPy
COG conversionrst_cog_convertrasterio (GDAL's COG driver)
Spectral indicesrst_ndvi, rst_evi, rst_savi, rst_ndwi, rst_nbr, rst_index, rst_thresholdGDAL raster calc
Band mathrst_derivedband, rst_derivedband_agg, rst_combineavg, rst_combineavg_aggNumPy
Terrain (Horn 3×3)rst_slope, rst_aspect, rst_hillshade, rst_tri, rst_tpi, rst_roughnessGDAL raster algorithms in the worker
Fill NoDatarst_fillnodataGDAL raster fill-nodata in the worker
Color reliefrst_color_reliefGDAL raster color-map in the worker
Rasterizerst_rasterize, rst_h3_rasterize_aggrasterio + shapely
Polygonizerst_polygonizeGDAL raster polygonize in the worker
Focal / convolutionrst_filter, rst_convolvescipy.ndimage
Proximityrst_proximityscipy
Contourrst_contourGDAL raster contour in the worker
Viewshedrst_viewshedGDAL raster viewshed in the worker
XYZ tilesrst_tilexyz, rst_xyzpyramidrio-tiler + morecantile
Tiling / retilerst_maketiles, rst_retilerasterio + NumPy
Grid aggregation (H3)rst_h3_tessellate, rst_h3_rastertogrid*h3
Grid aggregation (quadbin)rst_quadbin_tessellate, rst_quadbin_rastertogrid*, rst_quadbin_rasterize_aggquadbin
Grid aggregation (BNG)rst_bng_tessellate, rst_bng_rastertogrid*, rst_bng_rasterize_aggpure-Python BNG codec + rasterio (27700 warp)

Side-by-sides​

Reproject (warp)​

rasterio computes the target grid for a single file:

Single-node rasterio: reproject one file to EPSG:3857
# Single-node rasterio: reproject one file to EPSG:3857
import rasterio
from rasterio.warp import calculate_default_transform, reproject, Resampling
with rasterio.open("in.tif") as src:
t, w, h = calculate_default_transform(
src.crs, "EPSG:3857", src.width, src.height, *src.bounds)
# ...write a reprojected file, one machine, one file at a time

pyrx runs GDAL's raster reproject on each tile across the cluster:

GeoBrix pyrx: reproject a DataFrame of tiles, distributed
# GeoBrix: reproject a whole DataFrame of tiles, distributed
from databricks.labs.gbx.rasterx import functions as rx
df2 = df.withColumn("tile", rx.rst_transform("tile", 3857))
# rst_transform runs GDAL's raster reproject on each tile in an Arrow UDF across the cluster

The test verifies two things. The pyrx-reprojected tile reports the correct target CRS (EPSG:3857). Its geographic bounds agree, within 1%, with the bounds calculate_default_transform computes on the rasterio side. Pixel-level equality between the two outputs was not asserted. The rasterio warp and the pyrx distributed warp agree on the reprojected grid geometry, not on every pixel value.


Clip​

rasterio clips a single file to a geometry:

Single-node rasterio: clip one raster to a geometry
# Single-node rasterio: clip one raster to a geometry
import rasterio
from rasterio.mask import mask as rio_mask
with rasterio.open("in.tif") as src:
out, out_transform = rio_mask(src, [geom], crop=True)

pyrx applies the same clip operation to every tile in a DataFrame:

GeoBrix pyrx: clip every tile to a geometry, distributed
# GeoBrix: clip every tile to a geometry, distributed
from databricks.labs.gbx.rasterx import functions as rx
from pyspark.sql import functions as f
# clip_geom column holds WKB/WKT geometry; third arg controls border pixel inclusion
df2 = df.withColumn("tile", rx.rst_clip("tile", "clip_geom", f.lit(False)))

Both operations mask the raster to the provided geometry. The third argument to rst_clip controls border pixel inclusion (True = include any pixel that touches the geometry boundary; False = include only pixels fully inside). This corresponds to rasterio.mask's all_touched parameter. Both are shown here as the same distributed clip operation, not as pixel-level equivalents.


NDVI (band math)​

rasterio + NumPy computes NDVI for a single file:

Single-node rasterio + NumPy: NDVI (band1=red, band2=nir)
# Single-node rasterio + NumPy: NDVI for one raster (band1=red, band2=nir)
import rasterio, numpy as np
with rasterio.open("in.tif") as src:
red = src.read(1).astype("float32")
nir = src.read(2).astype("float32")
ndvi = (nir - red) / (nir + red)

pyrx computes the same NDVI across a DataFrame of tiles:

GeoBrix pyrx: NDVI across a DataFrame of tiles, distributed
# GeoBrix: NDVI across a DataFrame of tiles, distributed
from databricks.labs.gbx.rasterx import functions as rx
# rst_ndvi(tile, red_idx, nir_idx) — band indices are 1-based
df2 = df.withColumn("tile", rx.rst_ndvi("tile", 1, 2))

The test asserts np.allclose between the rasterio-computed NDVI array and the pyrx-computed result. The rasterio side is NumPy arithmetic on band arrays. The pyrx side runs GDAL's raster calc. The two agree to within floating-point tolerance.


Gaps and divergences​

Distributed in pyrx today​

Everything in the matrix above. Every rst_* function runs at the Python level. A dedicated cluster with the JAR adds the JVM level where it is faster.

JVM-level differences​

None. Every function and mode runs at the Python level, the conforming TIN mode included.

See Surfaces & Levels for how GeoBrix picks an implementation.

See Benchmarking and Performance for per-function timing data across both implementations.

With the plain GeoBrix wheel, rasterio brings the GDAL bundled in PyPI's rasterio wheels, which has a narrower driver set than GeoBrix's own GDAL build. The GeoBrix env wheels (environment 5 and 6) give rasterio that build instead, the one the JVM loads from the same wheel.


How it is distributed​

The Python level distributes rasterio and NumPy compute over Spark via three mechanisms, none of which require the Spark JVM internals:

  • Arrow scalar UDFs (per-tile functions): each scalar rst_* function invoked via the Python Column API is a @pandas_udf (tile_scalar_udf / tile_scalar_udf2 in pyrx/_udf.py). It receives a batch of tiles' raster bytes as a Pandas Series, calls rasterio/NumPy once per tile within the batch, and returns transformed bytes. The Arrow batch crosses the JVM↔Python boundary once per batch, not once per row. The same functions are also registered as plain @f.udf for SQL use (so DESCRIBE FUNCTION and SQL calls work). The Python Column API always routes through the pandas_udf path.
  • Grouped-aggregate pandas UDFs (merges and combineavg): rst_merge_agg, rst_combineavg_agg, and similar grouped aggregators use @pandas_udf(BinaryType()) — Spark 4 auto-detects the grouped-aggregate eval type. Each receives all tiles in a group as a Pandas Series and merges them in one Python call.
  • Streaming UDTFs (fan-out): rst_h3_tessellate, rst_quadbin_tessellate, rst_bng_tessellate, rst_maketiles, rst_retile, rst_xyzpyramid use @udtf to yield one output row per chip or tile without materializing the full output in memory first.

No _jvm access, no .rdd, no Spark driver-side collect before distributing. Data-source readers and writers (raster_gbx, gtiff_gbx, etc.) use the DataSource V2 API. See Performance for partitioning guidance and per-tier timing benchmarks.