query-43dd9ea9dbec4ba8f6b35a01115c60ce

rq turtle/ttl

yield less results; I also interpreted “all the images” as “return as many images as possible”.(Q2934)goat or (Q172923)Capra , because lower levels such as (Q189804)Caprinae I interpreted “goat species” as anything below

Use at

PREFIX mwapi: <http://wikiba.se/ontology#>
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#>
#defaultView:ImageGrid
SELECT DISTINCT ?image ?species ?speciesLabel
# find all the goat species
# get their labels separately, to avoid looking up labels many times when we have several images
# get images from Wikimedia Commons
# get images from Wikidata, just in case those aren’t annotated on Commons (though I did at least manually add P180 to the Q2934 (goat) P18s)
# combine the Commons and Wikidata images (note that we have SELECT DISTINCT at the top to remove duplicates again) and add labels
WHERE {
  {  {
  SELECT ?image ?species WHERE {

     {
  SELECT DISTINCT ?species WHERE {
    ?species (wdt:P171|wdt:P279)* wd:Q189804. # apparently (some?) goat species are subclasses of rather than subtaxons of goat?
  }
}
    BIND(STRAFTER(STR(?species), STR(wd:)) AS ?itemId)
    BIND(CONCAT("haswbstatement:P180=", ?itemId) AS ?search)
    SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:endpoint "commons.wikimedia.org";
                      wikibase:api "Search";
                      mwapi:srsearch ?search;
                      mwapi:srnamespace "6"; # NS_FILE
                      mwapi:srlimit "max".
      ?title wikibase:apiOutput mwapi:title.
    }
    BIND(URI(CONCAT("http://commons.wikimedia.org/wiki/Special:FilePath/", ENCODE_FOR_URI(STRAFTER(?title, "File:")))) AS ?image)
  }
} } UNION
  {  {
  SELECT ?image ?species WHERE {
     {
  SELECT DISTINCT ?species WHERE {
    ?species (wdt:P171|wdt:P279)* wd:Q189804. # apparently (some?) goat species are subclasses of rather than subtaxons of goat?
  }
}
    ?species wdt:P18 ?image.
  }
} }
   {
  SELECT ?species ?speciesLabel WHERE {
     {
  SELECT DISTINCT ?species WHERE {
    ?species (wdt:P171|wdt:P279)* wd:Q189804. # apparently (some?) goat species are subclasses of rather than subtaxons of goat?
  }
}
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }
}
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v5("?image"):::projected v2("?itemId") v3("?search") v1("?species"):::projected v4("?title") c14(["max"]):::literal c2(["wd:Q189804"]):::iri c5(["bd:serviceParam"]):::iri c12(["6"]):::literal c9(["Search"]):::literal c20(["#91;AUTO_LANGUAGE#93;,en"]):::literal c16(["mwapi:title"]):::iri c7(["commons.wikimedia.org"]):::literal subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P279"--> c2 end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P171"--> c2 end union1r <== or ==> union1l end v1 --"wdt:P18"--> v5 end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; subgraph union2[" Union "] subgraph union2l[" "] style union2l fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P279"--> c2 end subgraph union2r[" "] style union2r fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P171"--> c2 end union2r <== or ==> union2l end bind0[/"substring-after(str(?species),str('wd:'))"/] v1 --o bind0 bind0 --as--o v2 bind1[/"concat('haswbstatement:P180=',?itemId)"/] v2 --o bind1 bind1 --as--o v3 subgraph s1["http://wikiba.se/ontology#mwapi"] style s1 stroke-width:4px; c5 --"mwapi:endpoint"--> c7 c5 --"mwapi:api"--> c9 c5 --"mwapi:srsearch"--> v3 c5 --"mwapi:srnamespace"--> c12 c5 --"mwapi:srlimit"--> c14 v4 --"mwapi:apiOutput"--> c16 end bind2[/"concat('http://commons.wikimedia.org/wiki/Special:FilePath/',encode-for-uri(substring-after(?title,'File:')))"/] v4 --o bind2 bind2 --as--o v5 end union0r <== or ==> union0l end subgraph union3[" Union "] subgraph union3l[" "] style union3l fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P279"--> c2 end subgraph union3r[" "] style union3r fill:#abf,stroke-dasharray: 3 3; v1 --"wdt:P171"--> c2 end union3r <== or ==> union3l end subgraph s2["http://wikiba.se/ontology#label"] style s2 stroke-width:4px; c5 --"mwapi:language"--> c20 end