List countries from DBpedia - sparql

Trying to query DBpedia for a list of all countries with the dbo:longName property and the capital of each country listed but get 0 results returned. Can't see whats wrong with the query.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:longName ;
dbo:capital ?capital .
}
What's missing?

You are missing that ?country has a rdf:type of dbo:Country and not dbo:longName. The right query should be:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?x a dbo:Country.
?x dbo:longName ?country.
?x dbp:capital ?capital
}
Update
Based on your comment you want the URI's of the country and their capital. Therefore, you don't need dbo:longName, because you don't want the country label name. You will select the instances:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital
}
Note that results will bring countries that are extinct. If you want to filter countries that have ended, you should get this result with:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?country ?capital
WHERE {
?country a dbo:Country.
?country dbo:capital ?capital.
FILTER NOT EXISTS { ?country dbo:dissolutionYear ?yearEnd }
}

Related

retrieving the countries details from geonames using a sparql query

I am trying to get specific countries informations from geonames using "Linked Data HTW Chur sparql endpoint" (url: http://linkeddata.fh-htwchur.ch/lodestar/sparql ):
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dcterms: <http://purl.org/dc/terms/>
select distinct ?country ?population ?lattitude ?longitude ?creationDate ?modificationDate
from <http://sws.geonames.org>
where { ?country gn:name ?country_name;
gn:population ?population;
wgs84_pos:lat ?lattitude;
wgs84_pos:long ?longitude;
dcterms:created ?creationDate;
dcterms:modified ?modificationDate
}
The query works if I remove "dcterms:created" and "dctermes:modified" but I need both to extract the data item creation date and the last modification date
If someone could please help me
You have to distinguish between the concept and the document about this concept.
Example for the United States:
https://sws.geonames.org/6252001/ (concept, i.e., the actual country)
https://sws.geonames.org/6252001/about.rdf (GeoNames document about this concept)
These URIs refer to each other via rdfs:isDefinedBy/foaf:primaryTopic.
The dcterms:created property gives the date when the document was created, not when the country was created. The same applies to dcterms:modified.
If you don’t need to refer to the "document about the concept" in your results, you could use property paths to get the values:
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?country ?population ?lattitude ?longitude ?creationDate ?modificationDate
WHERE {
?country gn:name ?country_name ;
gn:population ?population ;
wgs84_pos:lat ?lattitude ;
wgs84_pos:long ?longitude ;
rdfs:isDefinedBy/dcterms:created ?creationDate ;
rdfs:isDefinedBy/dcterms:modified ?modificationDate .
}
Otherwise you can use another variable for it:
PREFIX gn: <http://www.geonames.org/ontology#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?country ?population ?lattitude ?longitude ?creationDate ?modificationDate
WHERE {
?country gn:name ?country_name ;
gn:population ?population ;
wgs84_pos:lat ?lattitude ;
wgs84_pos:long ?longitude ;
rdfs:isDefinedBy ?countryDocument .
?countryDocument dcterms:created ?creationDate ;
dcterms:modified ?modificationDate .
}

How to get Wikidata labels in more than one language?

I'm traying to get the regions of Italy in both Italian and English. I can get then in one laguage with this query...
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?RegionITLabel ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo
SERVICE wikibase:label { bd:serviceParam wikibase:language "it" }
}
ORDER BY ?regionITLabel
... but adding another language using the standard SPARQL syntax doesn't work.
... but adding another language using the standard SPARQL syntax doesn't work.
How are you doing that? This works:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?label (lang(?label) as ?label_lang) ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo ;
rdfs:label ?label
}
order by ?RegionIT
Link to try query
To limit to just Italian and English filter on the lang:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT DISTINCT ?RegionIT ?label ?ISO_code ?Geo
{
?RegionIT wdt:P31 wd:Q16110;
wdt:P300 ?ISO_code;
wdt:P625 ?Geo ;
rdfs:label ?label
filter(lang(?label) = 'it' || lang(?label) = 'en')
}
order by ?RegionIT
Link to try query
Obviously that multiplies the number of results, one for each language. If that's an issue you can do:
...
rdfs:label ?label_it , ?label_en
filter(lang(?label_it) = 'it' && lang(?label_en) = 'en')
...
which is effectively what the language service does.
Let's list all countries in English and Russian.
#List of countries in English and Russian
SELECT ?country ?label_en ?label_ru
WHERE
{
?country wdt:P31 wd:Q6256.
?country rdfs:label ?label_en filter (lang(?label_en) = "en").
?country rdfs:label ?label_ru filter (lang(?label_ru) = "ru").
}
SPARQL query
This example was taken from the tutorial Research in programming Wikidata, section "Countries".

