首页 > 解决方案 > Wikidata SPARQL - 国家及其(仍然存在的)邻国

问题描述

我想从 Wikidata 中使用 SPARQL 查询一个国家的邻居,如下所示

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" .
    }
}

我的问题是,例如,在德国邻国的这个例子中,仍然显示了不再存在的国家,例如:

已经试过了

我已经可以通过FILTER声明减少数字了。

问题

选择

标签: sparqlwikidatawikidata-api

解决方案


您可以检查类型为wd:Q133346('border') 或 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

试试看!

从某种意义上说,记录是重复的:对于阿富汗-乌兹别克斯坦边界,该列表同时包含(?country1=Afganistan,?country2=Uzbekistan)(?country1=Uzbekistan,?country2=Afganistan)


  • 与世界上所有国家/地区有邻国的数据库或列表或准备的HashMaps

您可以在https://opendata.stackexchange.com上询问。


推荐阅读