EN
Back to the archive

The encyclopedia · Software & IT · Technical decision · 2008–2014

Geohash turned coordinates into sortable strings, and databases adopted it

Gustavo Niemeyer's 2008 geohash packs lat/lon into one base32 string; longer shared prefixes mean closer places, so databases index locations as strings.

geohash.org (Gustavo Niemeyer) · Elasticsearch

The solution

In February 2008 Gustavo Niemeyer introduced geohash and launched geohash.org: a public-domain geocode system that encodes a geographic location into a short string of letters and digits. The idea was to make coordinates something you could write in an email, a URL or a database key.

The trick is hierarchy in the prefix. A geohash interleaves the bits of latitude and longitude and encodes them in base32; each extra character adds five bits of precision and identifies one of 32 sub-cells of the previous one. Nearby locations generally share prefixes, and chopping characters off the end shrinks the area while keeping the code valid.

That property is what made it a standard. Elasticsearch documents that a geo-point can be expressed as a geohash — base32 strings of interleaved latitude and longitude bits — and uses them for indexing and grid aggregations, so a proximity search becomes a string-prefix search over an index that already exists.

Why it worked

  • Prefix length doubles as precision and proximity, so one field does both.
  • A short string is easier to share than a coordinate pair.
  • Existing string indexes need no new machinery to do geospatial work.
  • Public-domain release removed any licensing reason to refuse it.
What it achievedOne string whose prefix means proximityclever

What can be applied

Compress a 2-D problem into a 1-D key and every existing string index becomes a spatial index — prefix length encodes both precision and proximity, so no new machinery is needed.

Aftermath

Geohash became a standard way to express and index locations: Elasticsearch documents geohash geopoints and geohash-grid aggregations, and the string form made it natural for caches, URLs and database keys. It is now one of the default encodings behind geospatial indexes.

Sources

spotted an error? The archive wants to know.

Related cases