在维基数据查询服务中订购地图图例项

Ordering map legend items in Wikidata Query Service

维基数据提供了一个cool way to render SPARQL results on OpenStreetMap. For example:

#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

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

似乎试图将图层名称解析为数字。如果失败,图例中的图层顺序对应于 table 中首次出现的顺序(在 "Map" 和 "Table" 视图之间切换)。

因此,您应该将其他变量与自然排序table 值绑定(然后按这些值对结果进行排序)。也许这可以使用 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))

Try it!