Note: This text was provided by @IrinaM, but put into the question as a revision because of the eight hour limit. It's now much later and the suggestion that @IrinaM post as an answer hasn't been acted upon. Here's a slightly edited CW answer containing that text.
There are several mistakes in the code in the question.
The 2nd query (the one that didn't “work”) would return several results. If I searched countries with foaf:name "Romania"
, it would return "Communist Romania"
, "Wallachian Romania"
, "Romania"
, etc. Out of these results, only one would have geo:lat
and geo:long
.
Basically, when a required property isn't satisfied by all the results, then no results will be returned.
The wrong ontology is used for geo
; it should be <http://www.w3.org/2003/01/geo/wgs84_pos#>
.
The other odd results need to be filtered out, to only keep the desired unique results. In this case, filtering after the inexistence of dbpedia-owl:dissolutionDate
works.
Exact matches of foaf:name using FILTER (?name="someCountryName"^^xsd:string)
do not work.
Here's code that retrieves successfully the latitude and longitude values for a given country (beware, some other filters may be needed when searching for the right country):
//setup prefixes
$prefixes = 'PREFIX dbo:<http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
';
//query no. 1 - find out "right" country - hoping for a unique result
$q1 = $prefixes.'SELECT ?c WHERE {
?c rdf:type dbo:Country;
foaf:name "'.$userPreferences->country.'"@en.
OPTIONAL {?c dbo:dissolutionDate ?x}
FILTER(!bound(?x))}';
//note: $userPreferences->country represents a country entered by the user in an input
//query no. 2 - find out long & lat
$q2 = $prefixes.'SELECT * WHERE {
<'.$countryReturned.'> geo:lat ?lat;
geo:long ?long.
}';
//note: $countryReturned represents the URI of the "unique" country my 1st query
//retrieved, we use it directly.
For those passionate about ARC2 PHP lib, this is how to make the queries (note:don't forget to include the ARC2.php file):
$dbpediaEndpoint = array('remote_store_endpoint' =>'http://dbpedia.org/sparql');
$dbpediaStore = ARC2::getRemoteStore($dbpediaEndpoint);
$rows1 = $dbpediaStore->query($q1,'rows');
var_dump($rows1); //to see quick results