Geohash encoding
A geohash turns a pair of coordinates into a compact, sortable string. It is widely used in databases and search engines because points that are near each other usually share a long common prefix, letting you find neighbours with a simple range scan.
How it works
The algorithm interleaves bits of longitude and latitude. Starting from the full ranges of -180 to 180 for longitude and -90 to 90 for latitude, it repeatedly halves a range and records a 1 if the coordinate falls in the upper half or a 0 otherwise. Bits alternate between longitude (first) and latitude. Every five bits become one base-32 character.
lat 57.64911, lng 10.40744 -> u4pruydqqvj
The more characters you request, the more halving steps run, and the smaller the resulting cell.
Precision vs cell size reference
| Precision | Approx cell dimensions | Typical use |
|---|---|---|
| 4 chars | ~39 × 20 km | Country-level clustering |
| 5 chars | ~4.9 × 4.9 km | City-level grouping |
| 6 chars | ~1.2 × 0.6 km | Neighbourhood clustering |
| 7 chars | ~153 × 153 m | Street-level proximity |
| 8 chars | ~38 × 19 m | Building-level |
| 9 chars | ~4.8 × 4.8 m | Precise indoor location |
| 12 chars | ~37 × 19 mm | Survey-grade (theoretical) |
Worked example
Encoding Copenhagen’s city hall square (latitude 55.6761, longitude 12.5683) at precision 7 yields approximately u3bu7gu. Any record stored in the same prefix u3bu7 is within a few hundred metres — enough to find nearby restaurants or attractions with a simple string prefix query.
The boundary edge case
This is the most common geohash gotcha: two physically adjacent points can share no prefix if a cell boundary runs between them. For example, a hotel at u3bu7zz and a restaurant just across the street in the next cell (u3bu7xy) differ from the third character onward. The standard fix is to also query the eight neighbouring cells around your target — the neighbours can be computed from the original geohash by bit manipulation. Most geohash libraries expose a neighbors() function for this.
Database indexing pattern
Geohashes shine as database index keys because standard B-tree indexes handle prefix scans efficiently:
-- Find all rows within roughly 1 km of the target (precision-6 prefix)
SELECT * FROM locations WHERE geohash LIKE 'u3bu7g%';
For production use, store multiple precision levels (for example, both a 5-char and a 7-char column) so you can tune the query radius without recomputing the hash at query time. All encoding in this tool runs locally in your browser.