1️⃣ What is Geohash?
Geohash is a system for encoding geographic coordinates (latitude and longitude) into a short string of letters and digits.
- It converts (lat, lon) → alphanumeric string
- The string represents a rectangular area on Earth (higher precision → longer string)
- It’s hierarchical, meaning shorter strings cover bigger areas, longer strings cover smaller areas
Use case: Efficient spatial indexing, location-based search, and proximity queries.
2️⃣ Why Geohash is Useful
-
Compact representation of coordinates
- Instead of storing two floats for latitude & longitude, you can store a string.
-
Spatial indexing
- Nearby coordinates often have similar Geohash prefixes, so you can do proximity search efficiently.
-
Database optimization
- Use Geohash as a key in databases (Redis, MongoDB, Postgres) to query regions fast.
-
Hierarchical querying
- Shorten a Geohash to get a larger area
- Lengthen a Geohash to get a smaller, precise area
3️⃣ How Geohash Works
-
Take latitude and longitude
- Latitude range: [-90, 90]
- Longitude range: [-180, 180]
-
Divide the range into halves repeatedly
- Assign 0 or 1 depending on whether the coordinate is in the lower or upper half
-
Interleave bits of latitude and longitude
- Create a single binary string
-
Convert binary string to base32
- Base32 characters:
0123456789bcdefghjkmnpqrstuvwxyz(note: letters likea,i,l,oare skipped for readability)
- Base32 characters:
4️⃣ Example
-
Coordinate:
(37.4219, -122.0840) -
Geohash (precision 7):
9q9hv7k -
Interpretation:
- Shorter Geohash (
9q9hv) → bigger area (~1.2 km × 0.6 km) - Longer Geohash (
9q9hv7k) → smaller area (~150 m × 150 m)
- Shorter Geohash (
-
Nearby locations → share same prefix
- Makes geospatial queries efficient
5️⃣ How Geohash is Used
-
Location-based search / services
- Find all users within 1 km radius
- Find all restaurants near a point
-
Spatial databases
- Store Geohash as an index column for faster queries
-
Mapping & clustering
- Aggregate points into cells for heatmaps
6️⃣ Benefits & Limitations
| Feature | Benefit / Limitation |
|---|---|
| Compact | Encodes coordinates into short strings |
| Hierarchical | Prefix length = spatial precision |
| Proximity | Nearby points often share prefix |
| Limitations | Rectangular area approximation (not perfect circle), edge cases near Geohash boundaries |
7️⃣ Quick Analogy
- Imagine the Earth as a grid of squares.
- Geohash = code of the square where a point lies.
- More letters = smaller squares → higher precision
If you want, I can also give a Java example showing how to encode/decode Geohash and do nearby searches — it’s very practical for system design and location-based apps.
Do you want me to do that?