SPARQL bordering countries example - sparql

Can anyone show me any SPARQL query to get all bordering contries of all countries from http://www4.wiwiss.fu-berlin.de/factbook/sparql?
For example Afghanistan has:
factbook:landboundary db:China,
factbook:landboundary db:Iran,
factbook:landboundary db:Pakistan,
factbook:landboundary db:Tajikistan,
factbook:landboundary db:Turkmenistan
My try of getting data:
SELECT ?country ?name ?neighbour
WHERE {
?country rdf:type factbook:Country .
?country rdfs:label ?name.
OPTIONAL{
?country factbook:landboundary ?neighbour.
}
}
ended with following message:
rethrew: de.fuberlin.wiwiss.d2rq.D2RQException: Table 'factbook.neighbors' doesn't exist: SELECT DISTINCT `T0_neighbors`.`name_encoded` FROM `bordercountries` AS `T0_bordercountries`, `neighbors` AS `T0_neighbors`, `countries` AS `T0_countries` WHERE `T0_bordercountries`.`Landboundaries_bordercountries_title` = `T0_neighbors`.`Name` AND `T0_bordercountries`.`Name` = `T0_countries`.`Name` AND `T0_countries`.`name_encoded` = 'Aruba' (E0)
I've asked the same question on http://answers.semanticweb.com but no luck yet so I'm trying my luck here

The failure seems to be caused by an internal system error. Your SPARQL query does not have any syntax errors and the predicates you provided are valid according to the data.
However, I don't understand how your query is supposed to return the neighbors of one specific country. Maybe you want to try something like this:
SELECT DISTINCT ?neighbor
WHERE {
?neighbor rdf:type factbook:Country .
?neighbor factbook:landboundary db:Afghanistan .
}

Very much later and not exactly an answer to this question (the SPARQL Endpoint to the CIA Factbook seem to be down at the moment), but WikiData has a few examples how to get bordering countries according to their data set at https://query.wikidata.org/
E.g. if you open the examples and search for "border", you get a query for "countries sharing a border with Cameroon":
#Population of countries sharing a border with Cameroon
#defaultView:LineChart
SELECT ?country ?year ?population ?countryLabel WHERE {
{
SELECT ?country ?year (AVG(?population) AS ?population) WHERE {
{
SELECT ?country (str(YEAR(?date)) AS ?year) ?population WHERE {
?country wdt:P47 wd:Q1009; # shares border with Cameroon
p:P1082 ?populationStatement.
?populationStatement ps:P1082 ?population;
pq:P585 ?date.
}
}
}
GROUP BY ?country ?year
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
To understand this one has to know (or figure out) that wd:Q1009 is actually Cameroon. Not sure how to do this.
This example also displays a - imo not very useful - display of the population of the surrounding countries by year.
A simpler version without the extra data is:
SELECT ?country ?countryLabel WHERE {
?country wdt:P47 wd:Q1009 # shares border with Cameroon
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
}
(The SERVICE wikibase:label is a WikiData extension)
Finally all bordering neighbours for all countries might be:
SELECT ?country ?countryLabel ?neighbourLabel ?neighbour WHERE {
?country wdt:P31 wd:Q6256;
wdt:P47 ?neighbour
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
} ORDER BY ?countryLabel ?neighbourLabel

Related

Wikidata: Filter post codes only current valid

I would like to filter post codes to show only the current active for today.
Problem is there are are cities with old post codes (Example).
My current query shows the old post codes:
SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
wdt:P17 wd:Q183;
wdt:P281 ?postcode;
wdt:P131 ?federal_state.
?federal_state wdt:P439 ?federal_state_nr.
}
ORDER BY (?postcode)
LIMIT 10
(query.wikidata.org)
I would have to use start time P580 and end time P582 but I don't see how.
You can use this for filtering out the claims which have an end time:
?city p:P281 ?postCodeStmt .
?postCodeStmt ps:P281 ?postcode .
FILTER NOT EXISTS { ?postcode pq:P582 ?endTime . }
The whole query becomes:
SELECT ?city ?cityLabel ?postcode ?federal_stateLabel ?federal_state_nr WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }
?city (wdt:P31/(wdt:P279*)) wd:Q7930989;
wdt:P17 wd:Q183;
p:P281 ?postCodeStmt;
wdt:P131 ?federal_state.
?federal_state wdt:P439 ?federal_state_nr.
?postCodeStmt ps:P281 ?postcode.
FILTER NOT EXISTS { ?postCodeStmt pq:P582 ?endTime . } # Filtering out old postal codes
}
ORDER BY (?postcode)
LIMIT 100
See also Wikidata:SPARQL tutorial§Qualifiers.

Wikidata query - how to add year to city's population?

