Skip to content

Instantly share code, notes, and snippets.

@RicSwirrl
Created January 19, 2012 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RicSwirrl/1639924 to your computer and use it in GitHub Desktop.
Save RicSwirrl/1639924 to your computer and use it in GitHub Desktop.
getLsoaData()
// get the LSOA data from the database.
var getLsoaData = function(tiles) {
// define this inside this closure - it's not useful outside the scope of this func
var buildSparql = function(tile) {
var lowerLat = tile[0][0];
var lowerLong = tile[0][1];
var upperLat = tile[1][0];
var upperLong = tile[1][1];
var sparql = "PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> " +
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
"SELECT ?lsoa ?notation ?label ?lat ?long ?score" +
" WHERE { " +
" GRAPH <http://opendatacommunities.org/id/graph/geography/lsoa> { " +
" ?lsoa a <http://opendatacommunities.org/def/geography#LSOA> . " +
" ?lsoa geo:lat ?lat . " +
" ?lsoa geo:long ?long . " +
" ?lsoa <http://www.w3.org/2004/02/skos/core#notation> ?notation . " +
" ?lsoa rdfs:label ?label . " +
" } " +
" GRAPH <http://opendatacommunities.org/id/graph/IMD/2010/" + scoreDomain + "> { " +
" ?obs <http://purl.org/linked-data/sdmx/2009/dimension#refArea> ?lsoa . " +
" ?obs <http://opendatacommunities.org/def/IMD#" + scoreDomain + "> ?score . " +
" } " +
" FILTER ( ?lat >= " + lowerLat + " && " +
" ?lat < " + upperLat + " && " +
" ?long >= " + lowerLong + " && " +
" ?long < " + upperLong + " ) . " +
"}";
return sparql;
};
var noOfTiles = tiles.length;
// nothing to do.
if(noOfTiles == 0) {
$(self).trigger('lsoaDataRetrieved');
}
var tilesRetrieved = 0;
var page = 1;
var pageSize = 1000;
var callAjaxSparqlPaging = function(sparql, tile) {
var queryUrl = "http://opendatacommunities.org/sparql.json?_page=" + page.toString() + "&_per_page=" + pageSize.toString() + "&query=" + encodeURIComponent(sparql);
window.swirrl.log('about to call', queryUrl);
$.ajax(
queryUrl,
{
success: function(data, textStatus, jqXHR) {
var pageOfData = data.results.bindings;
$.each(pageOfData, function(idx, el){
var data = {
uri: el.lsoa['value'],
label: el.label['value'],
lat: parseFloat(el.lat['value']),
lng: parseFloat(el['long']['value']),
score: parseFloat(el.score['value'])
}
var lsoaNotation = el.notation['value'];
setLsoaData(tile, lsoaNotation, data);
});
if (pageOfData.length == pageSize) {
// this page was full. There might be more.
page += 1;
callAjaxSparqlPaging(sparql);
} else {
// no more pages.
tilesRetrieved+=1;
if (tilesRetrieved == tiles.length) {
// we've got all the lsoaData.
$(self).trigger('lsoaDataRetrieved');
}
}
},
error: function(jqXHR, textStatus, errorThrown) {
window.swirrl.log("SPARQL Fail: " + errorThrown + " " + textStatus);
$(self).trigger('dataError');
},
dataType: 'json',
timeout: 10000 // timeout after a short time.
}
);
};
// for each tile, get all the pages of data.
$.each(tiles, function(i, tile){
var sparql = buildSparql(tile);
callAjaxSparqlPaging(sparql, tile);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment