GeoJSON Data URIs

In a previous post I described how I was getting the geographic context for a web page from an alternate JSON representation. It was pointed out that this requires an extra request for what could be a fairly small bit of data. Via Sam Ruby, I see that I can head off the extra request and yet keep my links with a Data URI from RFC 2397. Instead of

<link type="application/json" href="http://example.com/where"/>

where

$ curl http://example.com/where
{"type": "Point", "coordinates": [0.0, 0.0]}

One can encode location directly in a URI using escaped GeoJSON:

<link href="data:application/json,%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D"/>

Andrew Turner came up with something like this in an old GeoJSON discussion, but none of us thought of Data URIs.

A round trip through the encoding specified in RFC 2397 is simple with Python:

>>> from urllib import quote, unquote
>>> from json import dumps, loads # or simplejson
>>> where = {'type': 'Point', 'coordinates': (0.0, 0.0)}
>>> json = dumps(where)
>>> json
'{"type": "Point", "coordinates": [0.0, 0.0]}'
>>> uri = 'data:application/json,' + quote(json)
>>> uri
'data:application/json,%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D'
>>> data = uri.split(',')[-1]
>>> data
'%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D'
>>> json = unquote(data)
>>> json
'{"type": "Point", "coordinates": [0.0, 0.0]}'
>>> where = loads(json)
>>> where
{'type': 'Point', 'coordinates': [0.0, 0.0]}

and a Data URI is similarly easy to handle with Javascript:

>>> uri = 'data:application/json,%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D'
"data:application/json,%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D"
>>> data = uri.split(',').pop()
"%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D"
>>> json = unescape(data)
"{"type": "Point", "coordinates": [0.0, 0.0]}"

The missing piece is a link relation type that specifies what such a Data URI means in the context of the document. Something like rel="where" [where-link-relation-type]:

<link rel="where" href="data:application/json,..."/>
%7B%22type%22%3A%20%22Point%22%2C%20%22coordinates%22%3A%20%5B0.0%2C%200.0%5D%7D"/>