跳到主要内容
跳到主要内容

处理 Geohash 的函数

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
备注
  • 所有坐标参数必须为相同类型:要么是 Float32,要么是 Float64
  • 对于 precision 参数,任何小于 1 或大于 12 的值都会默默转换为 12

返回值

  • 编码坐标的字母数字字符串(使用修改版本的 base32 编码字母表)。 String

示例

查询:

SELECT geohashEncode(-5.60302734375, 42.593994140625, 0) AS res;

结果:

┌─res──────────┐
│ ezs42d000000 │
└──────────────┘

geohashDecode

将任何 geohash 编码字符串解码为经度和纬度。

语法

geohashDecode(hash_str)

输入值

  • hash_str — Geohash 编码字符串。

返回值

  • 包含经度和纬度的 Float64 值的元组 (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
备注

所有坐标参数必须为相同类型:要么是 Float32,要么是 Float64

返回值

  • 包含所提供区域的 geohash-boxes 的精度长字符串数组,您不应依赖于项目的顺序。 Array(String)。
  • [] - 如果最小纬度和经度值不小于相应的最大值,返回空数组。
备注

如果结果数组超过 10,000,000 项,函数将抛出异常。

示例

查询:

SELECT geohashesInBox(24.48, 40.56, 24.785, 40.81, 4) AS thasos;

结果:

┌─thasos──────────────────────────────────────┐
│ ['sx1q','sx1r','sx32','sx1w','sx1x','sx38'] │
└─────────────────────────────────────────────┘