如何通过SPARQL从WikiData获取含巴黎的全球大城市GPS坐标
问题:SPARQL查询全球大城市遗漏巴黎的原因及解决办法
我为实现基础地理编码,编写了以下用于查询全球人口超10万大城市的SPARQL语句:
SELECT ?city ?cityLabel ?countryLabel ?iso ?population ?gps WHERE { ?city wdt:P31 wd:Q515 . hint:Prior hint:runFirst true . ?city wdt:P17 ?country . ?country wdt:P297 ?iso . ?city wdt:P625 ?gps . ?city wdt:P1082 ?population . FILTER (?population > 100000) . SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } } ORDER BY DESC(?population) LIMIT 5000
但结果集中未包含法国巴黎,却包含法国的一些小城市,请问问题出在哪里?
问题原因
原查询中使用?city wdt:P31 wd:Q515,其中wdt:P31代表“实例属于”,wd:Q515是“城市”类别。但在Wikidata中,巴黎的实体(wd:Q90)直接类型是wd:Q15284(法国市镇),并非直接属于wd:Q515,它是通过P279(子类关系)间接继承自Q515的。原查询仅匹配直接类型为城市的实体,因此漏掉了这类通过子类关联的城市。
修正后的查询语句
将类型匹配改为子类链查询,用wdt:P31/wdt:P279*匹配所有属于Q515或其子类的实体,同时添加DISTINCT避免重复结果:
SELECT DISTINCT ?city ?cityLabel ?countryLabel ?iso ?population ?gps WHERE { ?city wdt:P31/wdt:P279* wd:Q515 . ?city wdt:P17 ?country . ?city wdt:P1082 ?population . ?city wdt:P625 ?gps . ?country wdt:P297 ?iso . FILTER (?population > 100000) . SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } } ORDER BY DESC(?population) LIMIT 5000
修改后即可正确包含巴黎这类通过子类关联的城市实体。
内容的提问来源于stack exchange,提问作者Vadim Kantorov
相关产品推荐
相关产品推荐

