GeoPackage Writer
Since: gpkg_gbx v0.4.0
gpkg_gbx — pyogrio-backed, pure-Python DataSource V2 writer with the OGR
driver preset to GPKG. It round-trips with the matching gpkg_gbx reader;
the schema (attrs..., geom_0, geom_0_srid, geom_0_srid_proj) is shared across all
*_gbx vector readers and writers.
- Large datasets: GeoPackage has no 2 GB cap and is the recommended single-file option
for large data. However, it still assembles on the driver (single-node write). For the very
largest outputs, use a classic cluster (high-memory Serverless does not help here) or
switch to the fully distributed
geojsonl_gbxwriter. See Choosing a writer for large datasets. - Compute: the
*_gbxwriters need no JAR and run on every compute, Serverless and standard (shared) clusters included. See Benchmarking for timings and methodology.
- Register first — call
register(spark)once before using any*_gbxformat (see the Writers Overview). - Single file — this writer assembles one output file; for sharded output, use the
GeoJSONL writer (
geojsonl_gbx). - Input geometry — provide geometry as WKB (binary) or WKT (text), with the CRS in
the companion
*_srid/*_srid_projcolumns. EWKB, EWKT, and GeoJSON-encoded geometry are not accepted. Writing from a DatabricksGEOMETRY/GEOGRAPHYcolumn? Export to WKB first withST_AsBinary(orST_AsTextfor WKT) — see the Schema section below — and avoidST_GeomAsEWKB/ST_AsEWKT/ST_AsGeoJSON.
Options
| Option | Default | Behavior |
|---|---|---|
driverName | GPKG (preset) | OGR driver. Preset by this named writer; override only if needed. |
mode | overwrite | overwrite only; append is rejected. |
fileName | (none) | Name the output unit explicitly. When set, .save(path) treats path as the parent directory (created if missing) and writes path/<fileName>.gpkg (extension auto-completed). See Output naming. |
geometryType | inferred from the data | Override the OGR geometry type. |
layerName | driver default | Output layer name where supported. |
geomCol | auto-detected from the *_srid column | Override the geometry column name. Locates the geometry and its SRID companion (<geomCol>_srid). See Named Vector Formats. |
sridCol | <geomCol>_srid | Override the SRID column name. Required — supplies the CRS authority code (e.g. "4326", or "0" if unknown). |
projCol | <geomCol>_srid_proj | Override the PROJ4 column name (optional fallback CRS when sridCol is "0"). |
Schema
gpkg_gbx uses the shared vector column contract — a geometry column plus
its *_srid (required) / *_srid_proj (optional) CRS companions; every other column becomes
an attribute. Point the writer at your columns with geomCol / sridCol / projCol, or
coerce to the geom_0 convention.
(df # geometry + CRS under your own column names
.write.format("gpkg_gbx")
.option("geomCol", "the_geom") # WKB or WKT
.option("sridCol", "epsg") # REQUIRED — CRS authority code
.option("projCol", "crs_proj") # optional — PROJ4 fallback
.mode("overwrite").save("/Volumes/cat/sch/vol/districts.gpkg"))
For coercing an arbitrary frame to this shape and for exporting Databricks GEOMETRY /
GEOGRAPHY columns, see the Vector Writer schema.
Output: a GeoPackage .gpkg (SQLite) file. The geometry is stored in a named geometry
column (default geom). Attribute names/types and the layer are preserved with no 2 GB cap
(set the layer name with layerName).
How it scales
Unlike a single-node pyogrio.write_* call that serializes one file on one machine, the
gpkg_gbx writer runs as a Spark DataSource V2 two-phase write. Each executor
writes its partition concurrently to a scratch fragment. Then the driver merges the
fragments into one output file. The merge is sequential and rename-free, so it is safe on
FUSE-mounted cloud storage (Unity Catalog Volumes, DBFS). Repartition the input to control
write parallelism.
Output naming
gpkg_gbx applies the standard single-file output naming contract. The canonical extension is .gpkg. Rules evaluated in order:
| Case | .save(path) / fileName | Resolved output |
|---|---|---|
fileName given | .option("fileName","districts").save("/out/exports") | /out/exports/districts.gpkg |
No fileName; path is an existing directory | .save("/out/exports") | /out/exports/exports.gpkg |
No fileName; path is a stem | .save("/out/exports/districts") | /out/exports/districts.gpkg |
Extension auto-completion: districts → districts.gpkg; districts.gpkg → unchanged. Passing a name ending in a different recognized geo extension (e.g. .geojson) raises a clear error.
Example
# Lightweight GeoPackage writer (pyogrio; OGR driver preset to "GPKG")
from databricks.labs.gbx import register
register(spark)
src = f"{SAMPLE_DATA_BASE}/nyc/geopackage/nyc_complete.gpkg"
df = spark.read.format("gpkg_gbx").load(src)
out = "/tmp/nyc_complete.gpkg" # a single named output file
df.write.format("gpkg_gbx").mode("overwrite").save(out)
back = spark.read.format("gpkg_gbx").load(out)
assert back.count() == df.count()
Typical pipeline: export a table to a single file
Export a table of vector data to a GeoPackage file — no coalesce needed (the writer merges partitions into a single file on commit):
df = spark.table("main.geo.districts") # vector data in a (Delta) table
df.write.format("gpkg_gbx").mode("overwrite").save("/Volumes/main/geo/exports/districts.gpkg")
Each partition is written concurrently, then merged into one output file. See Benchmarking for export timings.
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("gpkg_gbx")) is session-less on Connect and commits via FUSE. FILE-tier writes use vector_file_write at the function layer instead. |
gbx_file_read (FILE-tier read) | Not in the DataSource | FILE-tier reads for vector data go through vector_file_read at the function layer. |
The writer reads the CRS from the geom_0_srid column (or sridCol) and registers it in the GPKG gpkg_spatial_ref_sys table via srs.SetFromUserInput. An EPSG, ESRI, or OGC authority string is accepted; if srid is "0", the geom_0_srid_proj PROJ4 fallback is used. See Coordinate reference systems for the full CRS model.
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.