Geohashing

djc slais-www at ucl.ac.uk
Wed Apr 29 14:28:54 CEST 2009


Raymond Hettinger wrote:
> import hashlib
> 
> def geohash(latitude, longitude, datedow):
>     '''Compute geohash() in http://xkcd.com/426/
> 
>     >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
>     37.857713 -122.544543
> 
>     '''
>     h = hashlib.md5(datedow).hexdigest()
>     p, q = [('%f' % float.fromhex('0.' + x)) for x in (h[:16], h
> [16:32])]
>     print('%d%s %d%s' % (latitude, p[1:], longitude, q[1:]))
> 
> if __name__ == '__main__':
>     import doctest
>     doctest.testmod()

Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hashlib
>>>
>>> def geohash(latitude, longitude, datedow):
...     '''Compute geohash() in http://xkcd.com/426/
...
...     >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
...     37.857713 -122.544543
...
...     '''
...     h = hashlib.md5(datedow).hexdigest()
...     p, q = [('%f' % float.fromhex('0.' + x)) for x in (h[:16],
h[16:32])]
...     print('%d%s %d%s' % (latitude, p[1:], longitude, q[1:]))
...
>>> if __name__ == '__main__':
...     import doctest
...     doctest.testmod()
...
**********************************************************************
File "__main__", line 4, in __main__.geohash
Failed example:
    geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.5/doctest.py", line 1228, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__.geohash[0]>", line 1
         geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
                                                              ^
     SyntaxError: invalid syntax
**********************************************************************
1 items had failures:
   1 of   1 in __main__.geohash
***Test Failed*** 1 failures.
(1, 1)
>>>


-- 
djc @work



More information about the Python-list mailing list