Arman Pouyaei Start a conversation
Lesson 26  ·  Nov 6, 2026
Data Engineering Fundamentals

Apache Sedona: Spark, but spatial

Your PySpark DataFrames from Phase 1 do not know what a geometry is — no spatial types, no ST_ functions, no spatial joins. Apache Sedona fixes that: it is a layer on top of Spark that adds a real Geometry type, the same ST_ function family you used in PostGIS and DuckDB, and spatial partitioning under the hood, so the spatial SQL from Lesson 22 runs across a cluster on lake-scale data.

The code

from sedona.spark import SedonaContext

config = SedonaContext.builder() \
    .appName("sedona-stations") \
    .getOrCreate()
sedona = SedonaContext.create(config)

stations = sedona.read.format("geoparquet").load("stations.geoparquet")
counties = sedona.read.format("geoparquet").load("counties.geoparquet")

stations.createOrReplaceTempView("stations")
counties.createOrReplaceTempView("counties")

result = sedona.sql("""
    SELECT c.name AS county, COUNT(*) AS n_stations
    FROM stations s
    JOIN counties c
      ON ST_Contains(c.geometry, s.geometry)
    GROUP BY c.name
""")
result.show()

In practice

Two things happen here. SedonaContext is a SparkSession with Sedona's types and functions registered — swap it in for a plain SparkSession and Spark SQL gains spatial powers. And the read format is geoparquet: Sedona reads Lesson 24's GeoParquet natively, geometry column and all, no conversion step.

The join is the interesting part. A naive Spark join would shuffle every station against every county. Sedona first repartitions both sides by space (the Lesson 25 idea, inside the engine), so candidate pairs land on the same machines, then applies an index inside each partition before running the exact ST_Contains test. That is what makes a spatial join over hundreds of millions of rows finish at all.

When do you reach for it? When the data no longer fits one machine: nationwide storm readings, years of satellite-derived features, global sensor networks. Below that, DuckDB or PostGIS on one box is simpler and usually faster. Same SQL either way — the ST_ names transfer, only the engine changes.

Python bridge: if you can write a Spark DataFrame pipeline (Lessons 18–21) and a spatial SQL query (Lesson 22), you already know Sedona. It is those two skills composed, plus one new entry point to remember: SedonaContext.

Further reading

Spatial SQL app — Apache Sedona documentation (the official tutorial: set up Sedona, load spatial data, and run SedonaSQL queries end to end)

Try it

Practice question: using the same stations and counties views, write a query that lists each station's name next to the county it sits in, sorted by county then station name.

Show answer
SELECT s.name AS station, c.name AS county
FROM stations s
JOIN counties c
  ON ST_Contains(c.geometry, s.geometry)
ORDER BY county, station;

Result: one row per station, labelled with its county. Stations that fall outside every county disappear — it is an inner join, so add a LEFT JOIN if you want them listed with a blank county instead.

← All posts