跳转到主要内容

Geohash

Geohash 是一种地理编码系统,它将地球表面划分为网格状区域,并将每个单元编码为由字母和数字组成的短字符串。它是一种层级化数据结构,因此 geohash 字符串越长,表示的地理位置就越精确。 如果你需要手动将地理坐标转换为 geohash 字符串,可以使用 geohash.org

geohashEncode

将纬度和经度编码为 geohash 字符串。 语法
geohashEncode(longitude, latitude, [precision])
输入值
  • longitude — 要编码的坐标的经度部分。浮点数,范围为 [-180°, 180°]Float
  • latitude — 要编码的坐标的纬度部分。浮点数,范围为 [-90°, 90°]Float
  • precision (可选) — 编码结果字符串的长度。默认为 12。整数,范围为 [1, 12]Int8
  • 所有坐标参数必须为相同类型,即 Float32Float64
  • 对于 precision 参数,任何小于 1 或大于 12 的值都会被静默转换为 12
返回值
  • 编码后坐标的字母数字字符串 (使用的是修改版的 base32 编码字母表) 。String
示例
Query
SELECT geohashEncode(-5.60302734375, 42.593994140625, 0) AS res;
Response
┌─res──────────┐
│ ezs42d000000 │
└──────────────┘

geohashDecode

将任何以 geohash 编码的字符串解码为经度和纬度。 语法
geohashDecode(hash_str)
输入值
  • hash_str — Geohash 编码的字符串。
返回值
  • 由经度和纬度的 Float64 值构成的 Tuple (longitude, latitude)Tuple(Float64)
示例
SELECT geohashDecode('ezs42') AS res;
┌─res─────────────────────────────┐
│ (-5.60302734375,42.60498046875) │
└─────────────────────────────────┘

geohashesInBox

返回指定精度、位于给定框内并与其边界相交的 geohash 编码字符串数组,本质上是将二维网格展平后得到的数组。 语法
geohashesInBox(longitude_min, latitude_min, longitude_max, latitude_max, precision)
参数
  • longitude_min — 最小经度。范围:[-180°, 180°]Float
  • latitude_min — 最小纬度。范围:[-90°, 90°]Float
  • longitude_max — 最大经度。范围:[-180°, 180°]Float
  • latitude_max — 最大纬度。范围:[-90°, 90°]Float
  • precision — Geohash 精度。范围:[1, 12]UInt8

所有坐标参数必须使用相同的类型:Float32Float64
返回值
  • 由覆盖给定区域的 geohash 网格组成的、长度为 precision 的字符串数组;不应依赖其中元素的顺序。Array(String)。
  • [] - 如果最小纬度和经度不小于对应的最大值,则返回空数组。

如果结果数组长度超过 10’000’000,函数会抛出异常。
示例
Query
SELECT geohashesInBox(24.48, 40.56, 24.785, 40.81, 4) AS thasos;
Response
┌─thasos──────────────────────────────────────┐
│ ['sx1q','sx1r','sx32','sx1w','sx1x','sx38'] │
└─────────────────────────────────────────────┘
最后修改于 2026年6月10日