query-84a57ba1a9cfd2cb8100ca901482c38d
.on TwitterOriginally posted
Use at
- https://query.wikidata.org/sparql
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pqv: <http://www.wikidata.org/prop/qualifier/value/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX bd: <http://www.bigdata.com/rdf#>
#defaultView:Map{"hide": ["?districtNumber", "?layer", "?rgb", "?shape", "?sanityCheckMdb"]}
# constituencies for the election to the German Bundestag 2017, with winning candidate and party
SELECT ?district ?districtLabel ?districtNumber ?mdb ?mdbLabel ?party ?partyLabel (?partyLabel AS ?layer) ?votesPercentage ?rgb ?shape ?sanityCheckMdb WHERE {
# find districts with shape
?district wdt:P3896 ?shape;
# successful candidate for 19th German Bundestag with party and % votes
p:P991 [
ps:P991 ?mdb;
pq:P2937 wd:Q30579723;
pq:P1268 ?party;
pqv:P1111 [ wikibase:quantityAmount ?votesPercentage; wikibase:quantityUnit wd:Q11229 ]
];
# district number in 2017 Bundestag constituencies
p:P528 [
ps:P528 ?districtNumberString;
pq:P972 wd:Q26971257
].
# turn string district number into integer
BIND(xsd:integer(?districtNumberString) AS ?districtNumber)
# sanity check
OPTIONAL {
?mdb p:P39 [
ps:P39 wd:Q1939555;
pq:P2937 wd:Q30579723;
pq:P768 ?district;
pq:P4100 ?party
].
BIND(true AS ?sanityCheckMdb)
}
# find original color of party
OPTIONAL { ?party wdt:P462/wdt:P465 ?rgbOriginalViaColor. }
OPTIONAL { ?party wdt:P465 ?rgbOriginalDirect. }
BIND(COALESCE(?rgbOriginalDirect, ?rgbOriginalViaColor) AS ?rgbOriginal)
# fade color depending on % votes; unfortunately, SPARQL has no facililties to parse hex as far as I’m aware,
# and the colors use a lot of different hex codes for each component, so we just replace each color fully :/
# each component (two digits) calculated in bc with ibase=obase=16, 100-(100-__)*3/4 or *2/4
BIND(IF(?votesPercentage >= (100/2),
?rgbOriginal,
IF(?votesPercentage >= (100/3),
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(?rgbOriginal, "000000", "404040"), "009EE0", "40B7E8"), "0088CE", "40A6DB"), "46962B", "75B161"), "BE3075", "CF6498"), "ED1B34", "F25567"),
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(?rgbOriginal, "000000", "808080"), "009EE0", "80CFF0"), "0088CE", "80C4E7"), "46962B", "A3CB96"), "BE3075", "DF98BB"), "ED1B34", "F78E9A")
)
) AS ?rgb)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?district rdfs:label ?districtLabel.
?party rdfs:label ?partyLabel.
?mdb rdfs:label ?mdbLabel.
}
}
ORDER BY ?districtNumber
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v2("?district"):::projected
v13("?districtLabel"):::projected
v8("?districtNumber"):::projected
v7("?districtNumberString")
v16("?layer")
v4("?mdb"):::projected
v15("?mdbLabel"):::projected
v5("?party"):::projected
v14("?partyLabel"):::projected
v12("?rgb"):::projected
v11("?rgbOriginal")
v10("?rgbOriginalDirect")
v9("?rgbOriginalViaColor")
v8("?sanityCheckMdb"):::projected
v3("?shape"):::projected
v6("?votesPercentage"):::projected
a1((" "))
a2((" "))
a3((" "))
a4((" "))
a5((" "))
c23(["bd:serviceParam"]):::iri
c8(["wd:Q11229"]):::iri
c4(["wd:Q30579723"]):::iri
c16(["wd:Q1939555"]):::iri
c25(["#91;AUTO_LANGUAGE#93;,en"]):::literal
c13(["wd:Q26971257"]):::iri
v2 --"p:direct/P3896"--> v3
a1 --"p:statement/P991"--> v4
a1 --"p:qualifier/P2937"--> c4
a1 --"p:qualifier/P1268"--> v5
a2 --"wikibase:quantityAmount"--> v6
a2 --"wikibase:quantityUnit"--> c8
a1 --"p:qualifier/value/P1111"--> a2
v2 --"p:P991"--> a1
a3 --"p:statement/P528"--> v7
a3 --"p:qualifier/P972"--> c13
v2 --"p:P528"--> a3
bind0[/"http://www.w3.org/2001/XMLSchema#integer(?districtNumberString)"/]
v7 --o bind0
bind0 --as--o v8
subgraph optional0["(optional)"]
style optional0 fill:#bbf,stroke-dasharray: 5 5;
a4 -."p:statement/P39".-> c16
a4 --"p:qualifier/P2937"--> c4
a4 --"p:qualifier/P768"--> v2
a4 --"p:qualifier/P4100"--> v5
v4 --"p:P39"--> a4
bind1[/"'true^^xsd:boolean'"/]
bind1 --as--o v8
end
subgraph optional1["(optional)"]
style optional1 fill:#bbf,stroke-dasharray: 5 5;
v5 -."p:direct/P462".-> a5
a5 --"p:direct/P465"--> v9
end
subgraph optional2["(optional)"]
style optional2 fill:#bbf,stroke-dasharray: 5 5;
v5 -."p:direct/P465".-> v10
end
bind2[/"?rgbOriginalDirect?rgbOriginalViaColor"/]
v10 --o bind2
v9 --o bind2
bind2 --as--o v11
bind3[/"if(?votesPercentage >= '100^^xsd:integer' / '2^^xsd:integer',?rgbOriginal,if(?votesPercentage >= '100^^xsd:integer' / '3^^xsd:integer',replace(replace(replace(replace(replace(replace(?rgbOriginal,'000000','404040'),'009EE0','40B7E8'),'0088CE','40A6DB'),'46962B','75B161'),'BE3075','CF6498'),'ED1B34','F25567'),replace(replace(replace(replace(replace(replace(?rgbOriginal,'000000','808080'),'009EE0','80CFF0'),'0088CE','80C4E7'),'46962B','A3CB96'),'BE3075','DF98BB'),'ED1B34','F78E9A')))"/]
v6 --o bind3
v11 --o bind3
bind3 --as--o v12
subgraph s1["http://wikiba.se/ontology#label"]
style s1 stroke-width:4px;
c23 --"wikibase:language"--> c25
v2 --"rdfs:label"--> v13
v5 --"rdfs:label"--> v14
v4 --"rdfs:label"--> v15
end
bind4[/"?partyLabel"/]
v14 --o bind4
bind4 --as--o v16