What Is a Geohash? How It Works, With a Worked Example

Published

A geohash is a short string of letters and digits that stands for a rectangle on the Earth. The Statue of Liberty, at 40.689247, -74.044502, has the 9-character geohash dr5r7p62n. Each extra character makes the rectangle smaller, so the length of the code sets its precision.

Geohashes are popular in software because they turn two numbers into one sortable text value. Places with the same starting characters are near each other, which makes it easy to index and search locations in databases.

The geohash alphabet

A geohash uses 32 characters: the digits 0 to 9 and the lowercase letters b to z, without a, i, l and o. Three of the missing letters are easy to mistake for the digits 1 and 0.

0123456789bcdefghjkmnpqrstuvwxyz

Each character stands for a number from 0 to 31, its position in this list. Since 32 = 25, every character carries exactly 5 bits.

How the encoding works

The idea is to keep cutting the world in half and record which half the point is in:

  1. Start with the whole world: longitude -180 to 180 and latitude -90 to 90.
  2. Cut the longitude range in the middle. If the point is in the eastern (upper) half, write bit 1; if it is in the western (lower) half, write 0. Keep only that half.
  3. Cut the latitude range in the middle the same way: north half 1, south half 0.
  4. Keep alternating longitude, latitude, longitude… Each new bit halves one side of the box.
  5. Group the bits in fives and turn each group into a character from the alphabet.

Because the bits of the two coordinates are interleaved, one string describes both latitude and longitude at once. Longitude gets the first bit. With an odd number of characters, longitude gets one bit more than latitude; since the longitude range is twice as large (360° against 180°), the cell ends up the same size in degrees on both axes. With an even number, both get the same number of bits and the cell is twice as wide in longitude.

Worked example: the Statue of Liberty

Point: latitude 40.689247, longitude -74.044502.

First character

BitCutMiddlePoint is…Bit value
1Longitude -180 to 1800below (west)0
2Latitude -90 to 900above (north)1
3Longitude -180 to 0-90above1
4Latitude 0 to 9045below0
5Longitude -90 to 0-45below0

The bits are 01100, which is 12 in binary. Character number 12 in the alphabet (counting from 0) is d.

Second and third characters

The next five cuts compare the point with latitude 22.5, longitude -67.5, latitude 33.75, longitude -78.75 and latitude 39.375. The results are 10111 = 23, which is r. So far the code is dr, a cell from 39.375° N to 45° N and from 78.75° W to 67.5° W, covering a large part of the US Northeast.

The third group compares with longitude -73.125, latitude 42.1875, longitude -75.9375, latitude 40.78125 and longitude -74.53125. The bits 00101 = 5 give the character 5.

Continuing the same way gives dr5r7p62n after nine characters. That cell runs from 40.689240° N to 40.689282° N and from 74.044504° W to 74.044461° W. Its centre, 40.689261, -74.044483, is about 2 m from the original point.

You do not need to do this by hand: the Plus Code and geohash converter encodes and decodes geohashes and shows the cell on a map.

Precision: cell size per length

Each character adds 5 bits, which alternately cut longitude and latitude. The table shows the cell size in degrees and in distance on a sphere with the mean Earth radius (6,371.0088 km). Widths are given at the equator; a degree of longitude shrinks toward the poles, so cells are narrower elsewhere.

LengthCell size (lat × long)HeightWidth at the equator
145° × 45°5,004 km5,004 km
25.625° × 11.25°625 km1,251 km
31.40625° × 1.40625°156 km156 km
40.17578° × 0.35156°19.5 km39.1 km
50.043945° × 0.043945°4.89 km4.89 km
60.0054932° × 0.010986°611 m1.22 km
70.0013733° × 0.0013733°153 m153 m
80.00017166° × 0.00034332°19.1 m38.2 m
90.000042915° × 0.000042915°4.77 m4.77 m
100.0000053644° × 0.000010729°60 cm1.19 m
110.0000013411° × 0.0000013411°15 cm15 cm
120.00000016764° × 0.00000033528°1.9 cm3.7 cm

At the latitude of New York the cells are narrower. The 9-character cell for the Statue of Liberty is about 4.77 m tall but only about 3.62 m wide.

A geohash is a cell, not a point. If you decode it, you get the centre of the cell plus an error margin of half the cell size on each axis.

The prefix property

Every geohash cell contains 32 smaller cells, one for each possible next character. So a shorter geohash is the parent of every longer geohash that starts with it: dr5r contains dr5r7p62n.

This is what makes geohashes useful in databases. To find everything in a cell, search for strings that start with its code. New York City Hall (40.712776, -74.005974) has the geohash dr5regw3p. It shares the first four characters, dr5r, with the Statue of Liberty, about 4.2 km away.

Edge cases to watch

The reverse is not always true: nearby places do not always share a prefix. When a cell boundary runs between two points, their codes can differ from the very first character.

Proximity searches therefore usually look at the target cell and its eight neighbours, not just one prefix. For exact distances, use the coordinates themselves with the distance calculator or read distance between two coordinates.

Geohash compared with other codes

Geohashes use latitude and longitude on WGS84, like GPS. If your source data uses another datum, convert it first; what is a datum explains why.

Frequently asked questions

What is a geohash used for?

Geohashes turn a latitude and longitude into a short text string. Databases and search engines use them to index locations, group nearby points and run fast proximity searches with simple text prefixes.

How many characters should a geohash have?

It depends on the precision you need. Five characters give a cell about 4.9 km across, seven characters about 153 m and nine characters about 4.8 m. Nine characters is a common choice for a single address or building.

Do nearby places always share the same geohash prefix?

No. Places with a long common prefix are always close, but two close places can have completely different geohashes if a cell boundary runs between them, for example at the equator or the Prime Meridian.

Is a geohash the same as a Plus Code?

No. Both encode a location as a short string, but they use different alphabets and grids. A geohash uses 32 characters and halves the cell with every bit; a Plus Code uses 20 characters and a different layout.