Arman Pouyaei Start a conversation
Lesson 25  ·  Nov 5, 2026
Data Engineering Fundamentals

Partition by place: spatial partitioning with geohashes

Lesson 15 taught partitioning by time — folders like region=us-northeast/month=2026-10 — so a query reads only the folders it needs. The same trick works for space: partition by location instead of, or as well as, time. A geohash turns a lat/lon pair into a short string, and points in the same cell share a prefix. Use that prefix as the partition key, and a query over one area reads only that area's folders.

The code

COPY (
  SELECT station_id, name, temp_c,
         ST_GeoHash(ST_Point(lon, lat), 4) AS geohash4
  FROM weather_stations
) TO 's3://lake/storm_readings/'
  (FORMAT PARQUET, PARTITION_BY (geohash4));

ST_GeoHash(ST_Point(lon, lat), 4) computes a 4-character geohash for each station — a 4-character cell is roughly 39 by 20 km, and each extra character shrinks the cell (3 characters is about 156 km across; 5 is about 5 km). PARTITION_BY (geohash4) then writes the Parquet files into one folder per cell, so the lake looks like this:

storm_readings/
  geohash4=dr5r/   ← New York / Princeton area
  geohash4=dr72/   ← Boston area
  geohash4=u4pr/   ← London area

A query for one area now prunes the rest away:

SELECT * FROM read_parquet('s3://lake/storm_readings/*/*.parquet')
WHERE geohash4 = 'dr5r';   -- reads only the New York-area folder

In practice

This is partition pruning from Lesson 15, applied to space: the WHERE clause matches the folder name, so the engine skips every other folder instead of scanning the whole lake. It is the standard layout for point data at scale — sensor networks, ride-hailing trips, storm readings — and it composes with time: partition by geohash4 and month when queries filter on both. Two things to watch: a region query usually spans several neighbouring cells, so you filter on a list of prefixes, not one; and points near a cell border live in different folders even though they are neighbours, which is exactly why the list matters. Pick the geohash length by the same cardinality rule as Lesson 15 — aim for dozens to hundreds of partitions, not millions of tiny ones.

Python bridge: this is the same binning idea as df.assign(geohash=...) followed by a groupby — or pandas.cut on latitude and longitude. The difference is where the bins live: as folders on disk, so the query engine can skip them before reading a single row. Lesson 29 comes back to geohash and H3 as indexes, not just partitions.

Further reading

How to partition your geospatial data lake for analysis with Amazon Redshift — AWS Public Sector Blog (a walkthrough of the same idea end to end: geohash a dataset, unload it to S3 partitioned by geohash, and query only the folders you need)

Try it

Practice question: using the same weather_stations table (station_id, name, lat, lon), write a query that returns each 3-character geohash cell and how many stations sit in it, most-crowded cell first. Bonus: what changes if you use 5 characters instead of 3?

Show answer
SELECT ST_GeoHash(ST_Point(lon, lat), 3) AS geohash3,
       COUNT(*) AS n_stations
FROM weather_stations
GROUP BY geohash3
ORDER BY n_stations DESC;

Result: one row per 3-character cell, most-crowded first — the top row is whichever cell holds the most stations in your table. Bonus: 5 characters makes much smaller cells (about 5 by 5 km instead of about 156 by 156 km), so you get many more cells with fewer stations each — finer pruning, but more folders and a small-files risk. 3 characters is coarse: few folders, less pruning.

← All posts