Reputation: 8569
Wikidata provides a 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
I'm wondering if there is a way to control the order of the items in the legend (top right).
Upvotes: 1
Views: 144
Reputation: 11479
It seems that leaflet tries to parse layer names as numbers. In case of failure, the order of layers in the legend corresponds to the order of the first appearance in the table (switch between "Map" and "Table" views).
Thus, you should bind additional variable with naturally sortable values (and then sort results by those values). Perhaps this is possible using REPLACE
, but I'd prefer to use simple dictionary.
#--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))
Upvotes: 2