Skip to main content

Raster Writer

Since: raster_gbx v0.4.0

Write raster tiles from the shared (source, tile) schema with the raster_gbx writer (rasterio-backed, JAR-free, supports overwrite).

No JAR needed

The *_gbx writers need no JAR and run on every compute, Serverless and standard (shared) clusters included. See the Benchmarking page for timings and methodology.

Schema​

Input schema — exactly (source, tile):

root
|-- source: string
|-- tile: struct
| |-- cellid: bigint
| |-- raster: binary
| |-- metadata: map<string,string>

This is the exact schema the raster readers emit. The writer requires these two columns and nothing else — extra OR missing columns both raise an error (there is no implicit projection). The on-disk format comes from tile.metadata (the GDAL driver/extension recorded at read time); the ext option controls only the filename suffix.

Typically you write a reader's output unchanged. To control output filenames, use the nameCol option — it overwrites the source value in place; do not add a column (that breaks the exact-schema check):

# (source, tile) straight from a reader -> write as-is
df.write.format("gtiff_gbx").mode("append").save("/Volumes/cat/sch/vol/out")

# name outputs from an existing column (overwrite `source`, don't add a column):
df.withColumn("source", df["scene_id"]) \
.write.format("gtiff_gbx").mode("append").save("/Volumes/cat/sch/vol/out")

Output: one raster file per input row, written under the target directory; the file format/extension is whatever tile.metadata records (e.g. GeoTIFF for gtiff_gbx).

Options​

The writer requires the exact (source, tile) schema and takes two writer options. The on-disk driver and block layout come from tile.metadata, not from writer options. The writer also takes compress, compressLevel and predictor to choose the GeoTIFF codec (see Output format & compression).

OptionDefaultDescription
ext"tif"Filename suffix appended to each written file. Does not change the on-disk format; that is driven by tile.metadata["driver"].
nameColunsetName of an existing string column whose value becomes the output filename (without extension). Must be one of the two table columns — in practice, overwrite source (see Control filenames). When unset, an opaque unique name is used.

Modes and codecs​

Supports .mode("overwrite"). For GeoTIFF output the compress option decides what happens to each tile. By default (auto) a ZSTD tile is written as it is and a tile in any other codec is re-encoded to ZSTD. An explicit codec re-encodes every tile. Tiles whose driver is not GTiff (e.g. COG) are re-encoded via rasterio.

Write raster tiles​

# Catch-all lightweight writer (output driver from tile.metadata; default GTiff)
from databricks.labs.gbx import register
register(spark)
df = spark.read.format("raster_gbx").load("{SAMPLE_RASTER_PATH}")
df.write.format("raster_gbx").mode("overwrite").save(OUT_DIR)

With the default compress="auto", a GeoTIFF tile that is already ZSTD-compressed is written as it is. A GeoTIFF tile in any other codec (DEFLATE, LZW, uncompressed) is re-encoded to ZSTD. A tile whose metadata["driver"] is non-GTiff (e.g. COG) is re-encoded via rasterio.

For an explicit GeoTIFF-forced writer, see the GeoTIFF Writer.

Register before writing

Like the Python readers, the writers are not auto-registered (Python Data Source V2 has no classpath auto-discovery). Call register(spark) first, as shown above. See Raster Readers → Register for the full explanation.

Control filenames (nameCol)​

# Control output filenames: overwrite 'source', set nameCol
from pyspark.sql.functions import concat, lit, monotonically_increasing_id
(df.withColumn("source", concat(lit("tile_"), monotonically_increasing_id()))
.write.format("gtiff_gbx").mode("overwrite")
.option("nameCol", "source").option("ext", "tif").save(OUT_DIR))

Output format & compression​

# GeoTIFF output: the writer options choose the codec
# compress -> auto (default) | zstd | deflate | lzw | none
# compressLevel, predictor -> refine an explicit codec (ignored under auto)
# Other drivers: the output driver and its encoding come from tile.metadata
# driver/format -> output driver (default GTiff)
# compression/blocksize/zlevel/zstd_level -> applied when encoding a non-GTiff driver (COG)
# Change the metadata via upstream transforms, then write.
OptionDefaultDescription
nameColunsetExisting string column whose value is the output filename (overwrite source). When unset, an opaque unique name is used.
ext"tif"Filename suffix. Does not change the on-disk format.
compress"auto"GeoTIFF codec: auto (ZSTD with a dtype-matched predictor and a size-adaptive level), zstd, deflate, lzw or none.
compressLevelcodec defaultLevel for an explicit zstd or deflate. Ignored under auto.
predictordtype-matchedTIFF predictor (1-3) for an explicit codec. Ignored under auto.

The output driver is read from tile.metadata. For GeoTIFF output, compress governs the codec. Under auto a ZSTD tile is written as it is and any other GeoTIFF tile is re-encoded to ZSTD. An explicit compress re-encodes every tile with that codec (decoded pixels are identical either way). The compression / blocksize / zlevel / zstd_level metadata keys (compression defaults to DEFLATE when the metadata names none) apply when a tile is encoded to another driver such as COG. See Materialized Compression for the codec, level, and dtype-matched predictor details.

# Choose the GeoTIFF codec at write time: DEFLATE level 9 instead of the default ZSTD
(df.write.format("gtiff_gbx").mode("overwrite")
.option("compress", "deflate")
.option("compressLevel", "9")
.save(OUT_DIR))

Tile Payloads vs Local Paths​

After a write round-trip, raster expressions should consume the binary payload inside the tile struct, not any /tmp/... paths that may appear in tile metadata. Those local paths refer to transient staging files on whichever executor produced the tile and will not exist elsewhere. GeoBrix expressions (rst_*) use the binary payload by default; hand-rolled UDFs should too.

Viewing Output​

Written rasters are standard GDAL-compatible files:

  • QGIS / ArcGIS / any GDAL-aware viewer opens them directly.
  • gdalinfo <file> (via the GDAL CLI) prints metadata, geotransform, and band statistics.
  • Read them back through GeoBrix with spark.read.format("raster_gbx").load(OUT_DIR).
Splitting source files into a tile grid?

This writer takes an already-loaded tile DataFrame ((source, tile) schema). Use cog_gbx instead if you have source files on disk to split into a grid of mini-COGs. The grid can be native pixel tiling or quadbin cell-aligned. The writer reads raw files, tiles them, and optionally emits a mosaic.vrt index. See the COG Writer and VRT & Mosaics.

Common functions: used vs excluded​

See GBX Common Functions for the full catalog of shared file-access primitives. The table below shows which are active in this writer and which are not, and why.

Common capabilityUsed here?How / why
list_local_files (session-free enumeration)Not used by the writerWriters do not enumerate source files; they write a DataFrame's output partitions.
gbx_file_write / FILE-tier writeNot in the DataSourceThe DataSource writer (df.write.format(...)) is session-less on Connect and commits via FUSE. FILE-tier writes use gbx_file_write at the function layer instead.
gbx_file_read (FILE-tier read)Not in the DataSourceFILE-tier reads for raster data go through gbx_file_read at the function layer.
Shared file-access layer

The *_gbx writers commit via the shared file_gbx file-access base — file_mode, layout, and the no-gating rule are described there. See also file_gbx Writer for the write API.

Next Steps​

  • Raster Readers — The corresponding read path.
  • Raster Functions — Transforms to run before the write.
  • COG Writer — Prepare source files into COGs; mosaic mode for large sources, native or quadbin tile grid.