`123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114` ``````# coding: UTF-8 """ Copyright (C) 2009 Hiroaki Kawai """ try: import _geohash except ImportError: _geohash = None def _encode_i2c(lat,lon,bitlength): digits='0123' r = '' while bitlength>0: r += digits[((lat&1)<<1)+(lon&1)] lat = lat>>1 lon = lon>>1 bitlength -= 1 return r[::-1] def _decode_c2i(treecode): lat = 0 lon = 0 for i in treecode: b = ord(i)-48 lat = (lat<<1)+int(b/2) lon = (lon<<1)+b%2 return (lat,lon,len(treecode)) def encode(lat,lon,precision=12): if _geohash and precision<=64: ints = _geohash.encode_int(lat, lon) ret = "" for intu in ints: for i in range(int(_geohash.intunit/2)): if len(ret) > precision: break ret += "0213"[(intu>>(_geohash.intunit-2-i*2))&0x03] return ret[:precision] b = 1<>bitlength: for tlon in (lon-1, lon, lon+1): r.append(_encode_i2c(tlat, tlon, bitlength)) tlat = lat-1 if tlat>=0: for tlon in (lon-1, lon, lon+1): r.append(_encode_i2c(tlat, tlon, bitlength)) return r def expand(treecode): r = neighbors(treecode) r.append(treecode) return r ``````