1

Wikidata 提供了一种在 OpenStreetMap 上呈现 SPARQL 结果的酷方法。例如:_

#Big cities, grouped into map layers by population
#defaultView:Map
SELECT DISTINCT ?city ?cityLabel (SAMPLE(?location) AS ?location) (MAX(?population) AS ?population) (SAMPLE(?layer) AS ?layer)
WHERE
{
  ?city wdt:P31/wdt:P279* wd:Q515;
        wdt:P625 ?location;
        wdt:P1082 ?population.
  FILTER(?population >= 500000).
  BIND(
    IF(?population < 1000000, "<1M",
    IF(?population < 2000000, "1M-2M",
    IF(?population < 5000000, "2M-5M",
    IF(?population < 10000000, "5M-10M",
    IF(?population < 20000000, "10M-20M",
    ">20M")))))
    AS ?layer).
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?city ?cityLabel

我想知道是否有办法控制图例中项目的顺序(右上角)。

4

1 回答 1

2

似乎试图将图层名称解析为数字。在失败的情况下,图例中的图层顺序对应于表格中第一次出现的顺序(在“地图”和“表格”视图之间切换)。

因此,您应该将附加变量与自然可排序的值绑定(然后按这些值对结果进行排序)。也许这可以使用REPLACE,但我更喜欢使用简单的字典。

#--Big cities, grouped into map layers by population--
#defaultView:Map
SELECT DISTINCT ?city ?cityLabel
(SAMPLE(?location) AS ?location) (MAX(?population) AS ?population)
(SAMPLE(?layer) AS ?layer) {
    VALUES (?layer ?order) {
    ("<1M" 1) ("1M-2M" 2) ("2M-5M" 3) ("5M-10M" 4) ("10M-20M" 5) (">20M" 6)}
    ?city wdt:P31/wdt:P279* wd:Q515;
          wdt:P625 ?location;
          wdt:P1082 ?population.
    FILTER(?population >= 500000).
    BIND(
    IF(?population < 1000000, "<1M",
    IF(?population < 2000000, "1M-2M",
    IF(?population < 5000000, "2M-5M",
    IF(?population < 10000000, "5M-10M",
    IF(?population < 20000000, "10M-20M",
    ">20M")))))
    AS ?layer).
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} GROUP BY ?city ?cityLabel ORDER BY (SAMPLE(?order))

试试看!

于 2018-04-24T20:58:59.480 回答