Griff
Griff

Reputation: 2124

Query dbpedia asking for birthplace of two categories in SPARQL

I am trying to get a list of:

Person A, BirthPlace of A, Person B, BirthPlace of B

where the birthplace is a city.

The relationship between person A and person B is that person A "influenced" person B. My code so far is as follows:

SELECT ?person ?birthplace ?influenced ?birthplace2
WHERE {
?person a <http://dbpedia.org/ontology/Person> .
?person dbpedia2:placeOfBirth ?birthplace .
?person <http://dbpedia.org/ontology/influenced> ?influenced.
?person dbpedia2:placeOfBirth ?birthplace2 .
?influenced a <http://dbpedia.org/ontology/Person>.
?country rdf:type dbpedia-owl:Country .
FILTER (str(?birthplace2) = str(?country))
FILTER (str(?birthplace) = str(?country))
}

At the moment I am doing country because that is only thing which works.

Currently, this is just giving me two columns with the same birthplace. It isn't showing the influenced person's birthplace.

Ideally I want the latitude and longitude of each birth city as well, such as:

Person A, BirthPlace of A, Lat, Long, Person B, BirthPlace of B, Lat, Long

But this might be too much to ask. Sorry, I'm new to SPARQL.

Upvotes: 3

Views: 1568

Answers (1)

Ian Dickinson
Ian Dickinson

Reputation: 13315

In your query you have:

?person dbpedia2:placeOfBirth ?birthplace .
?person <http://dbpedia.org/ontology/influenced> ?influenced.
?person dbpedia2:placeOfBirth ?birthplace2 .

which says that ?person was born in both ?birthplace and ?birthplace2. I think you meant:

?person dbpedia2:placeOfBirth ?birthplace .
?person <http://dbpedia.org/ontology/influenced> ?influenced.
?influenced dbpedia2:placeOfBirth ?birthplace2 .

As for getting the lat and long of each city, right now dbpedia is down so I can't look at the city resources to see how they map to geo-coordinates. However, once dbpedia is back up, you can do a SPARQL describe query:

describe <http://dbpedia.org/... rest of resource URI>

and see what you get back. That will tell you which predicates you need to use to pull out the location. Beware that if the lat/long information is missing, you won't see that city in the results unless you put the part of the query pattern that selects out the location in a SPARQL optional clause.

Update

OK, DbPedia is back up now. I believe this is what you want:

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>

SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2 
                ?lat1 ?long1 ?lat2 ?long2
WHERE 
{
  ?person1 a dbpedia-owl:Person ;
           dbpedia-owl:birthPlace ?birthplace1 ;
           dbpedia-owl:influenced ?person2 .
  ?person2 dbpedia-owl:birthPlace ?birthplace2 .

  optional {
    ?birthplace1 geo:lat ?lat1 .
    ?birthplace1 geo:long ?long1 .

    ?birthplace2 geo:lat ?lat2 .
    ?birthplace2 geo:long ?long2 .
  }
}

Update 2

The query works for me in dbpedia/iSparql (here's a perma-link):

iSparql results

Update 3

Restricting to only cities:

PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>

SELECT DISTINCT ?person1 ?birthplace1 ?person2 ?birthplace2 
                ?lat1 ?long1 ?lat2 ?long2
WHERE 
{
  ?person1 a dbpedia-owl:Person ;
           dbpedia-owl:birthPlace ?birthplace1 ;
           dbpedia-owl:influenced ?person2 .
  ?person2 dbpedia-owl:birthPlace ?birthplace2 .

  ?birthplace1 a dbpedia-owl:City .
  ?birthplace2 a dbpedia-owl:City .

  optional {
    ?birthplace1 geo:lat ?lat1 .
    ?birthplace1 geo:long ?long1 .

    ?birthplace2 geo:lat ?lat2 .
    ?birthplace2 geo:long ?long2 .
  }
}

Upvotes: 3

Related Questions