This is my sparql query to get all the country name with some property of that country.
SELECT distinct ?country ?capital ?currency ?lat ?long
WHERE {
?country rdf:type dbo:Country .
?country dbo:capital ?capital .
?country dbo:currency ?currency .
?country geo:lat ?lat.
?country geo:long ?long.
}
ORDER BY ?country
But the problem is, Some country is missing, like "Switzerland". You go to http://dbpedia.org/page/Switzerland this page you will see that it's type is country. you will also do not find exactly "Austria" rather "Austrian_Empire". Why? There is a entity called "Austria" and it is dbo:Country type.
Switzerland does not seem to have dbo:capital, which is why it's not included in the results of your query.
If you want to get even results that do not have some of the properties, use OPTIONAL:
SELECT distinct ?country ?capital ?currency ?lat ?long
WHERE {
?country rdf:type dbo:Country .
OPTIONAL
{
?country dbo:capital ?capital .
?country dbo:currency ?currency .
?country geo:lat ?lat.
?country geo:long ?long.
}
}
ORDER BY ?country
Though this query returns even entities that are not countries (but for some reason are dbo:Country), like Cinema of Switzerland.
Related
I'm not sure what I'm doing wrong. I have a nice list, but not only are the cities duplicating, but I'm unsure how they're defined as cities. I would expect to see London in the results and have similar results to this Wikipedia page. These results are quite different to the Wikipedia page.
I want to:
Get a list of cities, with their first-level administrative country subdivision (province/state/region), similar to this Wikipedia page
While avoiding duplicate cities.
SELECT ?city ?cityLabel ?country ?population ?countryLabel ?region ?regionLabel ?lat ?long
WHERE
{
?city wdt:P31/wdt:P279 wd:Q515 . # find instances of subclasses of city
?city (wdt:P131) ?region.
?region wdt:P31/wdt:P279 wd:Q10864048 .
?city wdt:P1082 ?population .
?city wdt:P17 ?country . # Also find the country of the city
?city p:P625 ?statement . # coordinate-location statement
?statement psv:P625 ?coordinate_node .
OPTIONAL { ?coordinate_node wikibase:geoLatitude ?lat. }
OPTIONAL { ?coordinate_node wikibase:geoLongitude ?long.}
FILTER (?population > 100000) .
# choose language
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 8000
Try it
Update:
Although not an answer to this specific question, anyone trying to get similar data to this should have a look here.
Update 2:
With help in the comments from #UninformedUser, the query is now:
SELECT DISTINCT ?city ?cityLabel ?country ?population ?countryLabel ?region ?regionLabel ?lat ?long
WHERE
{
?city wdt:P31/wdt:P279 wd:Q515 . # find instances of subclasses of city
?city (wdt:P131) ?region.
?region wdt:P31/wdt:P279 wd:Q10864048 .
?city p:P1082 ?populationStmt .
?populationStmt ps:P1082 ?population ; pq:P585 ?pop_date .
?city wdt:P17 ?country . # Also find the country of the city
?city p:P625 ?statement . # coordinate-location statement
?statement psv:P625 ?coordinate_node .
OPTIONAL { ?coordinate_node wikibase:geoLatitude ?lat. }
OPTIONAL { ?coordinate_node wikibase:geoLongitude ?long.}
FILTER NOT EXISTS {
?city p:P1082/pq:P585 ?pop_date_ .
FILTER (?pop_date_ > ?pop_date)
}
FILTER (?population > 100000) .
# choose language
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 8000
Try it
I wrote the following SPARQL query to find the wikidata item with the label "San Leucio" in Italy.
SELECT DISTINCT * WHERE {
?location ?label 'San Leucio'#en .
?location wdt:P17 wd:Q38 .
?location rdfs:label ?locationName .
OPTIONAL {
?article schema:about ?location .
?article schema:isPartOf <https://en.wikivoyage.org/> .
}
?location wdt:P18 ?image .
FILTER(lang(?locationName) = "en")
}
The query returns these 3 results:
wd:Q55179410
wd:Q20009063
wd:Q846499
The result I want is wd:Q846499, which is outside of Naples, Italy. Is there any way I could further filter this query to return the result that is nearest to Naples? I know that I can get the geoCoordinates for each of these with ?location wdt:P625 ?coordinates, but I'm not sure how I could use that to compare to the geo-coordinates of Naples to get what I want.
SELECT DISTINCT * {
VALUES ?naples {wd:Q2634}
?Napfes wdt:P625 ?naples_coordinates.
?location rdfs:label 'San Leucio'#en .
?location wdt:P17 wd:Q38 .
?location wdt:P18 ?image .
?location wdt:P625 ?location_coordinates.
OPTIONAL {
?article schema:about ?location .
?article schema:isPartOf <https://en.wikivoyage.org/> .
}
BIND (geof:distance(?location_coordinates, ?naples_coordinates) AS ?distance)
} ORDER BY ?distance LIMIT 1
I know this is an easy question to answer, but I have been googling this for a few hours without success. Why does the following SPARQL being entered in http://dbpedia.org/snorql/ not return anything?
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name ?country_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
FILTER(?country=dbpedia:Canada).
?country foaf:name ?country_name .
}
LIMIT 200
I also tried -with the same results (i.e. none)
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
?country foaf:name ?country_name .
FILTER(langMatches(lang(?country_name),"EN") && ?country_name="Canada")
}
LIMIT 200
No need for filters for an equality.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?country ?city ?city_name
WHERE {
?city rdf:type dbo:City ;
foaf:name ?city_name ;
dbo:country ?country .
?country foaf:name "Canada"#en .
FILTER(langMatches(lang(?city_name), "en"))
}
ORDER BY ?city_name
LIMIT 100
Test : http://linkedwiki.com/query/Filter_cities_by_country_with_DBpedia
Figured it out with some further digging.
To get the first query digging, I was using the wrong prefix. I needed to use <http://dbpedia.org/Resource> not the ontology one. This comes pre-defined in snorql - but it didn't actually have a prefix. But with some fiddling the following worked.
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name ?country_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
FILTER(?country=:Canada).
?country foaf:name ?country_name .
}
LIMIT 200
As for matching on name, apparently you can't just match on a literal when using foaf. You need to add the language tag:
prefix dbo: <http://dbpedia.org/ontology/>
SELECT ?country ?city ?city_name
WHERE {
?city rdf:type dbo:City .
?city foaf:name ?city_name .
?city dbo:country ?country .
?country foaf:name ?country_name .
FILTER(langMatches(lang(?country_name),"EN") && ?country_name="Canada"#en)
}
LIMIT 200
Using http://dbpedia.org/sparql, I want to receive the geographic coordinates of all bank buildings. The list of classes tells me that I should query for Bank.
Yet, the following code yields nothing:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[]
rdf:type dbpedia-owl:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If instead I query for any sibling to Bank, (e.g. Brewery or LawFirm), I see at least some results. What's wrong with above code?
If you look into a dbpedia page for a bank, you can see that instead on rdf:type, banks have a property dbpedia-owl:industry that has dbpedia:Bank (Refah bank) or dbpedia:Financial_services (Cyprus bank) as a value. So if you rewrite your query as the following, you will get some results:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry dbpedia:Bank ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (LANGMATCHES(LANG(?label), 'en'))
}
If you add dbpedia:Financial_services, other organisations such as London stock exchange will also appear:
SELECT DISTINCT ?bank
WHERE {
?bank dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label.
FILTER (?place in (dbpedia:Financial_services, dbpedia:Bank) &&
LANGMATCHES(LANG(?label), 'en'))
}
Again, by examining the London stock exchange, you can see that there is a product property that separates these financial institutions. So this will give you banks, but it might not cover all the banks available:
SELECT DISTINCT ?label ?lat ?long
WHERE {
[] dbpedia-owl:industry ?place ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?label;
dbpedia-owl:product ?product.
FILTER ( ?place in (dbpedia:Financial_services, dbpedia:Bank)
&& ?product in (dbpedia:Bank, dbpedia:Private_banking, dbpedia:Professional_Banking, dbpedia:Retail_banking, dbpedia:Investment_banking, dbpedia:Commercial_bank)
&& LANGMATCHES(LANG(?label), "en"))
} ORDER BY DESC(COUNT(DISTINCT ?product))
I am sure what I want to do is very easy, yet I cannot seem to get the query right. I have records in dataset which have values such as city name e.g. 'New York' and it's corresponding country code e.g 'US'. I also have access to the full country name and country ISO codes.
I would like to get the population and abstract value's for these cities off dbpedia, by using a where clause such as:
Get population where name = "New York" and isoCountryCode = "US"
I've searched for help on this to no avail.
so far I have been kindly helped by #rohk with this query, 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 returns no results for Bangkok, Thailand.
I just cant seem to get the SPARQL query correct, I'm sure I am being silly with my query. If any guru's could provide me with help I'd appreciate it. Thanks!
I guess 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 the English abstract, add a 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 the state and it doesn't have a populationTotal figure attached. “New York City” is the city.
This query is working
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 : For Bangkok, there are 2 problems :
No country code for Thailand : you can use rdfs:label "Thailand"#en instead.
rdf:type of Bangkok is not schema:City but dbpedia-owl:Settlement
Here is a working query 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' )
}