Sparql syntax error on nested select

I am trying to select City with highest population. My Query looks like this:
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?population ?capital
WHERE {
?x dct:subject dbc:Countries_in_Europe .
?x dbo:capital ?capital .
?capital dbo:populationTotal ?population .
FILTER (?population =
(
SELECT MAX(?popul) AS ?pop
WHERE{
?capital dbo:populationTotal ?popul .
}
)
)
}
LIMIT 200
I haved followed This PDF page 5.
I am using apache-jena-fuseki-2.4.0 and getting a syntax error
Encountered " "select" "SELECT "" at line 18, column 1.
Any ideas where the problem is?
I don't know where you have seen that you can bind the result of a SELECT query to a variable in the FILTER, but that's totally wrong. This might work for SQL but not for SPARQL.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
SELECT ?population ?capital
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?population
FILTER ( ?population = ?pop )
# sub-SELECTs are evaluated first and encapsulated by { }
# in addition (as to TomP's comment), you have to restrict to European captials here
{ SELECT (MAX(?popul) AS ?pop)
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?popul }
}
}
LIMIT 200
But, if you really want to have the "capital in Europe with the highest population", why not using the easier and more obvious way which reflects your task, i.e. ORDER BY + LIMIT 1:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
SELECT ?population ?capital
WHERE
{ ?x dct:subject dbc:Countries_in_Europe ;
dbo:capital ?capital .
?capital dbo:populationTotal ?population
}
ORDER BY DESC(?population)
LIMIT 1

How to get such data as abstraction, location and link for image in Dbpedia?

I am trying to get some data about museum but it is not successfull. This is my code. I know the name of the museum, so I want to get data about this museum
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?abstract ?location WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name dbpedia:Chekhov_Shop .
}
The dbpprop:name that you have selected (dbpedia:Chekhov_Shop) is in fact a string. If you look at the dbpedia page it has been defined as The Chekhov Shop. Therefore, my suggestion is to filter your query based on the name you like to be displayed:
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT * WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name ?name.
Filter (str(?name)="The Chekhov Shop")
}
And if you need more information about this specific architectural structure, you start exploring. For example,
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT distinct * WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name ?name.
?architectural_structure dbpprop:location ?location.
?architectural_structure dbpedia-owl:abstract ?abstract.
Filter (str(?name)="The Chekhov Shop")
}
In general, when you are faced with a triple store try to find all ?o ?p ?s and then see where you need to put a specific predicate.

How do get city population by name with SPARQL

I need to get city population by its name. I'm sure SPARQL could provide this, but I'm not sure how to write the query. I have a query which provides COuntry's capital and coordinates by it's name, so I assume it should be something similar. HELP!
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?country ?population ?capital ?lat ?long WHERE {
?country a o:Country ; foaf:name "Germany"#en; o:capital [ geo:lat ?lat ; geo:long ?long ; p:name ?capital ]
}
All you need is:
PREFIX o: <http://dbpedia.org/ontology/>
PREFIX p: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?pop WHERE {
?country a o:Country ;
foaf:name ?name ;
p:populationEstimate ?pop .
}
The only tricky part was finding out the name of the property that links Countries and Populations.