1 安装场景
- 在 anaconda多内核环境中,实现 由 经纬度 得到 geohash 编码 .
- 实现方式:
- 安装方式
- 通过 命令行 安装
- 在 Anaconda Prompt 窗口中,通过 pip 或 conda 命令安装Geohash模块 .
- 通过 源代码 安装
- 下载tar包或者源代码,通过 setup.py 文件完整安装 .
- 安装方式详见:
- 主要版本:
- 即,Geohash 模块中包含的的方法不同。前者较少,后者较多。
2 详细描述
- 文章主要记录在Anaconda Python 2 环境下,通过 geohash 完成编码的过程。具体为以下 4 方面:
3 具体代码(已安装mzgeohash)
# ### mzgeohash
# - 调用方式
# - 实现原理
# ### list all member methods
# In[23]:
dir(mzgeohash)
# #### 经纬度 转换为 geohash
# In[2]:
mzgeohash.encode([139.76608408614993, 35.681382017210126])
# #### geohash 转换为 经纬度
# In[33]:
mzgeohash.decode('xn76urwe1g9y')
# #### 已知 geohash ,计算某一方向的临近geohash
# - 参数 1
# - 已知 geohash 串
# - 参数 2
# - 方向
# In[35]:
mzgeohash.adjacent('xn76urwe1g9y','n') #
# #### 已知 geohash ,临近geohash
# - 九宫格,即周围 8 个geohash
# In[34]:
mzgeohash.neighbors('xn76urwe1g9y')
# #### 已知 某位置点经纬度和周围经纬度点集,计算同属 geohash
# In[37]:
test_centroid = [-122.18472385000001, 37.7881345]
test_points = [
(-122.2992715, 37.9030588),
(-122.396742, 37.792976),
(-122.4474142, 37.72198087),
(-121.9764, 37.557355),
(-122.029095, 37.973737),
(-122.224274, 37.774963),
(-122.271604, 37.803664),
(-122.126871, 37.697185),
(-122.087967, 37.670399),
(-122.123801, 37.893394),
(-122.269029, 37.80787),
(-122.265609, 37.797484),
(-122.267227, 37.828415),
(-122.067423, 37.905628),
(-122.267227, 37.828415),
(-122.38666, 37.599787),
(-122.075567, 37.690754),
(-122.401407, 37.789256),
(-122.283451, 37.87404),
(-122.269029, 37.80787),
(-122.1837911, 37.87836087),
(-122.419694, 37.765062),
(-122.2945822, 37.80467476),
(-122.21244024, 37.71297174),
(-121.945154, 38.018914),
(-122.466233, 37.684638),
(-122.056013, 37.928403),
(-122.406857, 37.784991),
(-122.418466, 37.752254),
(-122.26978, 37.853024),
(-122.251793, 37.844601),
(-121.928099, 37.699759),
(-122.416038, 37.637753),
(-122.1613112, 37.72261921),
(-122.0575506, 37.63479954),
(-122.392612, 37.616035),
(-122.413756, 37.779528),
(-122.353165, 37.936887),
(-122.197273, 37.754006),
(-122.017867, 37.591208),
(-122.024597, 38.003275),
(-122.4690807, 37.70612055),
(-122.268045, 37.869867),
(-122.444116, 37.664174),
(-121.900367, 37.701695),
(-122.317269, 37.925655),
(-122.434092, 37.732921)
]
# expect = '9q9'
mzgeohash.neighborsfit(test_centroid, test_points)
-
mzgeohash 运行Demo
- 2 利用 unittest 建立 mzgeohash 测试用例并执行
import unittest
from mzgeohash.test_geohash import Test_encode_decode
from mzgeohash.test_geohash import Test_adjacent
from mzgeohash.test_geohash import Test_neighbors
from mzgeohash.test_geohash import Test_neighborsfit
suite=unittest.TestSuite()
suite.addTest(Test_encode_decode('test_decode'))
suite.addTest(Test_encode_decode('test_encode'))
suite.addTest(Test_encode_decode('test_roundtrip'))
suite.addTest(Test_neighbors('test_neighbors'))
suite.addTest(Test_adjacent('test_adjacent'))
suite.addTest(Test_neighborsfit('test_neighborsfit'))
runner=unittest.TextTestRunner()
runner.run(suite);
-
unittest 建立测试用例并执行Demo
4 参考文章
5 总结