sparqlgeosparql

SPARQL get dbpedia ressources by category


I'm a beginner in SPARQL. And I'm having problems to get the latitude and longitude of all university by city on DBpedia.

I tried multiple things without success. This page shows the universities of Paris on the dbo:campus property, so I like to get the list of the universities with this property and after that get the geographics coordinates.

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name, ?univ, ?lat, ?long WHERE {
    ?p rdf:type dbo:Place.
    ?p rdfs:label ?name.
    ?p dbo:campus ?u.
    ?u geo:lat ?lat.
    ?u geo:long ?long.
    ?u rdfs:label ?univ
    FILTER(LANG(?name) = "en").
    FILTER(?name = "Paris")
}

I check this post DBpedia SPARQL Query US Universities but it doesn't work with another country.


Solution

    1. If you read "is SOME_PROPERTY of"on a rendered DBpedia page, this means the inverse direction, i.e., it shows the triple in its inverted form. Thus, you have to invert the triple pattern in the SPARQL query. For your example, it means that universities are the subject and Paris the object:

      ?u dbo:campus ?p

    2. The labels are language tagged in DBpedia; thus, FILTER(?name = "Paris") is not enough. Adding the English language tag helps:

      FILTER(?name = "Paris"@en)

    A working query would be

    PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX dbo: <http://dbpedia.org/ontology/>
    SELECT ?name, ?univ WHERE {
        ?p rdf:type dbo:Place.
        ?p rdfs:label ?name.
        ?u dbo:campus ?p.
        ?u geo:lat ?lat.
        ?u geo:long ?long.
        ?u rdfs:label ?univ
        FILTER(LANG(?name) = "en").
        FILTER(?name = "Paris"@en)
    }
    

    Some comments:

    A "better" query could be:

    PREFIX  geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
    PREFIX  dbo:  <http://dbpedia.org/ontology/>
    PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX  dbr:  <http://dbpedia.org/resource/>
    
    SELECT  ?name ?univ ?lat ?long
    WHERE
      { VALUES ?p { dbo:Paris }
        ?p  rdfs:label  ?name .
        ?u  dbo:campus  ?p ;
            geo:lat     ?lat ;
            geo:long    ?long ;
            rdfs:label  ?univ
        FILTER langMatches(lang(?name), "en")
      }