PMTiles Function Reference
The gbx_pmtiles_agg aggregate runs on every compute. The pmtiles_gbx DataSource writer is a pure-Python writer that also runs on every compute. See the PMTiles Writer page. See Surfaces & Levels for how GeoBrix picks the implementation.
GeoBrix encodes tile pyramids (raster or vector) into the PMTiles v3 single-file archive format. PMTiles replaces the "directory of tiles" pattern with one compact, hash-deduplicated, range-readable file servable directly from cloud object storage. Tile content bytes (PNG / JPEG / WebP / MVT) pass through verbatim — PMTiles is container-only.
databricks.labs.gbx.pmtiles (Python). PMTiles is a peer of RasterX / VectorX / GridX, not a dependency.
Two entry points
Pick based on pyramid size:
| Entry point | When to use | Limit |
|---|---|---|
gbx_pmtiles_agg UDAF (this page) | The full pyramid fits in a single Spark cell. Returns a BINARY column. Convenient for one-shot bundle generation. | ~100 MiB of tile payload by default; hard ceiling at the 2 GiB Spark cell limit. |
PMTiles Writer (.write.format("pmtiles_gbx")) | Larger pyramids; per-partition writes with spatial sharding, no in-memory consolidation. | Bound only by available disk. |
Registration
The Python entry (px.pmtiles_agg) registers gbx_pmtiles_agg in the session on its first call. To call it from SQL first, register it once per session:
from databricks.labs.gbx import register
register(spark, only=["gbx_pmtiles_agg"])
register(spark) also installs the pmtiles_gbx writer, which is a Python DataSource.
register(spark) installs gbx_pmtiles_agg with every other function. To install only the aggregate, call the standalone helper:
from databricks.labs.gbx.pmtiles import register_pmtiles_agg
register_pmtiles_agg(spark) # installs gbx_pmtiles_agg (Serverless-safe)
Quick start
UDAF: aggregate to a single blob
from pyspark.sql import functions as f
from databricks.labs.gbx.pmtiles import functions as px
# tiles_df: (z: int, x: int, y: int, bytes: binary)
pmt = (
tiles_df.agg(
px.pmtiles_agg(
f.col("bytes"), f.col("z"), f.col("x"), f.col("y"),
'{"name":"my_tileset","attribution":"contoso"}',
).alias("pmt")
)
.collect()[0]["pmt"]
)
with open("/tmp/out.pmtiles", "wb") as fh:
fh.write(pmt)
SELECT gbx_pmtiles_agg(bytes, z, x, y, '{"name":"my_tileset"}') AS pmt
FROM tiles_z2;
DataSource: write a pyramid with pmtiles_gbx
from databricks.labs.gbx import register
register(spark)
(
tiles_df
.write
.format("pmtiles_gbx")
.option("shardZoom", "0") # 0 = one merged archive
.option("metadata", '{"name":"my_tileset"}')
.mode("overwrite")
.save("/tmp/out.pmtiles")
)
With shardZoom=0 the output path is the final file. See the PMTiles Writer page for sharded output and the other options.
Always pass .mode("overwrite"). The default ErrorIfExists is not supported — the failure is loud and points you at .mode("overwrite").
Schema contract
The DataSource writer expects this write schema:
z INT — tile zoom level (0..31)
x INT — tile x within the zoom
y INT — tile y within the zoom
bytes BINARY — tile payload (PNG / JPEG / WebP / MVT)
The UDAF is more relaxed: z/x/y accept either INT or LONG (PySpark's createDataFrame infers Python ints as LongType by default, which the UDAF coerces in update).
Tile-type detection
The encoder reads the first 12 bytes of the first non-empty tile payload and sets the PMTiles 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 auto-detection via .option("tileType", "<byte>") (e.g. "2" for PNG when emitting tiles via a custom encoder that doesn't carry standard magic bytes).
Tile compression
GeoBrix passes tile bytes through unchanged. If your tiles are already compressed (e.g. gzipped MVTs), set .option("tileCompression", "<byte>") so the PMTiles header advertises the correct compression to downstream readers:
| Byte | Compression (spec § 3.3) |
|---|---|
1 | None (default) |
2 | gzip |
3 | brotli |
4 | zstd |
The internal compression (root directory + metadata) is always none in v0.4.0; the spec's compressed-root-directory variant ships in a future release.
Examples below use SQL. PMTiles functions are prefixed with gbx_ (e.g. gbx_pmtiles_agg). For language-specific usage, see Language Bindings.
pmtiles_agg
SQLPython| Surface | Call | Returns |
|---|---|---|
| SQL (session) | gbx_pmtiles_agg(bytes, z, x, y, [metadata_json]) | BINARY |
| Python | pmtiles.functions.pmtiles_agg(bytes, z, x, y, metadata_json=None) | Column |
| Parameter | SQL type | Default | Description |
|---|---|---|---|
bytes | BINARY | The tile payload, passed through as it is (PNG, JPEG, WebP or MVT). A NULL payload is skipped. | |
z | INT | The zoom level of the tile. | |
x | INT | The tile column. | |
y | INT | The tile row (north-down slippy-map order). | |
metadata_json | STRING | '{}' | The archive's metadata as JSON text; NULL, blank or '{}' means none. |
Powered by the pmtiles package. Grouped aggregate: groupBy(...).agg(px.pmtiles_agg("bytes", "z", "x", "y")) folds a group's (bytes, z, x, y) map tiles into one PMTiles v3 archive (BINARY). It accepts raster tiles (PNG / JPEG / WebP) or vector tiles (MVT).
Aggregate a per-tile (z, x, y, bytes) row set into a single PMTile v3 archive blob.
- SQL
- Python
-- Build a 9-tile PMTile pyramid from an existing `tiles_z2(z, x, y, bytes)` table.
-- The result column `pmt` is a BINARY blob containing the full PMTile v3 archive.
SELECT gbx_pmtiles_agg(bytes, z, x, y, '{"name":"my_tileset"}') AS pmt
FROM tiles_z2;
+-----------------------------------------------------+
|pmt |
+-----------------------------------------------------+
|[50 4D 54 69 6C 65 73 03 7F 00 00 00 00 00 00 00 ...]|
+-----------------------------------------------------+
(BINARY: PMTiles v3 archive. Starts with the magic bytes "PMTiles" and version byte 3; holds the 9 tiles at zoom 2; cut here)
from databricks.labs.gbx.pmtiles import register_pmtiles_agg
register_pmtiles_agg(spark)
test_data = [
(2, x, y, f"tile_{x}_{y}".encode("utf-8"))
for x in range(3)
for y in range(3)
]
df = spark.createDataFrame(test_data, ["z", "x", "y", "tile_bytes"])
result = (
df.agg(
px.pmtiles_agg(
"tile_bytes",
"z",
"x",
"y",
'{"name":"my_tileset"}',
).alias("pmt")
)
.first()
)
+------------------------------------------+
|pmt |
+------------------------------------------+
|[50 4D 54 69 6C 65 73 03 ...] |
+------------------------------------------+
(BINARY: PMTiles v3 archive — starts with magic bytes b'PMTiles' + version byte 3; contains 9 synthetic tiles at zoom 2)
The 4-argument form omits the metadata JSON (defaults to '{}'):
-- 4-arg form: metadata defaults to '{}'. Result is still a valid PMTile v3 blob.
SELECT gbx_pmtiles_agg(bytes, z, x, y) AS pmt
FROM tiles_z2;
Duplicate tile coordinates
When multiple rows share the same tile coordinates (z, x, y):
- Vector (MVT) tiles — features from all matching rows are combined into one multi-feature tile. Features from different layers are kept in their respective layers; attributes are preserved per feature.
- Raster tiles (PNG, JPEG, WebP) — the first non-null tile is used; subsequent tiles for the same coordinates are ignored (raster images cannot be meaningfully combined).
Tile type is detected automatically from the content of the first non-null payload (see Tile-type detection above).
Typical pipelines
- Raster pyramid:
gbx_rst_xyzpyramid(tile, minZoom, maxZoom)produces per-tile rows of PNG bytes — pipe straight intogbx_pmtiles_agg. - Vector pyramid:
gbx_st_asmvt_pyramid(geom, attrs, minZoom, maxZoom, layer)produces per-tile MVT bytes — pipe straight intogbx_pmtiles_agg. Becausegbx_st_asmvt_pyramidemits one row per feature, the aggregate merges all features that share a tile coordinate into a single multi-feature tile.
For pyramids that exceed the Spark cell ceiling, use the PMTiles Writer instead.
See the Helios notebooks for a worked end-to-end example. There gbx_pmtiles_agg packages vector MVT tiles (NB01), a raster XYZ pyramid (NB02), and a hillshade pyramid (NB03) into separate PMTiles archives over a San Francisco AOI.
Serving from object storage
PMTiles is designed to be served as a single static file via 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 (pin to a specific version and add
integrity/crossoriginSRI attributes for production use):<script src="https://unpkg.com/pmtiles@3/dist/pmtiles.js"></script><script>const protocol = new pmtiles.Protocol();maplibregl.addProtocol("pmtiles", protocol.tile);const map = new maplibregl.Map({container: "map",style: {version: 8,sources: { my: { type: "vector", url: "pmtiles://https://my-bucket/out.pmtiles" } },layers: [/* ... */]}});</script>
Limits in v0.4.0
- No leaf directories. If the global root directory would exceed 16,257 bytes (spec § 4), the encoder errors out and asks you to split your input. In practice this only happens with very large pyramids (tens of millions of tiles); the limit will be relaxed in a future release.
- Read within GeoBrix via the
pmtiles_gbxreader; the JS / Python pmtiles client libraries also work for external read access. - No cross-task dedup in the DataSource. Identical tiles across partitions are stored multiple times in the final file. The UDAF path does per-blob SHA-256 dedup, so for known-redundant pyramids prefer the UDAF if your data fits.
References
- PMTiles v3 specification
- pmtiles.io online viewer
- MapLibre GL JS
- Felt — open or import a PMTile by URL
Next Steps
- PMTiles Writer — DataSource for streaming large pyramids to disk.
- Raster Functions — Generate tile bytes with
gbx_rst_xyzpyramid. - Helios notebooks — worked end-to-end example using
gbx_pmtiles_aggfor all three data modalities (vector MVT, raster XYZ, hillshade). - VectorX Function Reference — Generate MVT tiles with
gbx_st_asmvt_pyramid.