【甘道夫】Geohash基础应用By Python

介绍Geohash基础概念的文章很多,我就不拷贝粘贴了,我只用最简单的理解概括:

Geohash用作坐标的索引,可大幅提升搜索坐标相关数据的效率。结合相邻9宫格的geohash,可以快速检索指定坐标附近一定范围内的地理信息目标(POI,道路等等)。

python环境下的geohash库推荐:

https://github.com/transitland/mapzen-geohash

该库基础功能完整,包括坐标编码为geohash,geohash解码为坐标,获取指定geohash周边9宫格的geohash。


安装:pip install mzgeohash


geohash编码的简单示例如下:

输入文件每行包含一个坐标,例如117.445044487,40.0585138025

处理代码:

# !/usr/bin/env python
# encoding:UTF-8
import time
import sys
import mzgeohash
def test(inputFile):
    outputfile = file(inputFile+".geohash", 'a')
    lineno = 0
    ISOTIMEFORMAT='%Y-%m-%d %X'    
    for line in open(inputFile):
        lineno += 1
        if lineno % 100 == 0:
            print "process line :",lineno,"    ",time.strftime(ISOTIMEFORMAT,time.localtime())
            sys.stdout.flush()
        xy = line.strip()
        x = float(xy.split(",")[0])
        y = float(xy.split(",")[1])
        geohash = mzgeohash.encode((x,y),length=8) 
        outputfile.write(xy +"\t"+str(geohash )+"\n")

if  __name__=="__main__":
    inputFile = sys.argv[1]
    test(inputFile)

产出文件每行示例:117.445044487,40.0585138025  wx5ebs


附上官方的简明使用示例:

>>> import mzgeohash
>>> mzgeohash.decode('xn76urwe1g9y')
(139.76608408614993, 35.681382017210126)
>>> mzgeohash.encode((139.76608408614993, 35.681382017210126))
'xn76urwe1g9y'
>>> mzgeohash.neighbors('xn76urwe1g9y')
{'c': 'xn76urwe1g9y',
 'e': 'xn76urwe1gdn',
 'n': 'xn76urwe1g9z',
 'ne': 'xn76urwe1gdp',
 'nw': 'xn76urwe1g9x',
 's': 'xn76urwe1g9v',
 'se': 'xn76urwe1gdj',
 'sw': 'xn76urwe1g9t',
 'w': 'xn76urwe1g9w'}



猜你喜欢

转载自blog.csdn.net/u010967382/article/details/74994502
今日推荐