Skip to content

Instantly share code, notes, and snippets.

@cwalv
Created August 29, 2013 02:16
Show Gist options
  • Save cwalv/6373593 to your computer and use it in GitHub Desktop.
Save cwalv/6373593 to your computer and use it in GitHub Desktop.
python geohash
geoHashCharMap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z']
stdCharMap = [chr(ord('0') + i) for i in range(10)] + [chr(ord('A') + i) for i in range(26)]
geoHashCharToStdCharMap = dict(zip(geoHashCharMap, stdCharMap))
def geoHashToBin(geoHash):
return [bin(int(geoHashCharToStdCharMap[c], 32))[2:].rjust(5, '0')
for c in geoHash]
def geoHashBinToLatLonBin(geoHashBin):
return (''.join(geoHashBin)[1::2], ''.join(geoHashBin)[::2])
def latLonBinToLatLon(latLonBin):
latBinStr, lonBinStr = latLonBin
latBinInt, lonBinInt = int(latBinStr, 2), int(lonBinStr, 2)
return (latBinInt/float(2**len(latBinStr)) * 180.0 - 90,
lonBinInt/float(2**len(lonBinStr)) * 360.0 - 180.0)
def geoHashToLatLon(geoHash):
return latLonBinToLatLon(geoHashBinToLatLonBin(geoHashToBin(geoHash)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment