timguy
timguy

Reputation: 2582

Wikidata SPARQL - Countries and their (still existing) neighbours

I want to query the neighbours to a country with SPARQL from Wikidata like this:

SELECT ?country ?countryLabel WHERE { 
  ?country wdt:P47 wd:Q183 . 
  FILTER NOT EXISTS{ ?country wdt:P576 ?date } # don't count dissolved country - at least filters German Democratic Republic
  SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
    }
}

My issue is that e.g. in this example for neighbours of germany there are still countries shown which does not exist anymore like:

Already tried

I could already reduce the number by the FILTER statement.

Question

Alternative

Upvotes: 2

Views: 687

Answers (1)

Stanislav Kralin
Stanislav Kralin

Reputation: 11479

You could check entities of type wd:Q133346 ('border') or wd:Q12413618 ('international border'):

SELECT ?border ?borderLabel ?country1Label ?country2Label ?isLandBorder ?isMaritimeBorder ?constraint {
  VALUES (?country1) {(wd:Q183)}
  ?border wdt:P31 wd:Q12413618 ;
          wdt:P17 ?country1 , ?country2 .
  FILTER (?country1 != ?country2)
  BIND (EXISTS {?border wdt:P31 wd:Q15104814} AS ?isLandBorder)
  BIND (EXISTS {?border wdt:P31 wd:Q3089219} AS ?isMaritimeBorder)
  BIND ((?isLandBorder || ?isMaritimeBorder) AS ?constraint)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  } ORDER BY ?country1Label

Try it!

In some sense, records are duplicated: for the Afghanistan–Uzbekistan border, the list contains both (?country1=Afganistan,?country2=Uzbekistan) and (?country1=Uzbekistan,?country2=Afganistan).


  • a database or lists or prepared HashMaps whatever with all countries of the world with neighbours

You could ask on https://opendata.stackexchange.com.

Upvotes: 2

Related Questions