query-fcdbb76bea7a57a82ef66880390ca4ce
Files with coordinate location in categories not connected to WikidataHello Dipsacus, only you can help me, with your immense experience with WCQS :) The query below is probably crap but somewhat works. Yet I think it only displays results from only SOME subcategories of a given category. Where is the mistake? I'd especially love if it only showed pictures from categories which are NOT connected to Wikidata yet, because it would tip me off about images which can be used to geo-localize the Commons category.
Use at
- https://query.wikidata.org/sparql
PREFIX mwapi: <http://wikiba.se/ontology#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX schema: <http://schema.org/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?file ?coord WITH {
SELECT ?out ?depth WHERE {
SERVICE <https://query.wikidata.org/bigdata/namespace/categories/sparql>
{
SERVICE mediawiki:categoryTree
{ bd:serviceParam mediawiki:start <https://commons.wikimedia.org/wiki/Category:Schools_in_Liberec_District> .
bd:serviceParam mediawiki:direction "Reverse" .
bd:serviceParam mediawiki:depth 3 .}
}
} ORDER BY ASC(?depth) } as %categories WITH {
SELECT ?out ?depth ?title ?member ?ns ?contentUrl WHERE {
include %categories
SERVICE <https://query.wikidata.org/sparql> {
?out schema:name ?title .
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "commons.wikipedia.org";
wikibase:api "Generator";
mwapi:generator "categorymembers";
mwapi:gcmtitle ?title;
mwapi:gcmlimit "max".
?member wikibase:apiOutput mwapi:title.
?ns wikibase:apiOutput "@ns".
}
BIND (CONCAT("http://commons.wikimedia.org/wiki/Special:FilePath/",REPLACE(SUBSTR(STR(?member),6)," ","_")) as ?image) .
BIND (REPLACE(wikibase:decodeUri(SUBSTR(STR(?image), 52)), " ", "_") AS ?filename)
BIND (REPLACE(SUBSTR(STR(?image), 52), "%20", "_") AS ?filenameUnencoded)
BIND (MD5(?filename) AS ?MD5)
BIND (URI(CONCAT("https://upload.wikimedia.org/wikipedia/commons/", SUBSTR(?MD5, 1, 1), "/", SUBSTR(?MD5, 1, 2), "/", ?filenameUnencoded)) As ?contentUrl)
FILTER (?ns = "6")
} } } as %images WITH {
select * WHERE {
include %images
?file schema:contentUrl ?contentUrl .
} } as %files where {
include %files
optional {?file wdt:P1259 ?coord1 .}
optional {?file wdt:P9149 ?coord2 .}
bind((coalesce(?coord2,?coord1)) as ?coord ) filter(bound(?coord)) .
}