ClickHouse function reference

geohashEncode

Encodes latitude and longitude as a geohash string.

Syntax:

geohashEncode(longitude, latitude[, precision])

Arguments:

  • longitude (Float32 or Float64): Longitude of the coordinate to encode. Range: [-180°, 180°].
  • latitude (Float32 or Float64): Latitude of the coordinate to encode. Range: [-90°, 90°].
  • precision (UInt8, optional): Length of the resulting encoded string. Range: [1, 12]. Default: 12.

Returns:

An alphanumeric string of the encoded coordinate (using a modified version of the base32-encoding alphabet). [String]

  • Both coordinate parameters must be of the same type: either Float32 or Float64.
  • Any precision value less than 1 or greater than 12 is silently converted to 12.

Example:

SELECT
	geohashEncode(-99.12345, 19.98765, 6) AS taco_truck_location;

Result:

| taco_truck_location |
|---------------------|
| 9g998q              |

In this example, we encode the location of a taco truck in Mexico City using a geohash with precision 6. This geohash can be used for efficient spatial indexing and proximity searches.

geohashDecode

Decodes a geohash-encoded string into longitude and latitude coordinates.

Syntax:

geohashDecode(hash_str)

Arguments:

  • hash_str (String): A geohash-encoded string.

Returns:

A tuple containing two Float64 values: (longitude, latitude).

Example:

SELECT
    geohashDecode('9q8yyk8yuv') AS taco_truck_location,
    tupleElement(geohashDecode('9q8yyk8yuv'), 1) AS longitude,
    tupleElement(geohashDecode('9q8yyk8yuv'), 2) AS latitude;

Result:

| taco_truck_location                      | longitude           | latitude           |
|------------------------------------------|---------------------|--------------------|
| (-122.41941511631012,37.77493089437485)  | -122.41941511631012 | 37.77493089437485  |

In this example, we decode the geohash of a popular taco truck location in San Francisco. The untuple function is used to separate the longitude and latitude into individual columns for easier use in further calculations or queries.

The precision of the returned coordinates depends on the length of the input geohash string. Longer geohashes provide more precise locations.

geohashesInBox

Returns an array of geohash-encoded strings that fall inside or intersect with the boundaries of a given geographic box.

Syntax:

geohashesInBox(longitude_min, latitude_min, longitude_max, latitude_max, precision)

Arguments:

  • longitude_min (Float64): Minimum longitude of the box. Range: [-180°, 180°].
  • latitude_min (Float64): Minimum latitude of the box. Range: [-90°, 90°].
  • longitude_max (Float64): Maximum longitude of the box. Range: [-180°, 180°].
  • latitude_max (Float64): Maximum latitude of the box. Range: [-90°, 90°].
  • precision (UInt8): Geohash precision. Range: [1, 12].

Returns:

An array of geohash-encoded strings (Array(String)) covering the provided area.

  • All coordinate parameters must be of the same type: either Float32 or Float64.
  • The function returns an empty array if minimum latitude and longitude values aren’t less than corresponding maximum values.
  • The function throws an exception if the resulting array is over 10,000,000 items long.

Example:

SELECT
	geohashesInBox(-98.5, 29.4, -98.3, 29.6, 5) AS taco_zone;

Result:

| taco_zone                                                                                                                                                                                                                                         |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ['9v1zq','9v1zw','9v1zy','9v3bn','9v3bq','9v1zr','9v1zx','9v1zz','9v3bp','9v3br','9v4p2','9v4p8','9v4pb','9v600','9v602','9v4p3','9v4p9','9v4pc','9v601','9v603','9v4p6','9v4pd','9v4pf','9v604','9v606','9v4p7','9v4pe','9v4pg','9v605','9v607'] |

In this example, we’re finding geohashes that cover a potential “taco zone” in San Antonio, Texas. The result is an array of geohash strings, each representing a small area within or intersecting the specified geographic box.