微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用Wikidata中的P625坐标定位克服单一值约束问题

如何解决用Wikidata中的P625坐标定位克服单一值约束问题

我正在尝试通过以下查询获取城市列表以及地区和国家/地区信息:

# get a list of cities
# for geograpy3 library
# see https://github.com/somnathrakshit/geograpy3/issues/15
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
# get human settlements
SELECT disTINCT ?city ?cityLabel (max(?cityPop) as ?cityPopulation) ?coord ?region ?regionLabel ?regionIsoCode ?country ?countryLabel ?countryIsoCode ?countryPopulation ?countryGdpPerCapita WHERE {
  # if you uncomment this line this query might run for some 3 hours on a local wikidata copy using Apache Jena
  # run for Vienna,Illinois,Vienna Austria,Paris Texas and Paris France as example only
  # VALUES ?city { wd:Q577544 wd:Q1741 wd:Q830149 wd:Q90}.
  # run for Andorra
  VALUES ?country {wd:Q228}.
  # instance of human settlement https://www.wikidata.org/wiki/Q486972
  ?city wdt:P31/wdt:P279* wd:Q486972 .
  # label of the City
  ?city rdfs:label ?cityLabel filter (lang(?cityLabel) = "en").
  # country this city belongs to
  ?city wdt:P17 ?country .
  # label for the country
  ?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
  # https://www.wikidata.org/wiki/Property:P297 ISO 3166-1 alpha-2 code
  ?country wdt:P297 ?countryIsoCode.
  # population of country
  ?country wdt:P1082 ?countryPopulation.
  OPTIONAL {
     ?country wdt:P2132 ?countryGdpPerCapita.
  }
  OPTIONAL {
     # located in administrative territory
     # https://www.wikidata.org/wiki/Property:P131
     ?city wdt:P131* ?region.
     # administrative unit of first order
     ?region wdt:P31/wdt:P279* wd:Q10864048.
     ?region rdfs:label ?regionLabel filter (lang(?regionLabel) = "en").
     # isocode state/province
     OPTIONAL { ?region wdt:P300 ?regionIsoCode. }
  }
  # population of city
  OPTIONAL { ?city wdt:P1082 ?cityPop.}
   # get the coordinates
  OPTIONAL { ?city wdt:P625 ?coord. }
} GROUP BY  ?city ?cityLabel  ?coord ?region ?regionLabel ?regionIsoCode ?country ?countryLabel ?countryIsoCode ?countryPopulation ?countryGdpPerCapita
ORDER BY ?cityLabel

try it! 实验一下我注释掉的查询

  # VALUES ?city { wd:Q577544 wd:Q1741 wd:Q830149 wd:Q90}.
  # run for Andorra
  VALUES ?country {wd:Q228}.

看到结果有意义。

现在进行安道尔审判,有多个坐标的城市:

https://www.wikidata.org/wiki/Property:P625 哪些事件被标记为问题。

Wikidata Query Screenshot

我知道有How to get only the most recent value from a Wikidata property?https://w.wiki/EKB

中所述的解决方法

我尝试了摘录中的方法

?city p:P1082 ?populationStatement . 
  ?populationStatement ps:P1082 ?cityPopulation.
  ?populationStatement pq:P585 ?date
  FILTER NOT EXISTS { ?city p:P1082/pq:P585 ?date_ . FILTER (?date_ > ?date) } 

这使查询真正变慢,在这种情况下,我正在研究数十万个人类住区的所有实例。即使在我的本地wikidata副本上,此过程也要运行3个小时以上!

所以我想知道是否存在MAX,AVG,具有限制之类的子查询之类的替代方案,或者是否有任何其他精妙的想法能够以令人满意的性能解决问题?

解决方法

您可以将sample()用作聚合函数(sparql doc)

从查询表达式开始,您需要将第一行更改为

SELECT DISTINCT ?city ?cityLabel (max(?cityPop) as ?cityPopulation) (sample(?coord) as ?coordinate) ?region ?regionLabel ?regionIsoCode ?country ?countryLabel ?countryIsoCode ?countryPopulation ?countryGdpPerCapita WHERE {

,最后一行是:

} GROUP BY  ?city ?cityLabel ?region ?regionLabel ?regionIsoCode ?country ?countryLabel ?countryIsoCode ?countryPopulation ?countryGdpPerCapita

结果应如下所示:https://w.wiki/dRV

您尝试的解决方法不起作用,因为与P1082(填充)不同,P625(坐标)在大多数情况下没有P585(时间点)限定符。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?