Geohash functions
Work with geohashes for compact representation of geographic coordinates.
ClickHouse function reference
geohashEncode
Encodes latitude and longitude as a geohash string.
Syntax:
Arguments:
longitude
(Float32
orFloat64
): Longitude of the coordinate to encode. Range: [-180°, 180°].latitude
(Float32
orFloat64
): 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
orFloat64
. - Any precision value less than 1 or greater than 12 is silently converted to 12.
Example:
Result:
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:
Arguments:
hash_str
(String
): A geohash-encoded string.
Returns:
A tuple containing two Float64
values: (longitude, latitude).
Example:
Result:
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:
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
orFloat64
. - 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:
Result:
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.
Was this page helpful?