OpenLayers constrained by hypertext

I go on at times about REST and its hypertext constraint. I'm sure some people wonder if I ever actually apply these principles or just like to argue about them. Much of my work involves Plone, which is a poor environment for doing things in a RESTful manner, but I try to find good patterns where I can. One of them involves OpenLayers, puts the REST hypertext constraint in a familiar context, and isn't something that I've seen in the OpenLayers examples or gallery.

The site I'm working on has resources about some places of the ancient world, such as the the place known as Aphrodisias. The URI http://pleiades.stoa.org/places/638753 leads to an HTML representation. There are also Atom, JSON, and KML representations, and these are linked from the HTML page as shown below:

<link rel="alternate"
    href="http://pleiades.stoa.org/places/638753#this" />
<link rel="alternate" type="application/atom+xml"
    href="http://pleiades.stoa.org/places/638753/atom" />
<link rel="alternate" type="application/json"
    href="http://pleiades.stoa.org/places/638753/@@json?sm=1" />
<link rel="alternate"
    type="application/vnd.google-earth.kml+xml"
    href="http://pleiades.stoa.org/places/638753/kml" />

The HTML page has an OpenLayers map of ancient world features contained in this place. You'll see one, the feature digitized from the Barrington Atlas map sheet, unless you're a authenticated user that can view Tom Elliot's unpublished features. Previous incarnations of these map pages called on dynamically generated scripts that contained the data, but I'm preferring now to get data from the alternate application/json representation of the place resource. It's trivial, more loosely coupling, and more scalable (at least in my Plone setup) to separate code and data, parsing the JSON link href out of the document and using it in an OpenLayers "GML" layer:

function getJSON() {
    var documentNode = document;
    var linkNode = documentNode.evaluate(
                    '//link[@rel="alternate" and @type="application/json"]',
                    documentNode,
                    null,
                    XPathResult.FIRST_ORDERED_NODE_TYPE,
                    null
                    ).singleNodeValue;
    var jsonURI = linkNode.getAttribute("href");
    return jsonURI;
}

function initMap() {
    var jsonURI = getJSON();
    vectors = new OpenLayers.Layer.GML(
                "Place",
                jsonURI,
                {format: OpenLayers.Format.GeoJSON}
                );

All data the OpenLayers code needs to render a map of the place is now discoverable through HTML links without having to go out of band.

Comments

Re: OpenLayers constrained by hypertext

Author: Vish

Hi Sean,

So, does that mean that you are pulling down the same data from the server to the browser twice? First in HTML and then JSON?

Thank You,

Vish

Re: OpenLayers constrained by hypertext

Author: Sean

The same data twice? No. There's a small overlap between the text/html and application/json representations, yes. A title string, an id, a URL. 100 bytes or so. There's no geometry or coordinates in the HTML page, and so the fractional overlap diminishes as the number of line and polygon features increases.

Re: OpenLayers constrained by hypertext

Author: Vish

Hi Sean,

I was just curious as to what your HTML representation looked like. Also, not to pick on things here, i am just thinking out loud...

1) Does your HTML representation not contain any attribute information about the feature or are u omitting it along with the geometry?

2) What are your thoughts on omitting the geometry for the feature in its HTML representation? Should it be in there as SVG/VML? Is it alright to have only subsets of the feature info in the different format? Or should they all have the same info?

3) If your HTML only contains the 100 bytes of info you mention... then why have a HTML representation at all? REST doesn't say anything about HTTP/HTML. Is it only for the web crawlers?

Thank You,

Vish

Re: OpenLayers constrained by hypertext

Author: Sean

Vish, I meant 100 bytes of overlap between the HTML and JSON representations. How much the representations overlap is totally up to the application (and me). Currently, the JSON representation is primarily in the service of the HTML pages. If users wanted richer JSON data, we'd consider providing it.