User:Bjohas/Request a query

From Wikidata
Jump to navigation Jump to search

Query to get locations of wikidata items with article names

[edit]

Hello! I'm interested in working on cross checking data between (various) wikipedias and OpenStreetMap. I've been using the mediawiki API, but there's limitations as to what you can fetch (see mediawiki-api list). I'd like to see whether I can get the data using a wikidata query instead. However, I'm completely new to SPARQL, and would really appreciate some help.

Query 1

[edit]

My objective is to retrieve all geolocated entries (wikidata, wikipedias etc) in a rectangular area (specified by two lat/lon pairs), so that these can be compared to what OpenStreetMap has in that area. Using the wiki data service, is this possible? (I've come across cases where a wikidata item didn't have coordinates, but one of the language wikipedias did. So this would be the ideal query.) Wikidata items would need to include claims ( including coordinates and commons category), while wikipedia items would need to contain coordinates, categories, wikidata item, and wikipedia language links.

Alternative

[edit]

However, if that's not possible, then I'd like to retrieve all wikidata items in a rectangle, together with the corresponding wikipedia pages. For each wikidata item, I would like to retrieve the data as just described, e.g. suppose Q1034758 is in the rectangle, then I would like to retrieve

I'd like to output in JSON. Any help would be greatly appreciated! Bjohas (talk) 11:12, 2 May 2017 (UTC)

Bjohas, not exactly what you need, but can be a starting point:
SELECT ?item ?itemLabel ?typeLabel ?location ?commCat ?sitelink
WHERE {
  SERVICE wikibase:box {
    ?item wdt:P625 ?location .
    bd:serviceParam wikibase:cornerSouthWest "Point(2.8 39.3)"^^geo:wktLiteral .
    bd:serviceParam wikibase:cornerNorthEast "Point(2.9 39.5)"^^geo:wktLiteral .
  }
  OPTIONAL { ?item wdt:P31 ?type . }
  OPTIONAL { ?item wdt:P373 ?commCat . }
  OPTIONAL { ?sitelink schema:about ?item ; schema:isPartOf <https://en.wikipedia.org/> }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
Vort (talk) 05:18, 4 May 2017 (UTC)
That's really helpful thanks! How would I add several wikipedias, e.g. en,es,ca,de ? Adding "OPTIONAL { ?sitelink schema:about ?item ; schema:isPartOf <https://ca.wikipedia.org/> }" didn't work, in that while it adds the "ca" entry, only one entry is provided (i.e. only one of en,es,ca,de, rather than all). Sorry, I'm really new to this. Bjohas (talk) 13:08, 5 May 2017 (UTC)
You have to make another variable for that. Look at this one:
SELECT ?item ?itemLabel ?typeLabel ?location ?commCat ?sitelinkEn ?sitelinkCa
WHERE {
  SERVICE wikibase:box {
    ?item wdt:P625 ?location .
    bd:serviceParam wikibase:cornerSouthWest "Point(2.8 39.3)"^^geo:wktLiteral .
    bd:serviceParam wikibase:cornerNorthEast "Point(2.9 39.5)"^^geo:wktLiteral .
  }
  OPTIONAL { ?item wdt:P31 ?type . }
  OPTIONAL { ?item wdt:P373 ?commCat . }
  OPTIONAL { ?sitelinkEn schema:about ?item ; schema:isPartOf <https://en.wikipedia.org/> }
  OPTIONAL { ?sitelinkCa schema:about ?item ; schema:isPartOf <https://ca.wikipedia.org/> }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!
Q.Zanden questions? 14:27, 5 May 2017 (UTC)
Amazing, thank you! Bjohas (talk) 15:23, 5 May 2017 (UTC)