PMTiles Writer
Since: pmtiles_gbx v0.4.0
Package a tile pyramid ((z, x, y, bytes)) into PMTiles
archives with the pmtiles_gbx writer (pure-Python, Serverless-safe, distributed
spatial sharding; see the benchmark).
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 (z, x, y, bytes):
root
|-- z: int
|-- x: int
|-- y: int
|-- bytes: binary
A tile pyramid: one row per tile, bytes being the already-encoded tile payload (PNG/JPEG/WebP/MVT/…). The writer requires exactly these four columns, in this order — extra, missing, or mis-ordered columns raise an error.
What each column means:
z— zoom level.0is the whole world in one tile; each level up subdivides every tile into four.x,y— the tile's column and row in the XYZ ("slippy-map") grid, addressed from the upper-left (north-west) origin:xincreases eastward,yincreases southward. Put differently,(x, y)identifies the tile by its upper-left corner. This is the Web-Mercator/XYZ convention PMTiles uses — the opposite of the TMS scheme, whereyincreases northward. So(z, x, y) = (0, 0, 0)is the single top-level tile, and at zoom 1(1, 0, 0)is the north-west quadrant.bytes— the already-encoded tile payload; its type (PNG/JPEG/WebP/MVT) is auto-detected from the leading magic bytes.
If your tiles already live in a DataFrame under different names, project them to the exact shape and order:
tiles.select(
tiles["zoom"].alias("z"),
tiles["tile_x"].alias("x"), # column index from the NW origin (east is +)
tiles["tile_y"].alias("y"), # row index from the NW origin (south is +)
tiles["payload"].alias("bytes"),
).write.format("pmtiles_gbx").mode("overwrite").save("/Volumes/cat/sch/vol/out")
Or generate the pyramid straight from rasters with GeoBrix RasterX. gbx_rst_xyzpyramid explodes a raster into one row per intersecting XYZ tile (rendering each as PNG/JPEG/WebP). It already emits a tile: STRUCT<z, x, y, bytes>, exactly the PMTiles schema, so you need no manual aliasing of the tile coordinates:
from pyspark.sql import functions as F
from databricks.labs.gbx.rasterx.functions import rst_xyzpyramid
# One row per (z, x, y) tile across zooms 0..5, as 256px PNG bytes. rst_xyzpyramid
# emits XYZ tiles addressed from the NW origin -- the convention described above.
tiles = (
rasters
.select(F.explode(rst_xyzpyramid("tile", F.lit(0), F.lit(5))).alias("t"))
.selectExpr(
"t.tile.z AS z",
"t.tile.x AS x",
"t.tile.y AS y",
"t.tile.bytes AS bytes",
)
)
tiles.write.format("pmtiles_gbx").mode("overwrite").save("/Volumes/cat/sch/vol/out")
Output: by default, spatially-sharded .pmtiles archives plus a catalog under the target directory; with shardZoom=0, a single .pmtiles archive file.
Options
The writer is write-only and requires .mode("overwrite") — a finalized archive cannot be appended to.
| Option | Default | Description |
|---|---|---|
shardZoom | "6" | Grid zoom that partitions the world into one bounded .pmtiles per parent tile. 0 = a single merged archive. |
targetTilesPerShard | unset | Optional cap on tiles per shard; when set, used to size shards instead of a fixed shardZoom. |
catalog | "stac" | Catalog written over the shards: stac, tilejson, or none. |
tileType | auto-detect | Override the PMTile tile_type: png, jpeg/jpg, webp, avif, or mvt. |
tileCompression | "none" | PMTile tile_compression advertised in the header: none, gzip, brotli, or zstd. Tile bytes pass through unchanged. |
metadata | "{}" | JSON metadata string written into the PMTile header (e.g. '{"name":"my_tileset","attribution":"..."}'). |
fileName | (none) | Name the output unit explicitly. When set, .save(path) treats path as the parent directory (created if missing). In single-archive mode (shardZoom=0): output = path/<fileName>.pmtiles (extension auto-completed). In sharded mode (default): output directory = path/<fileName> (no extension appended). See Output naming. |
# Knobs (sensible defaults):
# shardZoom 6 -> sharded; 0 -> single archive
# targetTilesPerShard adaptive sharding (subdivide dense cells)
# catalog stac (default) | tilejson | none
# tileType auto-sniff (png/jpeg/webp/mvt); override if needed
# tileCompression none (default) | gzip | brotli | zstd
# metadata JSON string -> archive metadata
Output naming
The writer applies a 3-case naming contract. The output unit differs by mode:
- Single-archive (
shardZoom=0) — the unit is a single.pmtilesfile. Extension auto-completion applies:tiles→tiles.pmtiles;tiles.pmtiles→ unchanged. Passing a name ending in a different recognized geo extension raises a clear error. - Sharded (the default,
shardZoom ≥ 1) — the unit is the output directory that holds the shard archives,overview.pmtiles, and the catalog. No.pmtilesextension is appended to the directory name.
Rules evaluated in order:
| Case | .save(path) / fileName | Single-archive resolved output | Sharded resolved output |
|---|---|---|---|
fileName given | .option("fileName","tiles").save("/out/exports") | /out/exports/tiles.pmtiles | /out/exports/tiles/ (directory) |
No fileName; path is an existing directory | .save("/out/exports") | /out/exports/exports.pmtiles | /out/exports/exports/ (directory under the given path) |
No fileName; path is a stem | .save("/out/exports/tiles") | /out/exports/tiles.pmtiles | /out/exports/tiles/ (directory) |
This pure-Python, JAR-free, Serverless-safe writer packages a tile pyramid
((z, x, y, bytes)) into PMTiles archives
using distributed spatial sharding. Each populated parent tile becomes one
bounded, non-overlapping .pmtiles shard, plus a global overview.pmtiles and a
catalog over the shards.
Sharded output (default)
# Lightweight PMTiles writer - distributed spatial sharding (default).
# Input is a tile pyramid: (z, x, y, bytes). shardZoom=6 emits one
# tileset/{z}/{x}/{y}.pmtiles per populated parent + overview.pmtiles + a
# STAC catalog.json.
from databricks.labs.gbx import register
register(spark)
df.write.format("pmtiles_gbx").mode("overwrite").option("shardZoom", "6").save(OUT_DIR)
Output layout:
OUT_DIR/tileset/{z}/{x}/{y}.pmtiles # one per populated parent (Z >= shardZoom)
OUT_DIR/tileset/overview.pmtiles # Z < shardZoom global overview
OUT_DIR/tileset/catalog.json # STAC/GeoJSON manifest
Single archive
# Single-archive PMTiles: shardZoom=0 packs every tile into one .pmtiles file.
from databricks.labs.gbx import register
register(spark)
df.write.format("pmtiles_gbx").mode("overwrite").option("shardZoom", "0").save(OUT_FILE)
Spatial sharding
The writer treats tiled output as immutable, spatially-indexed shards. It partitions
the world by a grid, emits one bounded .pmtiles per parent tile, and delivers a
catalog over the shards rather than one merged file. This keeps shards
independently regenerable and lets a browser fetch only the shard for the area in
view. Set shardZoom=0 for a single merged archive.
Tile-Type Detection
The writer reads the first 12 bytes of the first non-empty tile payload and sets the PMTile header's tile_type byte:
| Magic bytes | tile_type | Meaning |
|---|---|---|
89 50 4E 47 | 2 (PNG) | PNG raster |
FF D8 | 3 (JPEG) | JPEG raster |
RIFF????WEBP | 4 (WebP) | WebP raster |
| anything else | 1 (MVT) | Mapbox Vector Tile (protobuf) |
Override with the tileType option when auto-detection isn't appropriate.
Reading PMTiles
To read PMTiles within GeoBrix, use the pmtiles_gbx reader, which reads tiles from an existing archive or builds a mosaic pyramid from rasters. For browser and inspection use, the client libraries also work:
- pmtiles JS library for MapLibre / browser rendering.
- The Python
pmtilespackage for tile inspection and extraction.
Serving from Object Storage
PMTiles is designed to be served as a single static file with HTTP Range requests. After uploading the output .pmtiles to S3 / ABFS / GCS:
- CORS: enable
GET, HEAD, OPTIONSfor your map host; allowRangeandIf-Matchheaders. - Content-Type: serve as
application/vnd.pmtiles. - Browse: drop the URL into pmtiles.io for a visual sanity check.
- Embed in MapLibre GL JS via the pmtiles protocol — see the PMTiles functions page for a worked HTML snippet.
Next Steps
- PMTiles Function Reference —
gbx_pmtiles_aggUDAF (the in-cell counterpart). - PMTiles Function Reference — Concepts, schema contract, and serving notes.
- Raster Functions — Generate per-tile PNG bytes with
gbx_rst_xyzpyramid. - Helios notebooks — worked end-to-end example: the PMTiles writer packages raster XYZ pyramids (NB02) and terrain hillshade (NB03) over a San Francisco AOI.
- VectorX Function Reference — Generate per-tile MVT bytes with
gbx_st_asmvt_pyramid.
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 capability | Used here? | How / why |
|---|---|---|
list_local_files (session-free enumeration) | Not used by the writer | Writers do not enumerate source files; they write a DataFrame's output partitions. |
gbx_file_write / FILE-tier write | Not in the DataSource | The 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 DataSource | FILE-tier reads for raster data go through gbx_file_read at the function 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.