I would like to get the year of population count for each city.
Do tou know how to add it correctly? Currently I got empty results.
Here's my query:
SELECT DISTINCT ?cityLabel ?population ?gps ?data WHERE {
?city (wdt:P31/(wdt:P279*)) wd:Q515;
wdt:P1082 ?population;
wdt:P625 ?gps.
OPTIONAL { ?population wdt:P585 ?date. } # here I have a problem
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population) LIMIT 100
PS. just paste it here: https://query.wikidata.org/
First problem: you are selecting ?data but the actual variable is ?date.
Second problem: ?population is the object of your statement, but qualifiers refer to a whole statement, not just its object.
For referring to the statement, you'll have to use p:P1082 instead of wdt:P1082.
You can obtain what you want with the following query:
SELECT DISTINCT ?cityLabel ?population ?gps ?date WHERE {
?city
wdt:P31/wdt:P279* wd:Q515;
wdt:P625 ?gps.
?city p:P1082 ?populationStatement .
?populationStatement ps:P1082 ?population .
OPTIONAL { ?populationStatement pq:P585 ?date. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population)
LIMIT 10
I set LIMIT 10 because this is a pretty heavy query and sometimes it reaches timeout.
To deepen the topic, I'd suggest you to read Wikidata:SPARQL tutorial§Qualifiers.

How to SPARQL select all living politicians from EU political parties on Wikidata?

Can somebody, please help me write a SPARQL query, which would select all currently living politicians, whose political institutions are from a country within the EU?
Currently I have 3 queries, but I don't know how to combine them
The first one checks for people who are living, whose occupation is politician and/or who are a member of a political party
SELECT DISTINCT ?politician ?politicianLabel
WHERE {
?politician wdt:P106 wd:Q82955 .
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
OPTIONAL {
?politician wdt:P102 ?membership .
}
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
The second one shows all EU countries
SELECT ?country ?countryLabel
WHERE {
?country wdt:P463 wd:Q458
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
And the third one finds parties from a specific country
SELECT ?party
WHERE {
?party wdt:P31 wd:Q7278
?party wdt:17 #EU Countries#
}
But How can I combine these queries? Can somebody help?
Thank you
Thank you #AKSW
This works:
WHERE {
?country wdt:P463 wd:Q458.
?party wdt:P31 wd:Q7278 .
?party wdt:P17 ?country .
?politician wdt:P106 wd:Q82955 . hint:Prior hint:runFirst true.
?politician wdt:P102 ?party .
FILTER NOT EXISTS {?politician wdt:P570|wdt:P509|wdt:P20 ?o}
}
If you start with the country and add the
hint:Prior hint:runFirst true.
line, it is optimized enough to give result.

SPARQL query for finding films originating from and released in the United States

I have the following SPARQL query that appears to correctly produce the films produced in the US (country of origin) and released in the US (place of publication) in 2018. The issue I'm having is that one row is produced for each release even though the other releases are outside of the US. I've added a limit to reduce the size of the response.
Here is the query:
SELECT ?item ?name ?publication_date ?placeLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item rdfs:label ?name;
wdt:P31 wd:Q11424;
wdt:P495 wd:Q30; # -> country of origin US
wdt:P577 ?publication_date.
?item p:P577 ?publication_statement.
?publication_statement pq:P291 ?place.
FILTER(xsd:date(?publication_date) > "2018-01-01"^^xsd:date)
FILTER(
(LANG(?name)) = "en"
&& ?place=wd:Q30) # -> place of publication
}
ORDER BY ?name
LIMIT 10
I would like to change it so that it produces one row per movie IF it had a release in the US in 2018.
Thanks for your help. Comments on the use of FILTER or other non idiomatic SPARQL are also welcome.
You can use GROUP BY:
SELECT ?item (SAMPLE(?name) as ?Name) (SAMPLE(?publication_date) as ?Date) WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item rdfs:label ?name;
wdt:P31 wd:Q11424;
wdt:P495 wd:Q30; # -> country of origin US
wdt:P577 ?publication_date.
?item p:P577 ?publication_statement.
?publication_statement pq:P291 ?place.
FILTER(xsd:date(?publication_date) > "2018-01-01"^^xsd:date)
FILTER(
(LANG(?name)) = "en"
&& ?place=wd:Q30) # -> place of publication
}
GROUP BY ?item
ORDER BY ?Name
LIMIT 10
See this query on Wikidata.
And you need to fix the SELECT line as you can't pass out the indeterminate non-group keys without explicitly saying. See similar question.

How to filter objects by property on Wikidata using SPARQL?

I cannot find a way to filter objects by name. For example, this query should limit country objects to those with official name "Canada":
SELECT DISTINCT ?country ?official_name WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,fr". }
?country wdt:P31 wd:Q6256.
?country wdt:P1448 ?official_name.
?country wdt:P1448 "Canada".
}
LIMIT 100
Here is a direct link to the query.
Any ideas?
Knowing how these things go, you'll likely be happier in the end with this somewhat fuzzy filter, than an exact match test... but you could change filter ( contains (str(?official_name), "Canada") ) below to filter ( str(?official_name) = "Canada")
SELECT DISTINCT ?country ?official_name WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,fr". }
?country wdt:P31 wd:Q6256.
?country wdt:P1448 ?official_name.
# ?country wdt:P1448 "Canada".
filter ( contains (str(?official_name), "Canada") )
}
LIMIT 100