I am using sparql to find a location of entity. I have urls from dbpedia-spootlight and want to find location for them. So the query I am using is:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT DISTINCT *
WHERE { ?uri rdfs:label ?label .
OPTIONAL { ?uri geo:lat ?lat . ?uri geo:long ?long } .
OPTIONAL { ?uri dbpedia-owl:country ?dbpediaContry . ?dbpediaContry dbpprop:cctld ?ccTLD } .
FILTER (?uri = <URL> && lang(?label) = "en" ) }
and it was fine until I have got this url: http://dbpedia.org/resource/Valencia,_Spain .
It has wikiPageRedirects to http://dbpedia.org/resource/Valencia and no other data.
I have got lost how could i build query to check cases with redirects.
Can anybody help me?
Try changing the first part of your query to use a UNION
e.g.
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT *
WHERE
{
{
?uri rdfs:label ?label .
}
UNION
{
[] dbo:wikiPageRedirects ?uri .
?uri rdfs:label ?label .
}
OPTIONAL
{
?uri geo:lat ?lat ;
geo:long ?long
}
OPTIONAL
{
?uri dbo:country ?dbpediaContry .
?dbpediaContry dbp:cctld ?ccTLD
}
FILTER (SAMETERM(?uri, <URL>) && lang(?label) = "en" )
}
The UNION
allow you to find things that have a URI and those that have a URI via a redirect before then applying the rest of your query. Note I also changed your FILTER
to use SAMETERM()
which should make the query faster.
In general if you have this kind of query where you are doing that sort of a FILTER
to set a constant I would strongly suggest just replacing all uses of the ?uri
variable with <URL>
instead which should see much better performance
Hmm, I have found a solution but I don´t think it is perfect.
I added additional columns:
...
OPTIONAL { ?uri dbpedia-owl:wikiPageRedirects ?red } .
OPTIONAL { ?red geo:lat ?rlat . ?red geo:long ?rlong }
...
but now I have 2 columns more to check in response.
Another way I was trying is:
...
OPTIONAL { ?uri dbpedia-owl:wikiPageRedirects ?red } .
{?uri geo:lat ?lat . ?uri geo:long ?long} UNION { ?red geo:lat ?lat . ?red geo:long ?long }
...
but then i have any answer if entity doesn´t have geo:long & geo:lat
So, anybody knows better solution?