Skip to main content

Vector Reader

Since: vector_gbx v0.4.0

vector_gbx is the catch-all vector reader. It is pure Python (pyogrio-backed), needs no JAR, and reads any OGR-supported format. It produces the (attrs..., geom_0, geom_0_srid, geom_0_srid_proj) schema.

Available Formats​

The reader reads any OGR/GDAL vector driver, including:

  • ESRI Shapefile (.shp), GeoJSON (.geojson, .json), GeoPackage (.gpkg), File Geodatabase (.gdb)
  • KML (.kml), GML (.gml), CSV with geometry (.csv), and more. The gbx_drivers('vector') call below lists the ones in your environment.
Which formats can this runtime read and write?

Call gbx_drivers() in SQL, or drivers() in Python, to list the vector drivers of the current environment. Each row says whether the driver reads, writes and is allowed by GeoBrix. It also says if the driver works as shipped, and what it still needs. The driverName option takes a name from the driver column. The tested column marks the drivers that GeoBrix's driver tests cover, and guidance points to a built-in reader where one exists. The result reflects the environment as it runs now, so adding or removing packages changes it. See the vector drivers in the env wheel for the shipped list.

SELECT driver, description, requires FROM gbx_drivers('vector') WHERE write AND allowed

Options​

The named format readers preset driverName.

OptionDefaultDescription
driverNamerequired on vector_gbx; preset on named readersOGR driver name (e.g. GPKG, ESRI Shapefile, GeoJSON) — forces a specific driver regardless of the file extension. Names match in any case. GDAL picks the driver from the file, and the named driver or an equivalent one is accepted (GeoJSON/GeoJSONSeq/ESRIJSON/TopoJSON/JSONFG, KML/LIBKML, OpenFileGDB/FileGDB, GPKG/SQLite); a file GDAL would open with another driver is read with the named one where GDAL can force it (the GeoJSON family, GPKG, CSV) and fails otherwise, naming the driver GDAL detected (with a .schema(...) you pass yourself, a file with an extension GDAL reads by itself is not checked against the driver). An unknown name fails.
asWKB"true"Output geometry as WKB (binary) vs WKT (text).
chunkSize"10000"Features per unit of work. The layer is streamed in batches of this many features, so per-task memory scales with chunkSize, not layer size.
layerName""Layer name for multi-layer formats (overrides the layer index).
layerNumber"0"Layer index for multi-layer formats (0-based). Ignored when layerName is set.
codedDomains"code"Fields with a coded-value domain return the stored code in the field's declared type ("code", the default) or the domain's description as a string ("description"; a code not listed in the domain returns the code as text). Range domains pass through unchanged.

Basic usage​

# Lightweight generic vector reader (pyogrio; no JAR)
from databricks.labs.gbx import register
register(spark)
df = spark.read.format("vector_gbx").load(SAMPLE) # (attrs..., geom_0, geom_0_srid, geom_0_srid_proj)
df.show()

The reader has Python and SQL bindings.

Typical pipeline: ingest into a table​

The common pattern is to land vector files in a table for downstream analytics — on Databricks a managed table is Delta:

df = (spark.read.format("vector_gbx")
.option("driverName", "GeoJSON") # pass any OGR driver name
.load("/Volumes/main/geo/raw/")) # a folder of files
df.write.mode("overwrite").saveAsTable("main.geo.features") # Delta table on Databricks

Reading a folder fans the files across the cluster, one partition per file, so ingest scales with the data. A single-node pyogrio.read_* parses one file on one machine. See Benchmarking for ingest figures.

Reading from a FILE-column table​

vector_file_read (Python level) accepts either a Volume path/directory or a FILE-column Delta table name. A FILE-column table stores one vector file per row — each FILE reference points to a complete vector file (e.g. a .gpkg). Reading decodes all features from each referenced file via pyogrio.

from databricks.labs.gbx.vectorx.file_read import vector_file_read

# Read all vector files referenced in a FILE-column table
features = vector_file_read(
spark,
"main.geo.vector_files", # fully-qualified table name
driver="GPKG",
as_wkb=True,
)
features.show()

