Implementations

From Geohashing
Revision as of 16:22, 31 May 2008 by 90.198.176.206 (talk) (Lazy Geohasher)

If you're looking to code your own utilities, you will need a source that provides the Dow's opening price for any given day. Details on such services are available at Dow Jones Industrial Average.

Official Implementations

  • Note: There is some confusion as to how the official implementations handle the -0 Issue.

Interactive Coordinate Calculator

This implementation IS FULLY 30W-compliant.

The original sample interactive coordinate calculator can be found at http://irc.peeron.com/xkcd/map. The common URL, http://xkcd.com/geohashing, redirects here. Any problems or issues should be mentioned to Zigdon either on the wiki or in the [#geohashing channel on irc.foonetic.net].

Reference Implementation

This implementation IS FULLY 30W-compliant.

A reference implementation is available:

view download

It cannot automatically apply the 30W rule, as it doesn't take your location into consideration. If you're East of 30W, call it with the -e option and it will yield the correct results.

Any problems or issues should be mentioned to Zigdon either on the wiki or in the [#geohashing channel on irc.foonetic.net].

MediaWiki Implementation

This implementation's 30W-compliance is tied to that of another implementation. See notes.

MediaWiki half-implementation (for use by template creators).

Python Implementations

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://irc.peeron.com/xkcd/map/data/%Y/%m/%d")).read()
if djia.find('404 Not Found') >= 0: sys.exit(sys.stderr.write("Dow Jones not available yet.\n"))
sum = hashlib.md5("%s-%s" % (date, djia)).digest()
print struct.unpack(">Q", sum[0:8])[0] / (2.**64), struct.unpack(">Q", sum[8:16])[0] / (2.**64)
  • 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 deprected in favor of hashlib since python 2.5, if you are using python 2.4 or earlier, substitude hashlib for md5.

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://irc.peeron.com/xkcd/map/data/%Y/%m/%d")).read()
if djia.find('404 Not Found') >= 0: sys.exit(sys.stderr.write("Dow Jones not available yet.\n"))
sum = hashlib.md5("%s-%s" % (date, djia)).digest()
n = str(south * (struct.unpack(">Q", sum[0:8])[0] / (2.**64) + abs(args[1])))
w = str(west * (struct.unpack(">Q", sum[8:16])[0] / (2.**64) + abs(args[2])))
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 package xkcd.geohash

This implementation IS NOT 30W-compliant.

There is also a Python package xkcd.geohash which is independent of the interactive coordinate calculator (but uses Google Finance as well).

RESTful Python implementation with Atom feed

This implementation IS FULLY 30W-compliant.

This implementation uses web.py to give simple, clean URLs.

The source for the Atom feed is available through anonymous svn here: https://staticfree.info/svn/ghfeed/ To contribute to it, please contact xxv and he can set you up with commit access.

Shell Script Implementation

This implementation IS FULLY 30W-compliant.
#!/bin/sh -e

[ "$2" -gt -30 ] && { date -R >/dev/null 2>&1 && y='-d -1day' || y='-v -1d'; }
command -v md5 >/dev/null 2>&1 || md5() { md5sum | cut -d ' ' -f 1; } 
command -v GET >/dev/null 2>&1 || GET() { wget -q -O - "$@"; }

d=$(date +%F)-$(GET http://irc.peeron.com/xkcd/map/data/$(date $y +%Y/%m/%d))
( echo 16i; echo -n $d | md5 | sed 's/.\{16\}/0.&p/g' | tr a-f A-F ) | dc \
| while read f; do echo $1$f; shift; done

Pass your graticule's lat and long as the arguments to the script. If you do not have libwww-perl's GET available, fetch may be substituted for wget (with the appropriate options), but curl may not, as it does not return a failure exit status when the day's data is not available yet.

On Mac OS-X, I had some problems with sh's math not coming out right, so here's a derivative script converted to bash, with a couple of "features":

This implementation IS FULLY 30W-compliant.
#!/bin/bash -e

if [ $# -lt 2 ] ; then  # Put your default geo-coords here
    : ${LAT:="36"}
    : ${LON:="-122"}
else
    : ${LAT:="$1"}
    : ${LON:="$2"}
fi

showGeoHash()
{
    echo "${LAT}${1} ${LON}${2}"
    echo "http://maps.google.com/maps?q=${LAT}${1}+${LON}${2}"
    open "http://maps.google.com/maps?q=${LAT}${1}+${LON}${2}"      # Comment-out this line for other-than Mac OS-X
}

[ "$LON" -gt -30 ] && { date -R >/dev/null 2>&1 && y='-d -1day' || y='-v -1d'; }
command -v md5 >/dev/null 2>&1 || md5() { md5sum | cut -d ' ' -f 1; } 
command -v GET >/dev/null 2>&1 || GET() { wget -q -O - "$@"; }

d=$(date +%F)-$(GET http://irc.peeron.com/xkcd/map/data/$(date $y +%Y/%m/%d))
showGeoHash `( echo 16i; echo -n $d | md5 | sed 's/.\{16\}/0.&p/g' | tr a-f A-F ) | dc`

Alternative geohash location suggester

This implementation IS FULLY 30W-compliant.

Geohash Recommender

Yet another implementation in Python is here as well. It does the same as the ones above but also gives "validation" information. Lets you know if the geohash location is under water and thus if its a valid place to go party. Using this it attempts to suggest the "best" alternative geohash by checking all neighbouring grid locations. I'll add more terrain recognition if people like it. A webservice to access it can be found JSON Web Service. The code is over here Source Code

Bugs

  • There are a few, it needs a few refreshes to get the whole tagging right. It also has trouble guessing the terrain type occasionally. Refreshing it makes it work but its a problem im looking into, have a look at the code and help if you can :)
  • Gives me a false location for May 28 and 29, 2008, which aren't yet known anywhere in the world according to the algorithm. --Tim P 21:29, 24 May 2008 (UTC)
    • But... thos dates aren't real yet. There is no dow result. Granted it should give an error instead of giving _something_ but...
      • As of 05:06, 26 May 2008 (UTC), this gives locations for May 27-30 west of 30W and for May 28-31 east of 30W, i.e., four days beyond what should be calculable as of that time. Beyond those dates, it gives a proper error message stating that no Dow is available. I think temporary data were added to the implementation for testing purposes; these should be removed. --Tim P
      • One could interpret the algorithm such that, for any date in the future, one is to use "the opening price from [DOW's] previous day of active trading is used", i.e., the most recent trading day. Obviously, this would lead to the location changing as the day approaches but, in a random spontaneous adventure generator, that seems almost mathematically pure. I mean, that's just what happens when you try to generate spontaneity too far into the future, yes?  ;) Ted 00:19, 30 May 2008 (UTC)

Comic Creator

This implementation IS FULLY 30W-compliant.

A python class to recreate the original comic, but for any given date with your own coordinates! Below shows the comic for the first North East Netherlands meetup. Although it fetches the Dow itself, you can add &dowjones=12345.67 if it doesn't do so correctly (e.g., peeron.com is down). Created by Hugo.

http://www.astro.rug.nl/~buddel/cgi-bin/geohashingcomic/geohashingcomic.cgi?year=2008&month=5&day=24&lat=55.218512&lon=6.566854

You can download the script as well if you wish.

In the image, Dow values less than 10,000.00 are padded with leading spaces, however in the algorithm it is not so it is compliant with other the algorithms. As Tim P suggested, the comic is akin to all the bank cheques with "19__" printed on them being used in 2000. The "form" has 5+2 "boxes" because that's how Dow prices are now, but that doesn't mean it can't be different.

Bugs

  • The font sizes are not exactly as the original and there can still be some alignment problems.
  • The -0 issue is ignored. Only if you enter -0 as an integer you get an incorrect outcome. You are supposed to enter your own location as a float, up to 6 decimals, everything is okay even when you enter -0.0.

Semi-manual offline calculator

This implementation IS FULLY 30W-compliant.

If you're going to be away from internet access, you'll need to compute the hash locally:

Notes

  • All you need is a Javascript-enabled browser. Tested in Firefox so far.
  • You will have to somehow acquire the Dow's opening value for the appropriate date; there may be a service to do this.
    • Remember, if you're east of 30W, you should use the previous day's opening price, even if a new one becomes available later in the day.
  • The date is filled in automatically using the current timestamp.
  • You'll have to combine the computed fractional coordinates with your graticule coordinates yourself.
  • I grabbed MD5 and base conversion code from random internet sites.

Text Messaging (email) service

This implementation IS FULLY 30W-compliant.

If your cell phone supports text messaging to an email address, you can get geohash updates on your phone by sending a formatted text to srv@geohash.info

Formatting

YYYY-MM-DD Lat Lon Example:

2008-02-18 42 -83

Alternatively, it defaults to the current date (EST for now) and you can just enter Latitude and Longitude

42 -83

Bugs

  • The -0 Issue is still very much an issue.
  • This used to display the degree symbol after coordinate elements, but due to poor cell phone support of this character, which resulted in messages cutting off after the first Latitude, I have removed it.
  • This is also not a terribly robust solution, but a better (stronger, faster) implementation is in the making.

Feeds

GeoRSS feed

This implementation IS NOT 30W-compliant.

You can subscribe to a feed (in GeoRSS format) that will give you updates on a daily basis[citation needed]. Updates include the "best guess" address of the location as well as the specific latitude/longitude coordinates.

http://atlas.freshlogicstudios.com/Features/Experiments/Xkcd/Xkcd.ashx?Latitude=38&Longitude=-91

For example, here's the GeoRSS geohash for St. Louis.

This GeoRSS feed is used to power the Atlas geohash implementation.

Atom feed

This implementation IS FULLY 30W-compliant.

You can subscribe to a feed that will give you updates on a daily basis:

http://staticfree.info/geohash/atom/LAT,LON

For example, here's the Atom Geohash for Boston.

Just put your coordinates in, subscribe and you'll be ready to go!

Source is available from subversion: https://staticfree.info/svn/ghfeed/

Atom Feed Bugs

  • I noticed yesterday that before the stock exchange data becomes available, the atom feed creates a kind of 'intemediary' geohash using todays date and yesterday's data. This might be intentional, but personally I find that undesirable on weekdays. Could it be modified to show yesterday's date and geohash until data is available? Nicktaylor 14:04, 23 May 2008 (UTC)
    • It's definitely broken. -- Phyzome 11:37, 28 May 2008 (UTC)
      • This implementation was written before the 30W rule. As it turns out, the intermediate hash falls back on the previous Dow while using the current date from 00:00 (local) to 09:30 ET, which happens to be the desired 30W behavior. However, this is not helpful west of 30W. An inverse problem occurs after 09:30 USET east of 30W, until 24:00 local. See detailed discussion at Talk:Implementations#Atom feed 30W-compliance. --Tim P 15:45, 29 May 2008 (UTC)

geohash.info

This implementation IS FULLY 30W-compliant.

Subscribe to a feed that updates as soon as each day's meetup is calculable for your graticule:

http://www.geohash.info/srv/feed.php?lat=LAT&lon=LON

If you visit the URL with no parameters, you will be prompted for your graticule coordinates.

Quirks

  • The script estimates your timezone based upon your longitude, and uses -04:30 as the timezone of the Dow. Therefore, the update times may be off by as much as an hour or two. Shouldn't be an issue, since they'll be around midnight for most people.
  • For non-30W users, the feed will be empty between midnight and 9:30 AM -- this should be acceptable, since most feed readers keep entries that have dropped off the end of the feed. (This behavior may be changed in the future.)

k4 implementation

This implementation IS NOT 30W-compliant.

Here's an implementation in k4, a vector-programming (i.e., self-obfuscating) language that seems to me to be very true to the spirit of xkcd. The following shell script takes the graticule as an argument, uses standard standard unix tools to retrieve the current Dow opening price, and returns a URL for a Google map to the the upcoming Saturday's meetup. It assumes the q executable is in your PATH.

#!/bin/sh

test $# -eq 2 || exit 1
dow=`curl -s 'http://finance.google.com/finance?cid=983582'| \
 egrep -o '<span id="ref_983582_op">[^<]+</span>'| \
 sed -r 's/.*>(.*)<.*/\1/'|tr -d ,`

q -lat $1 -lon $2 -dow $dow<<"EOF"
\
(.q.set').(!:;,/)@\:.Q.opt .z.x;
h:.Q.n,6#.Q.A;c:16 xexp'-:'1+!16
u:"http://maps.google.com/maps?q="
d:.q.ssr[$.z.D+7-.q.mod[.z.D]7;".";"-"]
s:0N 16#,/$-15!"-"/:(d;dow);l:(lat;lon)
-1@u,"+"/:l,'1_'$sum'(h?/:.q.upper s)*\:c;
\\
EOF

For example, here's New York as of 2008-05-23:

% gh 40 -74
http://maps.google.com/maps?q=40.126648+-74.5475331

N.B., this was written on a Friday; I make no guarantee that it will produce useful results when run on any other day of the week (though Saturdays will probably work).

Adavies42 20:50, 23 May 2008 (UTC)

Automatic GPX File Generator

This implementation IS NOT 30W-compliant.

I have modified the sample perl implementation so that it automatically generates a .gpx file which you can upload to your favourite GPS device/software. Download here Before you use the script, you need to modify it and put your own lat/lon values into the corresponding variables at the beginning. ~~Wansti

GeohashGenerator for Popfly

This implementation IS FULLY 30W-compliant.

I have created a Popfly block to use for creating mashups at http://www.popfly.com/users/rbuckton/GeohashGenerator. Can be integrated with Virtual Earth, etc.

rbuckton

Lazy Geohasher

This implementation IS PARTIALLY 30W-compliant.

If you'd rather just stay at home and wait for a geohash to come to you, there's a perl script which will search for combinations of times and DOW Jones opening prices to find when you could win the much coveted Couch Potato Geohash or Cubicle Geohash award. Just put in the ranges of values for the stock market and the time you want to wait for, and watch those CPU cycles fly! -- thaniel.drake@gmail.com.

30w compliance statement: This code does not use live data. Users are expected to determine from the date of the Geohash which dates opening value is relevant.

Disclaimer: The author humbly requests that you do not manipulate the Dow Jones Industrial Average to ensure that a geohash collides with your address. The author accepts no responsibility for any resulting financial instability you cause by such manipulations.