1. Geohash算法介绍 – 详细介绍
----------------------------------------------------------------------------------------------------------------------------------------
2.Python的实现方案
- 方案一:使用 libgeohash 库函数
pip install libgeohash
- 方案二:使用 geohash 库函数
pip install geohash
- 方案三:使用 transbigdata 库函数
pip install -U transbigdata
- 方案四:使用 自己复现的 函数
2.1:使用 libgeohash 库函数–详细说明
库分为3个模块:
- geohash\u base:用于与geohash交互的基函数,例如
encode
、decode
、neighbors
、bbox
等。 - 距离度量:与距离相关的函数,例如
distance
、dimensions
等。 - geometry:将多边形转换为geohash列表的函数,反之亦然。例如
polygon_to_geohash
,geohash_to_polygon
。用地理差近似地理区域是有用的。使用shapely库进行几何计算。
安装库方法:pip install libgeohash
代码:
import libgeohash as gh
# 1.1编码示例
print(gh.encode(lat=39.9078, lon=116.3977)) # 'wx4g08ypycpg'
print(gh.encode(lat=39.9078, lon=116.3977, precision=5)) # 'wx4g0', 指定编码精度(默认12)
# 1.2解码示例
# (geohash, errers)主要是将geohash解码为它的确切值,包括错误结果的边距。返回四个浮点值:纬度、经度、纬度的正负误差(为正)、经度的正负误差(为正)。
print(gh.decode(ghash='wx4g08ypycpg')) # (39.90779994986951, 116.39770014211535)
print(gh.decode('wx4g08ypycpg', errors=True)) # (39.90779994986951, 116.39770014211535, 8.381903171539307e-08, 1.6763806343078613e-07)
print(gh.decode('wx4g0', errors=True)) # (39.92431640625, 116.38916015625, 0.02197265625, 0.02197265625)
# 1.3邻居区域查找 (相邻的八个区域)
print(gh.neighbors(ghash='wx4g08ypycpg'))
print(gh.neighbors(ghash='wx4g0')) # {'n': 'wx4g2', 's': 'wx4fb', 'e': 'wx4g1', 'w': 'wx4ep', 'ne': 'wx4g3', 'nw': 'wx4er', 'se': 'wx4fc', 'sw': 'wx4dz'}
# 1.4 geohash区域的边界(coordinates表示输出的形式是否为坐标形式,默认False)
print(gh.bbox('wx4g08ypycpg')) # {'n': 39.907800033688545, 's': 39.90779986605048, 'w': 116.39769997447729, 'e': 116.39770030975342}
print(gh.bbox(ghash='wx4g0')) # {'n': 39.9462890625, 's': 39.90234375, 'w': 116.3671875, 'e': 116.4111328125}
print(gh.bbox(ghash='wx4g0', coordinates=True)) # [(39.90234375, 116.3671875), (39.9462890625, 116.3671875), (39.9462890625, 116.4111328125), (39.90234375, 116.4111328125)]
# 2.1 返回由geohash引用的边界框的维度,单位为米。(宽度、高度)
# 当 actual 为True时,返回由geohash创建的边界框的实际大小,而不是基于geohash长度的度量。
print(gh.dimensions('wx4g08ypycpg')) # (0.037, 0.019)-->更精确,边界框的长宽就越小
print(gh.dimensions(