Skip to content

Commit 353d22f

Browse files
authored
Create geohash-ii.py
1 parent 3dee923 commit 353d22f

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

geohash-ii.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class GeoHash:
2+
"""
3+
@param: geohash: geohash a base32 string
4+
@return: latitude and longitude a location coordinate pair
5+
"""
6+
def decode(self, geohash):
7+
# write your code here
8+
code = self.b32decode(geohash)
9+
longs, lats = [-180, 180], [-90, 90]
10+
longtitude, latitude = 0, 0
11+
prec = len(code)
12+
for i in range(0, prec, 2):
13+
if code[i] == '0':
14+
longs[1] = longtitude
15+
else:
16+
longs[0] = longtitude
17+
longtitude = (longs[0] + longs[1]) / 2
18+
if (i + 1) >= prec:
19+
break
20+
if code[i + 1] == '0':
21+
lats[1] = latitude
22+
else:
23+
lats[0] = latitude
24+
latitude = (lats[0] + lats[1]) / 2
25+
return (latitude, longtitude)
26+
27+
def b32decode(self, geohash):
28+
r = ''
29+
bcode = {'0': '00000', '1': '00001', '2': '00010', '3': '00011',
30+
'4': '00100', '5': '00101', '6': '00110', '7': '00111',
31+
'8': '01000', '9': '01001', 'b': '01010', 'c': '01011',
32+
'd': '01100', 'e': '01101', 'f': '01110', 'g': '01111',
33+
'h': '10000', 'j': '10001', 'k': '10010', 'm': '10011',
34+
'n': '10100', 'p': '10101', 'q': '10110', 'r': '10111',
35+
's': '11000', 't': '11001', 'u': '11010', 'v': '11011',
36+
'w': '11100', 'x': '11101', 'y': '11110', 'z': '11111'}
37+
for c in geohash:
38+
r += bcode[c]
39+
return r

0 commit comments

Comments
 (0)