source_type="auto" (the default) distinguishes a table from a path. A string starting with /, matching a URI scheme, or having a known vector file extension is treated as a path. A dotted name, an extension-less string, or a non-existent path is treated as a table. Pass source_type="table" or source_type="path" to bypass the heuristic.

Auto-order default. vector_file_read auto-orders by the resolved source path before decoding. For a vector FILE table, one file maps to one row — every source opens exactly once regardless of row order. Auto-ordering produces a deterministic, cross-format-consistent default row sequence; it is not a throughput lever here (there are no multiple tiles per file to amortize). Pass skip_ordering=True when the table is already physically ordered or when you control ordering downstream:

features = vector_file_read(
spark,
"main.geo.vector_files",
skip_ordering=True, # preserve table/scan order; no additional sort step
)

Durable co-location: CLUSTER BY + OPTIMIZE​

Use layout="cluster" in vector_file_write (or gbx_file_write) to declare CLUSTER BY path in the table DDL. After a bulk insert, run OPTIMIZE to materialize the clustering. Rows for the same source land in the same data files, which makes repeated reads and compaction-stable:

from databricks.labs.gbx.vectorx.file_write import vector_file_write

vector_file_write(
spark,
local_out="/tmp/roads.gpkg",
target="main.geo.vector_files",
layout="cluster", # CLUSTER BY path in the DDL
file_mode="auto",
)
-- After initial write or after bulk inserts, materialize clustering:
OPTIMIZE main.geo.vector_files;

Once the table has been optimized, pass skip_ordering=True when reading — the rows are already co-located, and skipping the sort avoids a redundant step.

layout="order" (the default) applies ORDER BY path at insert time and works well for tables built in a single write. Use layout="cluster" for tables updated incrementally and maintained with periodic OPTIMIZE.

Output Schema​

root
|-- geom_0: binary (geometry in WKB format)
|-- geom_0_srid: integer (spatial reference ID)
|-- geom_0_srid_proj: string (projection definition)
|-- <attribute_1>: <type> (feature attributes...)
|-- <attribute_2>: <type>
|-- ...

Databricks Integration​

The vector readers output geometry in WKB format. To use it with Databricks spatial functions, convert it to the GEOMETRY type. The example uses the Shapefile reader and a sample-data Volumes path. The same pattern applies to every vector reader.

Requires Databricks Runtime

These examples use st_geomfromwkb to convert GeoBrix WKB to Databricks GEOMETRY type.

Convert to GEOMETRY​

# Convert WKB to Databricks GEOMETRY type
df = spark.read.format("shapefile_gbx").load("/Volumes/main/default/geobrix_samples/geobrix-examples/nyc/subway/nyc_subway.shp.zip")
df_with_geom = df.select("*", expr("st_geomfromwkb(geom_0)").alias("geometry"))

SQL Example​

-- Read shapefile and convert to GEOMETRY in SQL
CREATE OR REPLACE TEMP VIEW stations_raw USING shapefile_gbx
OPTIONS (path '/Volumes/main/default/geobrix_samples/geobrix-examples/nyc/subway/nyc_subway.shp.zip');

CREATE OR REPLACE TEMP VIEW stations AS
SELECT *, st_geomfromwkb(geom_0) as geometry
FROM stations_raw;

SELECT name, geometry FROM stations LIMIT 10;

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 reader and which are not, and why.

Common capabilityUsed here?How / why
list_local_files (session-free enumeration)UsedDirectory reads — recursive, include_hidden, extensions, and path_glob_filter are routed through this shared predicate for all format-specific readers.
gbx_file_read / FILE-tier readNot in the DataSourceThe DataSource is session-less on Connect; FILE-tier reads go through vector_file_read (function layer) which injects _file_ref on the driver before mapInPandas. vector_file_read also accepts a FILE-column Delta table name — see Reading from a FILE-column table.
gbx_file_write / FILE-tier writeNot in the DataSourceFILE-tier writes go through vector_file_write (function layer). The DataSource writer commits via FUSE.

Next Steps​

Shared file-access layer

The *_gbx readers use the shared file_gbx file-access base for FILE / FUSE routing and enumeration — capability tiers, the no-gating rule, and layout options are described there.