Accessing Cross Domain Data with YQL

imageThe same origin policy prevents code from one domain accessing data from a different domain. For a mapping site requests for KML, GeoRSS, WFS services, and some WMS operations are all affected by this policy, and therefore require a range of workarounds, usually involving a proxy.

One solution is the ExtJS ScriptTagProxy that can be used to retrieve data from an external domain. However for this to work the server must return executable JavaScript code. For example to access an external WMS capabilities file you’d need to set up a special handler on your server to wrap the data in JavaScript before being added to your web page. This pattern is referred to as JSONP (JSON with padding).

YQL

Thanks to this Unwritten Guide to Yahoo Query Language it became apparent you can get Yahoo to automatically do this wrapping for you. Whilst using YQL is still technically a proxy, it’s a proxy you don’t have to worry about maintaining.

I’m not a huge fan of relying on commercial APIs and services, partly as if they are free then they can also change without warning or disappear, and partly as fully understanding the Terms of Service requires both a law and computer science degree.

However configuring your own proxy and wrapping is a large overhead if accessing data from another domain is the only reason you need server-side code.

YQL looks a lot like standard SQL, but can be used to query data from a URL. Go to the YQL Console and try out the following query:

select * from xml where url='http://api.geoext.org/1.0/examples/data/wmscap.xml'

An External WMS Capabilities Store

This method can be used to easily access a WMS Capabilities file on an external server. I’ve put a small demo online showing a working example. The source code can be found on BitBucket.

The example is based on GeoExt’s original WMS Capabilities demo. The code makes use of the ScriptTagProxy, and a slightly modified reader that takes the results part of the JSON (the XML) and passes it on to the standard WMSCapabilitiesReader:

GeoExt.data.YahooWMSCapabilitiesReader = Ext.extend(GeoExt.data.WMSCapabilitiesReader, {   
    readRecords: function(data){    
        data = data.results.toString();    
        return GeoExt.data.YahooWMSCapabilitiesReader.superclass.readRecords.call(this, data);    
    }    
});

Yahoo have many “clones” of services offered by the other web giants, which whilst well implemented don’t really interest me – when was the last time you looked at Yahoo Maps?.  However along with YQL, Yahoo have a few interesting and unique developer tools such as Yahoo Pipes and YUI (in fact ExtJS branched out of YUI), so its worth keeping an eye on the often forgotten man of the Internet.



2 views shared on this article. Join in...

  1. Hi. This is a very nice idea. How would you go about extending this to all the other tools: get feat info? or printing? also, wms get map is routed through the proxy when you config one in OpenLayers. Would this work? (I couldn’t see the map in your example)

    Regards,
    Duarte

  2. geographika says:

    Hi Duarte,

    The use case I needed the above code for was for a simple mapping page that needed to add in WMS layers from any remote server.

    It was to install on IIS / Windows, so setting up a Python CGI proxy seemed overkill, and also I’d have to whitelist all sites.

    Its not going to replace all the features of a proxy (though it would be nice to have a plugin that used YQL through Openlayer.Proxyhost), but simplifies some setups.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

You may use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>