Implementations/Libraries/Python

From Geohashing

Python

Python Implementation 1

This implementation IS FULLY 30W-compliant.
import hashlib, datetime, struct, urllib, re, sys
date = datetime.date.today()
djia = urllib.urlopen((date - datetime.timedelta(w30)).strftime("http://carabiner.peeron.com/xkcd/map/data/%Y/%m/%d")).read()
if '404 Not Found' in djia: sys.exit(sys.stderr.write("Dow Jones not available yet.\n"))
sum = hashlib.md5("%s-%s" % (date, djia)).digest()
lat, lon = [x/2.**64 for x in struct.unpack_from(">QQ", sum)]; print lat, lon
  • In the above, w30 on line 3 should be replaced by 1 if you're east of -30°, and with 0 otherwise.
  • As md5 has been deprecated in favor of hashlib since python 2.5, if you are using python 2.4 or earlier, substitute hashlib for md5.
  • To generate globalhashes, set w30=1 and multiply the final lat, lon values as indicated on that page.

Python Implementation 2

This implementation IS FULLY 30W-compliant.
import hashlib, datetime, struct, urllib, re, sys, webbrowser
myLat = 34
myLon = -87
args = sys.argv
if len(args) == 1:
    args.append(myLat)
    args.append(myLon)
else:
    args[1] = int(args[1])
    args[2] = int(args[2])
if args[2] < -30:
    td30 = 0
else:
    td30 = 1
if args[1] < 0:
    south = -1
else:
    south = 1
if args[2] < 0:
    west = -1
else:
    west = 1
date = datetime.date.today()
djia = urllib.urlopen((date - datetime.timedelta(td30)).strftime("http://carabiner.peeron.com/xkcd/map/data/%Y/%m/%d")).read()
if '404 Not Found' in djia: sys.exit(sys.stderr.write("Dow Jones not available yet.\n"))
sum = hashlib.md5("%s-%s" % (date, djia)).digest()
n, w = [str(d*(abs(a)+f)) for d, f, a in zip((south, west),
    [x/2.**64 for x in struct.unpack_from(">QQ", sum)], args[1:])]
print n, w
dest = 'http://maps.google.com/maps?f=q&hl=en&geocode=&q=' + n + '+' + w + '&ie=UTF8&ll=' + n + ',' + w + '&spn=0.918082,1,864929&z=9&iwloc=addr'
print dest
webbrowser.open(dest)
  • The above code is based on the earlier sample code, and is subject to the same provisos regarding md5 and hashlib.
  • You can set the myLat and myLon variables on lines 2 and 3 to your latitude and longitude, and invoke the code directly.
  • You can also save the code to a module (e.g., geohash.py) and invoke it like this: python geohash.py 34 -87 (substituting my example with the latitude and longitude of your choice).

Python Implementation 2 (Python 3 Update)

This implementation IS FULLY 30W-compliant.
  • This is simply a conversion of the above code to Python 3.
  • Substitute your own latitude and longitude in the myLat and myLong variables.
import hashlib, datetime, struct, sys, webbrowser, re
from urllib.request import urlopen
from urllib.error import HTTPError

myLat = 34
myLon = -82
args = sys.argv
if len(args) == 1:
    args.append(myLat)
    args.append(myLon)
else:
    args[1] = int(args[1])
    args[2] = int(args[2])
if args[2] < -30:
    td30 = 0
else:
    td30 = 1
if args[1] < 0:
    south = -1
else:
    south = 1
if args[2] < 0:
    west = -1
else:
    west = 1
date = datetime.date.today()
try:
    djia = urlopen((date - datetime.timedelta(td30)).strftime("http://carabiner.peeron.com/xkcd/map/data/%Y/%m/%d")).read().decode('utf-8')
except HTTPError:
    sys.exit(sys.stderr.write('Dow Jones not available yet.\n'))
sum = hashlib.md5(bytes('{0}-{1}'.format(date,djia), 'utf-8')).digest()
n, w = [str(d*(abs(a)+f)) for d, f, a in zip((south, west),
    [x/2.**64 for x in struct.unpack_from(">QQ", sum)], args[1:])]
print(n, w)
dest = 'http://maps.google.com/maps?f=q&hl=en&geocode=&q=' + n + '+' + w + '&ie=UTF8&ll=' + n + ',' + w + '&spn=0.918082,1,864929&z=9&iwloc=addr'
print(dest)
webbrowser.open(dest)