地理哈希

Build Status

原生 Swift 地理哈希库,支持二进制和字符编码

我还有一个 Rust 版本

地理哈希在内部表示为 64 位整数。哈希可以使用字符精度(最多 12 个字符)或二进制精度(每个角度最多 32 位)来构建。

GeohashBits(location: Location(longitude: -0.1, latitude: 51.5), characterPrecision: 12).hash()
=> "gcpuvxr1jzf"
GeohashBits(location: Location(longitude: -0.1, latitude: 51.5), bitPrecision: 26).hash()
=> "gcpuvxr1jz" 

请注意,即使“完整”表示总共为 52 位,但最后一个值仍被截断为 10 个字符(50 位)。由于字符编码是 Base32,我们每个字符需要 5 位。

当字符表示为纬度和经度提供不同数量的位时(例如,地理哈希 7,其经度为 18 位,纬度为 17 位),地理哈希边界和质心会得到正确处理。

GeohashBits(hash: "u10hfr2").boundingBox().center()
=> (longitude: 0.0995635986328125, latitude: 51.5004730224609)

这与您从 PostGIS 获得的答案相同

select ST_AsText(ST_PointFromGeoHash('u10hfr2'));
st_astext                  
--------------------------------------------
POINT(0.0995635986328125 51.5004730224609)

该库还支持计算邻居

GeohashBits(hash: "u10hfr2c4pv").neighbor(.north).hash()
=> "u10hfr2c60j"

致谢

基于 Redis 实现