XQuery/DBpedia with SPARQL - Stadium locations

From Wikibooks, open books for an open world
Jump to navigation Jump to search

At the risk of being repetitious, here is another script which mashes up data from DBpedia with GoogleMaps, this time to show the location of all venues in a supplied Wikipedia Category of venues.

Examples[edit]

Football Venues in England[edit]

Football Venues in Scotland[edit]


Script[edit]

(: 
   This accepts a category of stadiums and generates a kml map of all stadiums
:)

declare namespace r = "http://www.w3.org/2005/sparql-results#";
declare variable $query := "
PREFIX p: <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#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE
  {?ground skos:subject <http://dbpedia.org/resource/Category:Football_venues_in_England>.
  ?ground geo:long ?long.
  ?ground geo:lat ?lat.
  ?ground rdfs:label ?groundname.
  OPTIONAL {?ground foaf:depiction ?image .}.
  OPTIONAL {?club p:ground ?ground.  ?club rdfs:label ?clubname . FILTER (lang(?clubname) = 'en')}.
  OPTIONAL {?ground foaf:page ?wiki.}.
  FILTER (lang(?groundname) ='en'). 
 } 
"; 

declare function local:execute-sparql($query as xs:string) {
 let $sparql := concat("http://dbpedia.org/sparql?format=xml&amp;default-graph-uri=http://dbpedia.org&amp;query=",
                encode-for-uri($query)
              )
 return doc($sparql)
};

declare function local:sparql-to-tuples($rdfxml) {
  for $result in $rdfxml//r:result
  return
   <tuple>
      { for $binding in $result/r:binding
        return        
         if ($binding/r:uri)
           then  element {$binding/@name} {
                  attribute type {"uri"} , 
                  string($binding/r:uri) 
                }
           else  element {$binding/@name} {
                  attribute type {$binding/@datatype}, 
                  string($binding/r:literal)
                }
       }
   </tuple>
 };

declare option exist:serialize "method=xhtml media-type=application/vnd.google-earth.kml+xml highlight-matches=none"; 

let $category := request:get-parameter("category","Football_venues_in_England")
let $queryx := replace($query,"Football_venues_in_England",$category)
let $result:= local:execute-sparql($queryx)
let $tuples := local:sparql-to-tuples($result)

let $x := response:set-header('Content-disposition','Content-disposition: inline;filename=stadiums.kml;')

return

<Document>
  <name>{replace($category,"_"," ")}</name> 
  <Style id="stadium">
    <IconStyle>
     <Icon><href>http://maps.google.com/mapfiles/kml/shapes/ranger_station.png</href>
    </Icon>
    </IconStyle>
  </Style>
   {
   for $groundid in distinct-values($tuples/ground)
   let $groundTuples := $tuples[ground=$groundid]
   let $ground := $groundTuples [1]
   let $name := string($ground/groundname)
   let $lat := xs:decimal($ground/lat)
   let $long := xs:decimal($ground/long)
   let $clubs := string-join($groundTuples/clubname,", ")
   let $wiki := string($ground/wiki)
   let $description := 
     <div>
     Ground of {$clubs} 
     {if ($ground/image) then (<br/>,<img src="{$ground/image}"/>) else () }
     <br/>
      <a href='{$groundid}'>DBpedia</a>
      <a href='{$wiki}'>Wikipedia</a>   
     <a href="http://images.google.co.uk/images?q=stadium+{$name}">Google Images</a>
    </div>
   return 
   <Placemark>
    <name>{$name}</name>
    <description>
    {util:serialize($description,"method=xhtml")}
     </description>
    <Point> 
    <coordinates>{concat($long, ",",$lat,",0")}</coordinates>
    </Point>
    <styleUrl>#stadium</styleUrl>
   </Placemark>
  } 
</Document>