SPARQLing the Highest Point in Every State

In a previous post, I mentioned that it should be pretty easy to use SPARQL to make a map of the highest point in each of the 50 US States. Having written that, I thought I should maybe actually, you know, try it.

The following chunk of code uses ARC2, an rdf/semantic web library for PHP to query the dbpedia endpoint and then put the results on a Google Map.

To try this out, you need to:

  1. Have a functional PHP installation
  2. Download ARC2 into your web path (no setup required)
  3. Set the path to ARC in the code below
  4. Get a Google Maps API Key (free)
  5. Set your API key in the code below
  6. Run

Note that this is a demo and is written to be easy to run – a real application might separate the data logic from the webpage and make more sophisticated use of Javascript/Google Maps API.

<?php
//include ARC2 libraries
include_once("path/to/ARC2.php");
//instantiate a RemoteStore
$config = array('remote_store_endpoint' => 'http://dbpedia.org/sparql');
$store = ARC2::getRemoteStore($config);
//build the SPARQL query
$q = '
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?state ?mtn ?lat ?long
WHERE {
?state skos:subject <http://dbpedia.org/resource/Category:States_of_the_United_States> .
?state dbpedia2:highestpoint ?mtn .
?mtn geo:lat ?lat .
?mtn geo:long ?long
}
';
//process the results
$results = array();
if ($rows = $store->query($q, 'rows')) {
foreach ($rows as $row) {
$state = substr($row['state'], strlen("http://dbpedia.org/resource/"));
$mtn = substr($row['mtn'], strlen("http://dbpedia.org/resource/"));
$lat = $row['lat'];
$lng = $row['long'];
$results[] = array($state, $mtn, $lat, $lng);
}
}
?>
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOUR_KEY"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 3);
map.addControl(new GMapTypeControl());
map.addControl(new GLargeMapControl());
<?php
//populate map with results
foreach($results as $result) {
list($state, $mtn, $lat, $lng) = $result;
echo("map.addOverlay(new GMarker(new GLatLng($lat,$lng), {title: '$mtn'}));n");
}
?>
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 100%; height: 100%"></div>
</body>
</html>

* Using substr() to chop off “http://dbpedia.org/resource/” from the names is probably cheating. I think you’re supposed to use rdfs:label@en instead.

This entry was posted in Programming and tagged , . Bookmark the permalink.

Leave a Reply