Arman Pouyaei Start a conversation
Lesson 23  ·  Nov 3, 2026
Data Engineering Fundamentals

DuckDB goes spatial: no server, just a file

Yesterday, spatial SQL ran in a database. Today it runs on your laptop: DuckDB is an in-process SQL engine — no server to set up — and its spatial extension adds a GEOMETRY type and the same ST_ functions you met in Lesson 22. Two lines switch it on, and then you can query a GeoJSON file as if it were a table.

The code

INSTALL spatial;
LOAD spatial;

SELECT name,
       ST_Distance(geom, ST_Point(-74.66, 40.36)) AS dist_deg
FROM ST_Read('stations.geojson')
ORDER BY dist_deg
LIMIT 5;

INSTALL spatial downloads the extension once; LOAD spatial switches it on for this session — the same two-step pattern as importing a Python package. ST_Read('stations.geojson') then reads the file as a table, with a geom geometry column, so there is nothing to import or load first. ST_Point(-74.66, 40.36) builds the Princeton reference point (longitude first, as always), ST_Distance measures each station against it, and ORDER BY … LIMIT 5 keeps the five nearest. One honest caveat: with plain lon/lat geometry the distance comes back in degrees, not kilometres — fine for ranking nearest stations, not for reporting a distance in km.

In practice

This is the local-exploration step of a geospatial pipeline: before you build anything, you open a file of stations, gauges, or storm tracks and ask quick questions in SQL — nearest to a site, inside a boundary, how far apart. A data engineer reaches for DuckDB here because it is fast on a laptop, reads Parquet, CSV, GeoJSON, and Shapefiles directly, and the ST_ function names carry straight over to PostGIS later, so nothing you learn is throwaway. It also reads Parquet files natively — including Parquet files that carry a geometry column — so the same queries work on lake data, not just raw GeoJSON.

Python bridge: think of it as geopandas.read_file() plus SQL. In Python you would load the file into a GeoDataFrame and compute distances; here the file stays a file, DuckDB queries it in place, and the ST_ functions do the geometry work — no server, no import step, same SQL you already know.

Further reading

Spatial Extension — DuckDB Docs (the official guide: installing and loading the extension, the GEOMETRY type, and ST_Read for GeoJSON, Shapefile, and GeoPackage files)

Try it

Practice question: using the same stations.geojson file, write a query that returns the name and distance of the 3 weather stations nearest to Princeton (longitude -74.66, latitude 40.36).

Show answer
INSTALL spatial;
LOAD spatial;

SELECT name,
       ST_Distance(geom, ST_Point(-74.66, 40.36)) AS dist_deg
FROM ST_Read('stations.geojson')
ORDER BY dist_deg
LIMIT 3;

Result: three rows — the three stations closest to Princeton, nearest first, each with its distance in degrees. The only change from the example is LIMIT 5 down to LIMIT 3; the ordering does the ranking, so the nearest station is always the first row.

← All posts