Skip to content

Instantly share code, notes, and snippets.

@fabiovalse
Last active August 29, 2015 14:02
Show Gist options
  • Save fabiovalse/69ce56a3c76be8fe9820 to your computer and use it in GitHub Desktop.
Save fabiovalse/69ce56a3c76be8fe9820 to your computer and use it in GitHub Desktop.
LOD CNR MI TUTORIAL - Clavius Example 2

The aim of this gist is to show how to handle the execution of simple SPARQL query over the DBpedia SPARQL endpoint using Javascript, JQuery and AJAX.

Christopher Clavius (25 March 1538 – 6 February 1612) was a German Jesuit mathematician and astronomer who was the main architect of the modern Gregorian calendar. In his last years he was probably the most respected astronomer in Europe and his textbooks were used for astronomical education for over fifty years in and even out of Europe.

The following code shows how to create a Google Maps containg the events occurred during the life of Clavius. On clicking the markers an infowindow opens showing the name of the event.

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="LOD CNR MI TUTORIAL - Clavius Example 2" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?libraries=geometry,visualization&amp;sensor=true"></script>
<script type="text/javascript" src="index.js"></script>
<title>Clavius Time Events</title>
</head>
<body onload="init()">
<div>
<input type="button" onclick="retrieveData()" value="Execute Query">
</div>
<div class="tab" id="map-canvas" style="height: 600px"></div>
</body>
</html>
var map;
var markers = [];
var infowindow;
function retrieveData() {
var query = "PREFIX : <http://dbpedia.org/resource/> PREFIX dbp: <http://dbpedia.org/ontology/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX dbpprop: <http://dbpedia.org/property/> SELECT ?event ?date ?place ?lat_lng WHERE { ?event rdf:type dbp:Event . OPTIONAL { ?event dbp:place ?place . ?place grs:point ?lat_lng } OPTIONAL { ?event dbp:date ?date .} FILTER ( ?date >= '1538-03-25'^^xsd:date && ?date < '1612-02-06'^^xsd:date )} GROUP BY ?event ?date ?place";
var url = 'http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=' + encodeURIComponent(query) + '&output=json';
console.log(url);
$.ajax({
url: url,
dataType: "json",
success: function (data) {
$('#raw_output').text(JSON.stringify(data, null, 3));
deleteMarkers();
show_events(data["results"]["bindings"]);
},
error: function(e) {}
});
}
function init() {
var mapOptions = {
center: new google.maps.LatLng(0.0, 0.0),
minZoom: 2,
zoom: 2,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
infowindow = new google.maps.InfoWindow();
}
function show_events(data) {
infowindow = new google.maps.InfoWindow();
for (i = 0; i < data.length; i++) {
if (data[i]["lat_lng"] != null) {
var lat_lng = data[i]["lat_lng"]["value"];
var array = lat_lng.split(" ");
var title = data[i]["event"]["value"].split("/");
title = title[4].replace(/_/g, "");
var latlng = new google.maps.LatLng(array[0], array[1]);
var content = data[i]["event"]["value"].split("/")[4].replace(/_/g, " ");
var marker = new google.maps.Marker({
position: latlng,
map: map,
content: content
})
markers.push(marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, this);
infowindow.setContent(this.content);
});
}
}
}
function deleteMarkers() {
setAllMap(null);
markers = [];
}
function setAllMap(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?event ?date ?place ?lat_lng
WHERE {
?event rdf:type dbp:Event .
OPTIONAL {
?event dbp:place ?place .
?place grs:point ?lat_lng
}
OPTIONAL { ?event dbp:date ?date .}
FILTER ( ?date >= '1538-03-25'^^xsd:date && ?date < '1612-02-06'^^xsd:date )
}
GROUP BY ?event ?date ?place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment