Skip to main content

Shapefile Reader

Since: shapefile_gbx v0.4.0

shapefile_gbx reads ESRI Shapefiles, zipped or not. It is pure Python (pyogrio-backed) and needs no JAR. It produces the (attrs..., geom_0, geom_0_srid, geom_0_srid_proj) schema.

What you can point .load() at​

The table below covers all three accepted forms.

Path passed to .load()What happens
/path/to/x.shpReads that single shapefile. Side-car files (.shx, .dbf, .prj, .cpg) are picked up automatically from the same directory.
/path/to/dir/Reads all shapefiles found under dir recursively and unions the results into one DataFrame. The directory may contain unzipped .shp bundles, zipped .shp.zip archives, or a mix — all are enumerated and unioned. All shapefiles must share the same schema; if their schemas diverge, the reader raises an error — load them separately or point at a single-stem directory.
/path/to/x.shp.zipReads a zipped shapefile archive (the .zip must contain the .shp and its side-cars at the top level).
Schema divergence

When loading a directory that contains shapefiles with different column layouts, the reader raises:

shapefile reader: shapefiles under <path> have differing schemas;
load them separately or use a single-stem directory.
Stems: <a>, <b>.

Either pass a path to a specific .shp file, or ensure all shapefiles in the directory share the same schema before loading them together.

Options​

shapefile_gbx presets the ESRI Shapefile driver.

OptionDefaultDescription
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.
layerNumber"0"Layer index for multi-layer formats (0-based). Ignored when layerName is set.
layerName""Layer name (overrides the layer index when set).
asWKB"true"Output geometry as WKB (binary) vs WKT (text).

All other vector reader options (driverName, …) are also available.

Basic usage​

# Lightweight Shapefile reader (pyogrio; no JAR)
from databricks.labs.gbx import register
register(spark)
df = spark.read.format("shapefile_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 Shapefile archives in a table for downstream analytics — on Databricks a managed table is Delta:

df = spark.read.format("shapefile_gbx").load("/Volumes/main/geo/raw/") # a folder of .shp/.zip files
df.write.mode("overwrite").saveAsTable("main.geo.parcels") # 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.

Next Steps​

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.
gbx_file_write / FILE-tier writeNot in the DataSourceFILE-tier writes go through vector_file_write (function layer). The DataSource writer commits via FUSE.
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.