query-976dcd52f83ef2f6aa50424b1aa2e85d

rq turtle/ttl

Castles and mountains: Shows how BIND IF construct works. See if castles are close to mountains (a source of castle building material) SELECT ?item ?itemLabel ?coord ?layer WHERE { { ?item wdt:P31 wd:Q8502 . } UNION { ?item wdt:P31 wd:Q23413 . }

?item wdt:P625 ?coord .

BIND(
    IF(EXISTS {?item wdt:P31 wd:Q8502}, "Mountain",
    IF(EXISTS {?item wdt:P31 wd:Q23413}, "Castle",
       "Unknown"))
AS ?layer)

SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }

}

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# Castles and mountains: Shows how BIND IF construct works. See if castles are close to mountains (a source of castle building material)
SELECT ?item ?itemLabel ?coord ?layer
WHERE
{
    {
        ?item wdt:P31 wd:Q8502 .
    }
    UNION 
    {
        ?item wdt:P31 wd:Q23413 .
    }

    ?item wdt:P625 ?coord .

    BIND(
        IF(EXISTS {?item wdt:P31 wd:Q8502}, "Mountain",
        IF(EXISTS {?item wdt:P31 wd:Q23413}, "Castle",
           "Unknown"))
    AS ?layer)

    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?coord"):::projected v1("?item"):::projected v3("?layer"):::projected c6(["bd:serviceParam"]):::iri c8(["en"]):::literal c2(["wd:Q8502"]):::iri c3(["wd:Q23413"]):::iri subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P31"--> c3 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P31"--> c2 end union0r <== or ==> union0l end v1 --"wdt:P625"--> v2 v1 --"wdt:P31"--> c2 v1 --"wdt:P31"--> c3 bind0[/"if( ,'Mountain',if( ,'Castle','Unknown'))"/] subgraph bind0e0["Exists Clause"] e0v1 --"wdt:P31"--> e0c2 e0v1("?item"):::projected e0c2(["wd:Q8502"]):::iri end bind0--EXISTS--> bind0e0 subgraph bind0e1["Exists Clause"] e1v1 --"wdt:P31"--> e1c2 e1v1("?item"):::projected e1c2(["wd:Q23413"]):::iri end bind0--EXISTS--> bind0e1 v1 --o bind0 c1 --o bind0 c2 --o bind0 c3 --o bind0 bind0 --as--o v3 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c6 --"wikibase:language"--> c8 end