3

Using SPARQL I want to show all French embassies in Liberia (spoiler: there is exactly one)

So I wrote this naive query:

SELECT ?item WHERE {
    ?item wdt:P17 wd:Q1014.    # country: Liberia
    ?item wdt:P31 wd:Q3917681. # instance of: embassy
    ?item wdt:P137 wd:Q142.    # operated by: France
}

PROBLEM: It finds zero items. Even though there is such an item in Liberia, more exactly it is located in the administrative territorial entity of "Monrovia", which itself has a country value of "Liberia".

More generally, I want to get ALL Wikidata items that are in a particular country. How to do?

It must also work if you replace Q1014 (Liberia) with Q183 (Germany), which is even trickier.

3

Not as straight forward as it seems. Here's a first attempt which works for the Liberian example.

    SELECT ?item ?territory ?territoryLabel 
WHERE {
    ?item wdt:P31 wd:Q3917681. # instance of: embassy
    ?item wdt:P137 wd:Q142.    # operated by: France
    ?item wdt:P131 ?territory .
    ?territory wdt:P17 wd:Q1014 . #countries seem to have a wdt:P17 property of their id
      SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en" .
}
  }
ORDER BY ?item

Gives duplicates for countries that have changed. See Minsk as an example of a territory in many former countries. A generic query would need to filter these out.

  • Obviously, I would prefer a query that works for all such examples, and gives no duplicates :-) Is there maybe a way to remove duplicates within the query itself? – Nicolas Raoul Sep 8 '16 at 3:47
  • Also, would that work for a village located in a region located in a prefecture located in a country? The levels of "located in the administrative territorial entity" can be arbitrarily deep. – Nicolas Raoul Sep 8 '16 at 3:49
2

This query does the trick:

SELECT DISTINCT ?item WHERE {
    ?item wdt:P131*/wdt:P17 wd:Q1014.    # country: Liberia (recursive)
    ?item wdt:P31 wd:Q3917681.           # instance of: embassy
    ?item wdt:P137 wd:Q142.              # operated by: France
}

It includes all sub-regions of the country.
It does not show any duplicates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.