Dbpedia SPARQL query to get a specific value for a given city
I'm sure I want to make it very simple, but I can't get the right query. I have records in a dataset that have values like city name for example. "New York" and the corresponding country code, such as "USA". I also have access to all ISO country and country codes.
I would like to get the population and abstract value for these cities from dbpedia using a where clause like:
Get population where name = "New York" and isoCountryCode = "US"
I have looked for help on this to no avail.
so far @rohk has helped me with this request which does not fully work for all locations:
SELECT DISTINCT ?city ?abstract ?pop
WHERE {
?city rdf:type schema:City ;
rdfs:label ?label ;
dbpedia-owl:abstract ?abstract ;
dbpedia-owl:country ?country ;
dbpedia-owl:populationTotal ?pop .
?country dbpprop:countryCode "USA"@en .
FILTER ( lang(?abstract) = 'en' and regex(?label, "New York City"))
}
The above works for New York, however, when I change it to:
SELECT DISTINCT ?city ?abstract ?pop
WHERE {
?city rdf:type schema:City ;
rdfs:label ?label ;
dbpedia-owl:abstract ?abstract ;
dbpedia-owl:country ?country ;
dbpedia-owl:populationTotal ?pop .
?country dbpprop:countryCode "THA"@en .
FILTER ( lang(?abstract) = 'en' and regex(?label, "Bangkok"))
}
It does not return results for Bangkok, Thailand.
I just can't seem to find the correct SPARQL query, I'm sure I'm stupid with my query. If any guru could help me, I would appreciate it. Thank!
source to share
This request works
SELECT DISTINCT *
WHERE {
?city rdf:type schema:City ;
rdfs:label ?label ;
dbpedia-owl:abstract ?abstract ;
dbpedia-owl:country ?country ;
dbpprop:website ?website ;
dbpedia-owl:populationTotal ?pop .
?country dbpprop:countryCode "USA"@en .
FILTER ( lang(?abstract) = 'en' and regex(?label, "New York City"))
}
EDIT: There are two problems for Bangkok:
- Country Code for Thailand: You can use instead
rdfs:label "Thailand"@en
. -
rdf:type
Bangkok notschema:City
butdbpedia-owl:Settlement
Here is a working request for Bangkok
SELECT DISTINCT *
WHERE {
?city rdf:type dbpedia-owl:Settlement ;
rdfs:label "Bangkok"@en ;
dbpedia-owl:abstract ?abstract ;
dbpedia-owl:populationTotal ?pop ;
dbpedia-owl:country ?country ;
dbpprop:website ?website .
?country rdfs:label "Thailand"@en .
FILTER ( lang(?abstract) = 'en' )
}
source to share
I think you want something like this:
SELECT * WHERE {
?x rdfs:label "New York City"@en.
?x dbpedia-owl:populationTotal ?pop.
?x dbpedia-owl:abstract ?abstract.
}
To get only abstracts in English, add FILTER
:
SELECT * WHERE {
?x rdfs:label "New York City"@en.
?x dbpedia-owl:populationTotal ?pop.
?x dbpedia-owl:abstract ?abstract.
FILTER (LANG(?abstract) = 'en')
}
"New York" is a state, and there is no figure in it populationTotal
. "New York" is a city.
source to share