2020-07-28 07:57:28 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/geo/geohash
2022-04-09 13:29:05 +00:00
sidebar_label: Geohash
2022-08-29 16:19:50 +00:00
title: "Functions for Working with Geohash"
2020-07-28 07:57:28 +00:00
---
2024-06-12 13:09:50 +00:00
## Geohash
2024-06-12 12:09:37 +00:00
2024-08-22 09:29:33 +00:00
[Geohash ](https://en.wikipedia.org/wiki/Geohash ) is the geocode system, which subdivides Earth’ s surface into buckets of grid shape and encodes each cell into a short string of letters and digits. It is a hierarchical data structure, so the longer the geohash string is, the more precise the geographic location will be.
2020-07-28 07:57:28 +00:00
If you need to manually convert geographic coordinates to geohash strings, you can use [geohash.org ](http://geohash.org/ ).
2022-06-02 10:55:18 +00:00
## geohashEncode
2020-07-28 07:57:28 +00:00
2024-06-12 13:09:50 +00:00
Encodes latitude and longitude as a [geohash ](#geohash )-string.
2020-07-28 07:57:28 +00:00
2024-08-22 09:29:33 +00:00
**Syntax**
2020-07-28 07:57:28 +00:00
``` sql
geohashEncode(longitude, latitude, [precision])
```
**Input values**
2024-08-22 11:49:16 +00:00
- `longitude` — Longitude part of the coordinate you want to encode. Floating in range`[-180°, 180°]`. [Float ](../../data-types/float.md ).
- `latitude` — Latitude part of the coordinate you want to encode. Floating in range `[-90°, 90°]` . [Float ](../../data-types/float.md ).
2024-08-22 09:29:33 +00:00
- `precision` (optional) — Length of the resulting encoded string. Defaults to `12` . Integer in the range `[1, 12]` . [Int8 ](../../data-types/int-uint.md ).
:::note
- All coordinate parameters must be of the same type: either `Float32` or `Float64` .
- For the `precision` parameter, any value less than `1` or greater than `12` is silently converted to `12` .
:::
2020-07-28 07:57:28 +00:00
**Returned values**
2024-08-22 09:29:33 +00:00
- Alphanumeric string of the encoded coordinate (modified version of the base32-encoding alphabet is used). [String ](../../data-types/string.md ).
2020-07-28 07:57:28 +00:00
**Example**
2024-08-22 09:29:33 +00:00
Query:
2020-07-28 07:57:28 +00:00
``` sql
2021-03-13 18:18:45 +00:00
SELECT geohashEncode(-5.60302734375, 42.593994140625, 0) AS res;
2020-07-28 07:57:28 +00:00
```
2024-08-22 09:29:33 +00:00
Result:
2020-07-28 07:57:28 +00:00
``` text
┌─res──────────┐
│ ezs42d000000 │
└──────────────┘
```
2022-06-02 10:55:18 +00:00
## geohashDecode
2020-07-28 07:57:28 +00:00
2024-06-12 13:09:50 +00:00
Decodes any [geohash ](#geohash )-encoded string into longitude and latitude.
2020-07-28 07:57:28 +00:00
2024-08-22 09:29:33 +00:00
**Syntax**
```sql
geohashDecode(hash_str)
```
2020-07-28 07:57:28 +00:00
**Input values**
2024-08-22 09:29:33 +00:00
- `hash_str` — Geohash-encoded string.
2020-07-28 07:57:28 +00:00
**Returned values**
2024-08-22 09:29:33 +00:00
- Tuple `(longitude, latitude)` of `Float64` values of longitude and latitude. [Tuple ](../../data-types/tuple.md )([Float64](../../data-types/float.md))
2020-07-28 07:57:28 +00:00
**Example**
``` sql
2021-03-13 18:18:45 +00:00
SELECT geohashDecode('ezs42') AS res;
2020-07-28 07:57:28 +00:00
```
``` text
┌─res─────────────────────────────┐
│ (-5.60302734375,42.60498046875) │
└─────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## geohashesInBox
2020-07-28 07:57:28 +00:00
2024-06-12 13:09:50 +00:00
Returns an array of [geohash ](#geohash )-encoded strings of given precision that fall inside and intersect boundaries of given box, basically a 2D grid flattened into array.
2020-07-28 07:57:28 +00:00
**Syntax**
``` sql
geohashesInBox(longitude_min, latitude_min, longitude_max, latitude_max, precision)
```
2021-02-15 21:22:10 +00:00
**Arguments**
2020-07-28 07:57:28 +00:00
2024-05-24 03:54:16 +00:00
- `longitude_min` — Minimum longitude. Range: `[-180°, 180°]` . [Float ](../../data-types/float.md ).
- `latitude_min` — Minimum latitude. Range: `[-90°, 90°]` . [Float ](../../data-types/float.md ).
- `longitude_max` — Maximum longitude. Range: `[-180°, 180°]` . [Float ](../../data-types/float.md ).
- `latitude_max` — Maximum latitude. Range: `[-90°, 90°]` . [Float ](../../data-types/float.md ).
- `precision` — Geohash precision. Range: `[1, 12]` . [UInt8 ](../../data-types/int-uint.md ).
2020-07-28 07:57:28 +00:00
2022-04-09 13:29:05 +00:00
:::note
All coordinate parameters must be of the same type: either `Float32` or `Float64` .
:::
2020-07-28 07:57:28 +00:00
**Returned values**
2024-05-24 03:54:16 +00:00
- Array of precision-long strings of geohash-boxes covering provided area, you should not rely on order of items. [Array ](../../data-types/array.md )([String](../../data-types/string.md)).
2023-04-19 15:55:29 +00:00
- `[]` - Empty array if minimum latitude and longitude values aren’ t less than corresponding maximum values.
2020-07-28 07:57:28 +00:00
2022-04-09 13:29:05 +00:00
:::note
Function throws an exception if resulting array is over 10’ 000’ 000 items long.
:::
2020-07-28 07:57:28 +00:00
**Example**
Query:
``` sql
2021-03-13 18:18:45 +00:00
SELECT geohashesInBox(24.48, 40.56, 24.785, 40.81, 4) AS thasos;
2020-07-28 07:57:28 +00:00
```
2021-03-13 18:18:45 +00:00
2020-07-28 07:57:28 +00:00
Result:
``` text
┌─thasos──────────────────────────────────────┐
│ ['sx1q','sx1r','sx32','sx1w','sx1x','sx38'] │
└─────────────────────────────────────────────┘
```