コモンズ:SPARQLクエリサービス/クエリ/例
このページはクエリ・サービスのウェブ・インタフェースでパースされてクエリ例のダイアログに出力されます。例の多くはListerbotがパースするtemplate:Wikidata listテンプレート内でも動作しますが、?item という項目変数の選択が必須です。
注意: 以下のコモンズSPARQLクエリの実行にはログインが必要です。
NOTE: Alternatively you can use the way faster QLever Wikimedia Commons instance which does not require login but is only updated once in a while. Last update was 2023-05-30.
シンプルなクエリ
これらの基本的なクエリはSPARQLや ウィキベースRDF形式の理解に役立ちます。
ダグラス・アダムズが題材のメディア
#shows M-entities that depict Douglas Adams
SELECT ?file WHERE {
?file wdt:P180 wd:Q42 .
}
ダグラス・アダムズが題材のメディアを画像グリッドで
ImageGrid表示モードは、表示可能かどうかを、image (P18)のようなウィキデータのプロパティの値として使われている形式のファイルのURLで調べます。
画像を閲覧するための文は ?file schema:url ?image.
#defaultView:ImageGrid
select ?file ?image where {
?file wdt:P180 wd:Q42.
?file schema:url ?image.
}
ミケランジェロによるダビデ像のデジタル複製
Files with digital representation of (P6243) set to David (Q179900) digital representation of (P6243) に David (Q179900)がセットされているファイル
# Digital depictions of "David" by Michelangelo
# Note how you can use a semicolon to group triples that have the same subject.
#defaultView:ImageGrid
select ?file ?image where {
?file wdt:P6243 wd:Q179900;
schema:url ?image.
}
"Wikimedia OTRS チケット数"の文があるファイルの数
Wikimedia VRTS ticket number (P6305 )文を持つファイル数
SELECT (COUNT(?file) AS ?count) {
?file wdt:P6305 ?value .
}
複数の"以下のデジタル複製"文を持つファイル
digital representation of (P6243) statements has "single value constraint", or in other words each file can be a digital representation of only one Wikidata item, (use depicts (P180) if there are more objects in the image). The query below finds constraint violations.
SELECT ?file (COUNT(?value) AS ?count) {
?file wdt:P6243 ?value .
}
GROUP BY ?file
HAVING ( ?count > 1 )
ORDER BY DESC(?count)
LIMIT 100
100×100 pxぴったりの画像を表示する画像グリッド
#defaultView:ImageGrid
select ?file ?image {
?file schema:contentUrl ?url;
schema:height 100;
schema:width 100;
schema:url ?image.
} limit 2000
ファイル種別ごとのファイル数
select ?encoding (count(*) as ?total) {
?file schema:encodingFormat ?encoding .
}
group by ?encoding
order by desc(?total)
サンプル10,000件の画像の高さ/幅を表示する散布図
#defaultView:ScatterChart
select * {
?file schema:height ?h ;
schema:width ?w .
} limit 10000
More random sample: https://w.wiki/55rr
"題材" (P180) = "橋" (Q12280)というタグを持つ画像のあるマップ
# Show a map of images depicting bridges AND having a coordinate for their point of view
#defaultView:Map
select ?file ?pov_coords ?image where {
?file wdt:P180 wd:Q12280;
wdt:P1259 ?pov_coords;
schema:url ?image.
}
バラが題材のファイルのキャプション
#Captions of files depicting roses
SELECT ?file ?fileLabel WHERE {
?file wdt:P180 wd:Q102231 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
種の識別にiNaturalistを利用しているナチュラリストが使っているカメラ
#Wikidata items of files in Category:Artworks with structured data with redirected P6243 property
SELECT DISTINCT ?capturedWith ?capturedWithLabel (count(?file) as ?counts)
WITH
{
SELECT ?file ?title
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:api "Generator" .
bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
bd:serviceParam mwapi:gcmtitle "Category:Media from iNaturalist" .
bd:serviceParam mwapi:generator "categorymembers" .
bd:serviceParam mwapi:gcmtype "file" .
bd:serviceParam mwapi:gcmlimit "max" .
?title wikibase:apiOutput mwapi:title .
?pageid wikibase:apiOutput "@pageid" .
}
BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
}
} AS %get_files
WHERE
{
INCLUDE %get_files
?file schema:url ?url ;
wdt:P4082 ?capturedWith .
SERVICE <https://query.wikidata.org/sparql> {
?capturedWith rdfs:label ?capturedWithLabel .
FILTER (lang(?capturedWithLabel) = "en")
}
}
GROUP BY ?capturedWith ?capturedWithLabel
ORDER BY DESC(?counts)
連携(フェデレーション)の利用
WARNING: The optimizer for Blazegraph has undesirable interactions with federated queries. Therefore, it's recommended that you turn off the optimizer completely or add a hint to run the federated portion first. This can be done with hint:Query hint:optimizer "None"
or hint:Prior hint:runFirst true
. Addtional info can be found at this WikidataCon 2017 presentation.
ウィキデータ項目のラベルと説明を検索
"ファイルのソース" (P7482)としてよく使われている値
- このクエリは連携を使って、最初の部分で見つけたウィキデータ項目用のウィキデータラベルを探します
# Most common values for "source of image" (P7482). Limited to a random sample of 100,000 images to avoid timeout.
SELECT ?count ?source ?source_label
WHERE {
{
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?source WHERE {
service bd:sample {
?file wdt:P7482 ?source .
bd:serviceParam bd:sample.limit 100000 .
bd:serviceParam bd:sample.sampleType "RANDOM" .
}
} GROUP BY ?source
}
SERVICE <https://query.wikidata.org/sparql> {
OPTIONAL {?source rdfs:label ?source_label FILTER (lang(?source_label) = 'en') } .
}
}
ORDER BY DESC(?count) ?source_label
- depicts (P180)に対する類似クエリ :
https://w.wiki/55fF
よく使われている"題材" (P180)に対する"表情、しぐさ、ポーズ" (P6022)修飾子の値
# Most common "expression, gesture or body pose" (P6022) qualifier values for "depicts" (P180)
SELECT ?count ?value ?value_label ?example
WITH {
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?value (SAMPLE(?file) AS ?example) WHERE {
?file p:P180/pq:P6022 ?value .
} GROUP BY ?value
ORDER BY DESC(?count)
LIMIT 2000
} AS %values
WHERE {
INCLUDE %values .
service <https://query.wikidata.org/sparql> {
OPTIONAL {?value rdfs:label ?value_label FILTER (lang(?value_label) = 'en') } .
}
}
ORDER BY DESC(?count) ?value_label
- restricted to humans:
tinyurl.com/y53kzn83
; non-humans:tinyurl.com/y35b5w3l
バラの色
- これは、画像の題材となっているバラの色項目のラベルを取得するのにWDQSへの連携を利用しています。
#defaultView:ImageGrid
prefix commons: <http://commons.wikimedia.org/wiki/Special:FilePath/>
select ?colorName ?image with {
select ?color (iri(replace(str(sample(?photo)), "^.*/", str(commons:))) as ?image) where {
[a schema:ImageObject] schema:contentUrl ?photo;
p:P180 [
ps:P180 wd:Q102231;
pq:P462 ?color
].
}
group by ?color
} as %roses where {
include %roses.
service <https://query.wikidata.org/sparql> {
service wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?color rdfs:label ?colorName.
}
}
}
興味のあるウィキデータ項目のセットを参照
バラのサブクラス
- これは、バラのサブクラスの項目とラベルを取得するのにWDQSを使っています
# Show all files depicting roses, and subclasses thereof
#defaultView:ImageGrid
select distinct ?item ?itemLabel ?image with {
select ?item ?itemLabel where {
service <https://query.wikidata.org/sparql> {
?item wdt:P31/wdt:P279* wd:Q34687. # subclasses of roses
service wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". ?item rdfs:label ?itemLabel . }
}
}
} as %wikidataItems where {
include %wikidataItems .
?file wdt:P180 ?item;
schema:url ?image.
} limit 1000
ファン・ゴッホの作品が題材になっているすべての画像
- Using federation to find all Wikidata items that have 'creator' (P170) as Vincent van Gogh (Q5582), then find all media items that depict those paintings and show in a grid.
#defaultView:ImageGrid
select ?image ?painting ?paintingLabel ?paintingDescription ?thumb with {
select * {
service <https://query.wikidata.org/sparql> {
?painting wdt:P170 wd:Q5582;
rdfs:label ?paintingLabel;
schema:description ?paintingDescription.
filter (lang(?paintingLabel) = "en")
filter (lang(?paintingDescription) = "en")
}
}
} as %paintings where {
include %paintings.
?image wdt:P180 ?painting;
schema:url ?thumb.
}
特定の教会及び"教会の建物"全般の両方が題材のものを検知
- Detecting redundancy of a "depicted" concept with something more specific based on Wikidata. Federation is used to retrieve all items that are instances of "church building".
#500 images that have both a specific church building and redundantly depict "church building" (Q16970)
select ?item ?church ?churchLabel ?churchDescription ?thumb with {
select * {
service <https://query.wikidata.org/sparql> {
?church wdt:P31 wd:Q16970;
rdfs:label ?churchLabel;
schema:description ?churchDescription.
filter (lang(?churchLabel) = "en")
filter (lang(?churchDescription) = "en")
}
}
} as %church where {
include %church.
?item wdt:P180 ?church.
?item wdt:P180 wd:Q16970.
}
LIMIT 500
ウィキデータ連携を使った、ドイツの雑誌'Die Gartenlaube'で出版されたイラスト
- This uses federation to find articles published in (P1433) the German magazine 'Die Gartenlaube', together with some information about those articles;
then, having retrieved that set, finds Commons files with the Commons statements that they were published in (P1433) those articles
#defaultView:ImageGrid{"hide":["?img2"]}
#defaultView:ImageGrid{"hide":["?img2"]}
select ?articleLabel ?articleDesc ?article ?img2 ?imgDesc with {
select * {
# SELECT all articles published in German magazine 'Die Gartenlaube'
service <https://query.wikidata.org/sparql> {
?article wdt:P1433 wd:Q655617;
rdfs:label ?articleLabel;
schema:description ?articleDesc.
filter(lang(?articleLabel) = "en")
filter(lang(?articleDesc) = "en")
}
}
} as %items where {
include %items .
# Select images published in any article.
?file wdt:P1433 ?article;
schema:url ?img2.
optional {?file rdfs:label ?imgDesc. filter(lang(?desc) = "de") } .
}
ヘルシンキという場所にある対象物の画像を、その対象物の作者と著作権状態付きで
# Images of objects located in Helsinki, together with authors of those objects and copyright status
#defaultView:ImageGrid
select ?image ?item ?itemLabel ?itemDescription ?thumb (?p170Label as ?author) (YEAR(?p570) as ?death) ?copyright_statusLabel with {
select * {
service <https://query.wikidata.org/sparql> {
?item wdt:P131* wd:Q1757 .
?item wdt:P170 ?p170 .
OPTIONAL { ?item rdfs:label ?itemLabel filter (lang(?itemLabel) = "en") }
OPTIONAL { ?item schema:description ?itemDescription filter (lang(?itemDescription) = "en") }
OPTIONAL { ?p170 rdfs:label ?p170Label filter (lang(?p170Label) = "en") }
OPTIONAL { ?p170 wdt:P570 ?p570 } .
OPTIONAL { ?p170 wdt:P7763 ?copyright_status.
?copyright_status rdfs:label ?copyright_statusLabel
filter (lang(?copyright_statusLabel) = "en")
}
}
}
} as %works where {
include %works.
?image wdt:P180 ?item;
schema:url ?thumb.
}
スウェーデンのWiki Loves Monumentsプロジェクトに参加しているファイルの地図、コンテスト年度で色分け
# Map of files participating in Wiki Loves Monuments in Sweden, color-coded by year of competition
#defaultView:Map
select ?image ?wlm ?wlmLabel ?username ?coords ?thumb ?layer
with {
select * {
service <https://query.wikidata.org/sparql> {
?wlm wdt:P179 wd:Q105194463;
rdfs:label ?wlmLabel;
filter (lang(?wlmLabel) = "en")
}
}
} as %items
where {
include %items.
?image wdt:P1344 ?wlm.
OPTIONAL {?image wdt:P1259 ?coords}
OPTIONAL {?image (p:P170/pq:P4174) ?username.}
?image schema:contentUrl ?url;
schema:url ?thumb.
BIND(
IF(?wlm = wd:Q105201214, "2011",
IF(?wlm = wd:Q105201215, "2012",
IF(?wlm = wd:Q105201216, "2013",
IF(?wlm = wd:Q105201217, "2014",
IF(?wlm = wd:Q105201218, "2015",
IF(?wlm = wd:Q105201219, "2016",
IF(?wlm = wd:Q105201220, "2017",
IF(?wlm = wd:Q105201221, "2018",
IF(?wlm = wd:Q105201222, "2019",
IF(?wlm = wd:Q105201223, "2020",
"")))))))))) AS ?layer).
}
Media files depicting (former) heads of state that are still alive
# Media files depicting (former) heads of state that are still alive
#defaultView:ImageGrid
select distinct ?file ?image ?leaderLabel with {
select ?leader ?leaderLabel where {
service <https://query.wikidata.org/sparql> {
?leader wdt:P31 wd:Q5;
wdt:P39 ?posHeld.
minus { ?leader wdt:P570 []. } # Minus date of death, only living people
?posHeld wdt:P31/wdt:P279* wd:Q48352. # Position held = subclass or instance of 'head of state'
service wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?leader rdfs:label ?leaderLabel.
}
}
}
} as %wikidataItems where {
include %wikidataItems .
?file wdt:P180 ?leader;
schema:url ?image.
}
連携クエリを使って、メトロポリタン美術館の作品のレポートを部門ごとに生成
# Number of Met Museum artworks in Commons, sorted by department
SELECT ?dept ?deptLabel ?count
WHERE {
{
# Use Wikidata federated query to generate curatorial departments of The Met
SELECT ?dept ?deptLabel WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?dept wdt:P31 wd:Q61781831 . # curatorial department
?dept wdt:P749 wd:Q160236 . # parent org being Met
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
?dept rdfs:label ?deptLabel . # Department labels
}
}
}
}
{
# Find files of The Met
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?dept WHERE {
?file wdt:P3634 ?metid . # Files with Met ID
?file wdt:P195 ?dept . # Department
} GROUP BY ?dept
}
}
ORDER BY DESC(?count) ?dept
Federation with Wikidata to group, analyse, or add information to Commons results
17世紀のAtlas De Witに見られる画像の地図
#defaultView:Map
select ?file ?image ?depictionLabel ?coord where {
?file wdt:P6243 wd:Q2520345 .
?file wdt:P180 ?depiction .
?file schema:url ?image .
service <https://query.wikidata.org/sparql> {
?depiction wdt:P625 ?coord.
service wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
?depiction rdfs:label ?depictionLabel .
}
}
}
Most common wikidata classes for values of "depicts" (P180) on Commons
# Most common classes of the values of "depicts" (P180) on Commons
SELECT ((?sum / 40) AS ?percent) ?class ?class_label ?instance ?instance_label ?sample
WITH {
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?value (SAMPLE(?file) AS ?example) WHERE {
service bd:sample {
?file wdt:P180 ?value .
bd:serviceParam bd:sample.limit 4000 .
bd:serviceParam bd:sample.sampleType "RANDOM" .
}
} GROUP BY ?value
ORDER BY DESC(?count)
# LIMIT 2000
} AS %values
WITH {
SELECT (SUM(?count) AS ?sum) ?class (SAMPLE(?value) AS ?instance) (SAMPLE(?example) AS ?sample) WHERE {
INCLUDE %values .
service <https://query.wikidata.org/sparql> {
?value wdt:P31 ?class
}
} GROUP BY ?class
} AS %classes
WHERE {
INCLUDE %classes .
service <https://query.wikidata.org/sparql> {
OPTIONAL {?instance rdfs:label ?instance_label FILTER (lang(?instance_label) = 'en') } .
OPTIONAL {?class rdfs:label ?class_label FILTER (lang(?class_label) = 'en') } .
}
}
ORDER BY DESC(?sum)
Federation with external sources
連携クエリを使ってヨーロピアナ項目の情報を取得
#Get information of Europeana item using federated query
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX edm: <http://www.europeana.eu/schemas/edm/>
PREFIX ore: <http://www.openarchives.org/ore/terms/>
SELECT * WHERE {
BIND(<http://data.europeana.eu/proxy/provider/91622/raa_kmb_16000200042758> as ?p854)
SERVICE <http://sparql.europeana.eu/> {
{
?p854 <http://purl.org/dc/terms/created> ?created .
?p854 <http://purl.org/dc/elements/1.1/identifier> ?identifier .
?p854 <http://purl.org/dc/elements/1.1/publisher> ?publisher .
?p854 <http://purl.org/dc/elements/1.1/rights> ?rights .
?p854 <http://purl.org/dc/elements/1.1/title> ?title .
?p854 <http://purl.org/dc/elements/1.1/description> ?description .
}
}
}
連携クエリを使ってヨーロピアナ所蔵品のコモンズカテゴリを読む
# Find Commons category suggestions for a Commons file using subjects stored in Europeana
# 1.) Read collection and identifier for M68435429 (File:Tervahovi_Oulu_1898_02.jpg)
# 2.) Read subjects in Finnish of photo defined by identifier from Europeana
# 3.) Translate subjects to YSO ontology using Finto-service.
# 3.b) Note: Query is routed through Sophox.org, because there is no direct access from WCQS to Finto.
# 4.) Translate YSO items to Wikidata items using Wikidata and read Commons categories
SELECT * WITH {
SELECT ?mediafile ?collection ?identifier ?europeana ?subject WHERE {
# 1.) Read collection and identifier for M68435429 (File:Tervahovi_Oulu_1898_02.jpg)
BIND(sdc:M68435429 as ?mediafile)
?mediafile p:P195 ?collection_prop .
?collection_prop ps:P195 ?collection .
?collection_prop pq:P217 ?identifier
# 2.) Read subjects in Finnish of photo defined by identifier from Europeana
SERVICE <http://sparql.europeana.eu/> {
?europeana <http://purl.org/dc/elements/1.1/identifier> ?identifier .
?europeana <http://purl.org/dc/elements/1.1/subject> ?subject .
}
}
} AS %europeana
WHERE {
INCLUDE %europeana .
# 3.) Translate subjects to YSO ontology using Finto-service.
# 3.b) Note: Query is routed through Sophox.org, because there is no direct access from WCQS to Finto.
SERVICE <http://zbw.eu/beta/sparql/stw/query> {
SERVICE <http://api.finto.fi/sparql> {
?yso skos:prefLabel ?subject ;
skos:inScheme <http://www.yso.fi/onto/yso/>
}
}
# 4.) Translate YSO items to Wikidata items using Wikidata and read Commons categories
BIND(REPLACE(STR(?yso), "http://www.yso.fi/onto/yso/p", "") as ?yso_number)
SERVICE <https://query.wikidata.org/sparql> {
?wikidata wdt:P2347 ?yso_number .
?wikidata wdt:P373 ?commonscat
}
}
利用者のアップロードを検索
ウィキメディア・コモンズの利用者'Coyau'が作者となっているファイルの世界地図
# World map of files authored by Wikimedia Commons user 'Coyau'
#defaultView:Map
select ?file ?image ?coordinates (?date AS ?layer) ?filename where {
bind("Coyau" AS ?username)
?file (p:P170/pq:P4174) ?username;
schema:url ?image;
?p ?coordinates. # any coordinate like wdt:P625,wdt:P1259,wdt:P9149
filter(datatype(?coordinates) = geo:wktLiteral). # If this line is not here, the query will be very slow
optional { ?file wdt:P571 ?date. }
?file schema:contentUrl ?url .
BIND (wikibase:decodeUri(CONCAT("File:", substr(str(?url),53))) AS ?filename).
}
LIMIT 10000
ウィキメディア・コモンズの利用者が作者となっているファイルのタイムライン
# Timeline of files authored by a Wikimedia Commons user
#TEMPLATE={"template":"Timeline of files authored by ?username","variables":{"?username":{"query":" SELECT DISTINCT ?username WHERE { [] p:P170/pq:P4174 ?username .} LIMIT 100"} } }
#defaultView:Timeline
SELECT ?file ?date WHERE {
BIND("Coyau" AS ?username)
?file (p:P170/pq:P4174) ?username;
wdt:P571 ?date.
}
LIMIT 1000
保守用クエリ
そのカメラの発売前に撮影されたファイル
select ?file ?file_date ?camera ?camera_date
with {
select * {
# SELECT all camera models
service <https://query.wikidata.org/sparql> {
?camera wdt:P31 wd:Q20741022;
wdt:P571 ?camera_date.
}
}
} as %cameras
where {
include %cameras .
#select images taken with the camera
?file wdt:P4082 ?camera;
wdt:P571 ?file_date.
FILTER(?file_date < ?camera_date)
} LIMIT 1000
コモンズのカテゴリを検索
Service wikibase:mwapi provides ability to combine SPARQL queries with Commons categories. Unfortunately only 10,000 files can be returned by that service, however that number can be extended as we can control which 10,000 by adding additional commands like
bd:serviceParam mwapi:gcmsort "timestamp" . bd:serviceParam mwapi:gcmdir "descending" .
Category:Artworks with structured data with redirected digital representation of propertyにあるファイルのウィキデータ項目
Maintenance category Category:Artworks with structured data with redirected digital representation of property collects files with digital representation of (P6243) statement where Wikidata item linked by that statement is a redirect to a different Wikidata item. The following query will show the filenames, the items linked by P6243 property and the items they redirect to. Be aware that the category is often empty, and even if it is not the redirects might have been already corrected.
SELECT ?file ?title ?redirect_from ?redirect_to
WITH
{
SELECT ?file ?title
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:api "Generator" .
bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
bd:serviceParam mwapi:gcmtitle "Category:Artworks with structured data with redirected digital representation of property" .
bd:serviceParam mwapi:generator "categorymembers" .
bd:serviceParam mwapi:gcmtype "file" .
bd:serviceParam mwapi:gcmlimit "max" .
?title wikibase:apiOutput mwapi:title .
?pageid wikibase:apiOutput "@pageid" .
}
BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
}
} AS %get_files
WHERE
{
INCLUDE %get_files
?file wdt:P6243 ?redirect_from .
SERVICE <https://query.wikidata.org/sparql> {
?redirect_from owl:sameAs ?redirect_to.
}
}
Depicts statements with Dutch labels, of files in one Commons category
SELECT ?file ?title ?depicts ?depicts_label
WITH
{
SELECT ?file ?title
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:api "Generator" .
bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
bd:serviceParam mwapi:gcmtitle "Category:Historia Naturalis van Rudolf II" .
bd:serviceParam mwapi:generator "categorymembers" .
bd:serviceParam mwapi:gcmtype "file" .
bd:serviceParam mwapi:gcmlimit "max" .
?title wikibase:apiOutput mwapi:title .
?pageid wikibase:apiOutput "@pageid" .
}
BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
}
} AS %get_files
WHERE
{
INCLUDE %get_files
?file wdt:P180 ?depicts .
service <https://query.wikidata.org/sparql> {
OPTIONAL {?depicts rdfs:label ?depicts_label FILTER (lang(?depicts_label) = 'nl') }
}
}
Camera location of files in a category
#defaultView:Map
SELECT ?file ?title ?image ?coords
WITH
{
SELECT ?file ?title
WHERE
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:api "Generator" .
bd:serviceParam wikibase:endpoint "commons.wikimedia.org" .
bd:serviceParam mwapi:gcmtitle "Category:River Ravensbourne" .
bd:serviceParam mwapi:generator "categorymembers" .
bd:serviceParam mwapi:gcmtype "file" .
bd:serviceParam mwapi:gcmlimit "max" .
?title wikibase:apiOutput mwapi:title .
?pageid wikibase:apiOutput "@pageid" .
}
BIND (URI(CONCAT('https://commons.wikimedia.org/entity/M', ?pageid)) AS ?file)
}
} AS %get_files
WHERE
{
INCLUDE %get_files
OPTIONAL {?file wdt:P1259 ?coords} . # coords of POV
OPTIONAL {?file wdt:P9149 ?coords} . # fallback: coords of depicted place
FILTER(bound(?coords)) .
?file schema:url ?image.
}
その他のクエリ
特定のプロパティがどのように使われているか調べるクエリ
以下のデジタル複製 (P6243)がいちばん使われているオブジェクト
コモンズ上でいちばん多くのファイルに digital representation of (P6243) 文を通してリンクしているウィキデータ項目
SELECT ?count ?item ?item_label ?commonscat_link
WITH {
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?item WHERE {
?file wdt:P6243 ?item .
} GROUP BY ?item
ORDER BY DESC(?count)
LIMIT 200
} AS %items
WHERE {
INCLUDE %items .
service <https://query.wikidata.org/sparql> {
OPTIONAL {?item rdfs:label ?item_label FILTER (lang(?item_label) = 'en') } .
OPTIONAL {
?item wdt:P373 ?commonscat .
BIND(IRI(CONCAT("https://commons.wikimedia.org/wiki/Category:", ?commonscat)) AS ?commonscat_link) .
}
}
} ORDER BY DESC(?count) ?item_label
表現されたオブジェクトの種類ごとの"以下のデジタル複製"(P6243)の数
Note -- limited to a sample of 50,000 files, although we (currently) have 225124 files with digital representation of (P6243) statements, as 50,000 seems to be as large a number as can be sent by federation to WDQS for analysis without timeout
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?class ?class_label
WITH {
SELECT ?file ?item WHERE {
?file wdt:P6243 ?item .
} LIMIT 50000
} AS %files
WHERE {
INCLUDE %files .
service <https://query.wikidata.org/sparql> {
?item wdt:P31 ?class .
OPTIONAL {?class rdfs:label ?class_label FILTER (lang(?class_label) = 'en') } .
}
} GROUP BY ?class ?class_label
ORDER BY DESC(?count) ?class_label
- より効果的なバージョン: https://w.wiki/55rv (ランダムサンプルによる)
"ファイルのソース" (P7482) = "インターネット上で利用可能なファイル" (Q74228490) である時に最も使われている修飾子
# # Most frequent qualifier properties when "source of image" (P7482) = "file available on the internet" (Q74228490)
# based on sample of 100,000 such images
SELECT ?count ?qualifier ?qualifier_label ?example ?example_value ?example_value_label
WITH {
SELECT ?file ?source_stmt WHERE {
service bd:sample {
?source_stmt ps:P7482 wd:Q74228490 .
bd:serviceParam bd:sample.limit 100000 .
bd:serviceParam bd:sample.sampleType "RANDOM" .
}
?file p:P7482 ?source_stmt .
}
} AS %files
WITH {
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?qualifier (SAMPLE(?file) AS ?example) (SAMPLE(?value) AS ?example_value) WHERE {
INCLUDE %files .
?file p:P7482 ?source_stmt .
?source_stmt ?qualifier ?value .
} GROUP BY ?qualifier
ORDER BY DESC(?count)
LIMIT 200
} AS %source_info
WHERE {
INCLUDE %source_info .
service <https://query.wikidata.org/sparql> {
?qualifier ^wikibase:qualifier ?qual_item .
OPTIONAL {?qual_item rdfs:label ?qualifier_label FILTER (lang(?qualifier_label) = 'en') } .
OPTIONAL {?example_value rdfs:label ?example_value_label FILTER (lang(?example_value_label) = 'en') } .
}
}
ORDER BY DESC(?count)
"ファイルのソース" (P7482) = "インターネット上で利用可能なファイル" (Q74228490) である時に最も使われている画像の提供者
(based on a random sample of 10,000 images with such provider statements)
# Most frequent providers of images when "source of image" (P7482) = "file available on the internet" (Q74228490)
SELECT ?count ?provider ?provider_label ?example
WITH {
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?provider (SAMPLE(?file) AS ?example) WHERE {
service bd:sample {
?source_stmt ps:P7482 wd:Q74228490 .
bd:serviceParam bd:sample.limit 10000 .
bd:serviceParam bd:sample.sampleType "RANDOM" .
}
?file p:P7482 ?source_stmt .
?source_stmt pq:P137 ?provider .
} GROUP BY ?provider
ORDER BY DESC(?count)
LIMIT 200
} AS %source_info
WHERE {
INCLUDE %source_info .
service <https://query.wikidata.org/sparql> {
OPTIONAL {?provider rdfs:label ?provider_label FILTER (lang(?provider_label) = 'en') } .
}
}
ORDER BY DESC(?count)
"題材" (P180)の値が最大規模だが、推奨値の無い画像
(from a non-random sample of 500,000 images)
# Images with largest number of depicts values
#defaultView:ImageGrid
SELECT ?count ?image (GROUP_CONCAT(?topicLabel; separator = ' / ') AS ?topicLabels)
WITH {
SELECT ?file WHERE {
?file a wikibase:Mediainfo .
} LIMIT 500000
} AS %files
WITH {
SELECT ?file ?topic WHERE {
INCLUDE %files .
MINUS {?file p:P180/wikibase:rank wikibase:PreferredRank} .
?file wdt:P180 ?topic .
}
} AS %file_topics
WITH {
SELECT (COUNT(DISTINCT(?topic)) AS ?count) ?file WHERE {
INCLUDE %file_topics .
} GROUP BY ?file
ORDER BY DESC(?count)
LIMIT 200
} AS %top_files
WITH {
SELECT ?count ?file ?topic WHERE {
INCLUDE %file_topics .
INCLUDE %top_files .
}
} AS %top_file_topics
WITH {
SELECT DISTINCT ?topic WHERE {
INCLUDE %top_file_topics .
}
} AS %top_topics
WITH{
SELECT ?topic ?topicLabel WHERE {
INCLUDE %top_topics .
service <https://query.wikidata.org/sparql> {
service wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?topic rdfs:label ?topicLabel.
}
}
}
} AS %topicLabels
WHERE {
INCLUDE %top_file_topics .
INCLUDE %topicLabels .
?file schema:contentUrl ?url .
bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
} GROUP BY ?count ?file ?image
ORDER BY DESC(?count)
重複した "題材" (P180) 文を持つファイル
Find all the files which have the same value added twice to the same property. In this example we used depicts (P180) statement, but it can be used with others as well. This query sometimes times out, but removing "DISTINCT" from it fixes the problem, but returns multiple rows per file
SELECT DISTINCT ?file {
?file p:P180 ?statement1.
?file p:P180 ?statement2.
?statement1 ps:P180 ?value .
?statement2 ps:P180 ?value .
FILTER(?statement1 != ?statement2)
}
limit 50
ネコが題材の物
# Things depicted with cats
#defaultView:ImageGrid
SELECT ?count ?depicted ?depictedLabel ?sampleImg
WITH {
SELECT ?target WHERE {
SERVICE <https://query.wikidata.org/sparql> {
{ ?target wdt:P31?/wdt:P279* wd:Q146 } ## include specific sorts of cats and individual cats
UNION
{ ?target wdt:P31? wd:Q43577 } ## and also breeds of cat
}
}
} AS %targets
WITH {
SELECT (COUNT(DISTINCT(?file)) AS ?count) ?depicted (SAMPLE(?file) AS ?sample) WHERE {
INCLUDE %targets .
?file p:P180 ?stmt .
?stmt ps:P180 ?target .
# ?stmt wikibase:rank wikibase:PreferredRank . ## Make sure the cat is marked as prominent -- **commented out for now**
?file p:P180 ?stmt2. FILTER (?stmt2 != ?stmt) . ## now get another depicts statement
?stmt2 ps:P180 ?depicted .
} GROUP BY ?depicted
} AS %things
WITH {
SELECT ?count ?depicted ?sample WHERE {
INCLUDE %things .
MINUS { ## exclude anything that was an original target
SELECT (?target AS ?depicted) WHERE {
INCLUDE %targets .
}
}
}
} AS %things1
WHERE {
INCLUDE %things1 .
SERVICE <https://query.wikidata.org/sparql> {
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?depicted rdfs:label ?depictedLabel .
}
}
?sample schema:contentUrl ?url;
schema:url ?sampleImg.
} ORDER BY DESC(?count)
- with a more random set of sampled images:
tinyurl.com/y35wjrz8
(choice will be different each time the query is modified, or if it is no longer in cache)
中心から1km付近で撮影された画像
#defaultView:Map{"hide":["?coor"]}
# query by Jura1, 2020-11-12
SELECT ?fileLabel ?fileDescription ?image ?coor
WHERE
{
hint:Query hint:optimizer "None".
SERVICE <https://query.wikidata.org/sparql> { wd:Q43244 wdt:P625 ?center } # Wikidata item with coordinates
SERVICE wikibase:around {
?file wdt:P1259 ?coor.
bd:serviceParam wikibase:center ?center .
bd:serviceParam wikibase:radius "1". # 1 kilometer around
}
?file schema:contentUrl ?url .
bind(iri(concat("http://commons.wikimedia.org/wiki/Special:FilePath/", wikibase:decodeUri(substr(str(?url),53)))) AS ?image)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
- 中心は d:Q43244#P625 の座標で決まる
ウィキメディア・コモンズの評価
アスレティック競技者が題材の「ウィキメディア・コモンズの価値ある画像」
#shows files that depict athletics competitors and with the Valued Image assessment
#defaultView:ImageGrid
SELECT ?file ?image
WITH {
SELECT ?item WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?item wdt:P106 wd:Q11513337 .
}
}
} AS %get_items
WHERE {
INCLUDE %get_items
?file wdt:P180 ?item .
?file wdt:P6731 wd:Q63348040 . # Commons valued image
?file schema:url ?image .
}
ハミングバードが題材の「ウィキメディア・コモンズの秀逸な画像」
#shows files that depict Hummingbirds and with the Quality Image assessment
#defaultView:ImageGrid
SELECT ?file ?image
WITH {
SELECT ?item WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?item wdt:P171/wdt:P171* wd:Q43624.
}
}
} AS %get_items
WHERE {
INCLUDE %get_items
?file wdt:P180 ?item .
?file wdt:P6731 wd:Q63348069 . # Commons quality image
?file schema:url ?image .
}
イタリアのアーチ橋が題材の「ウィキメディア・コモンズの秀逸な画像」
#shows files that depict arch bridges in Italy and with the Quality Image assessment
#defaultView:ImageGrid
SELECT ?file ?image
WITH {
SELECT ?item WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?item wdt:P31 wd:Q158438 .
?item wdt:P17 wd:Q38 .
}
}
} AS %get_items
WHERE {
INCLUDE %get_items
?file wdt:P180 ?item .
?file schema:url ?image .
?file wdt:P6731 wd:Q63348069 .
}
Lepidopteraが題材でキャノンのカメラで撮影された「ウィキメディア・コモンズの今日の一枚」
#shows files that depict Lepidoptera, with the Featured Picture assessment, and taken with a Canon camera
#defaultView:ImageGrid
SELECT ?file ?image
WITH {
SELECT ?item WHERE {
SERVICE <https://query.wikidata.org/sparql> {
?item wdt:P171/wdt:P171* wd:Q28319.
}
}
} AS %get_items
WHERE {
INCLUDE %get_items
?file wdt:P180 ?item .
?file schema:url ?image .
?file wdt:P6731 wd:Q63348049 .
?file wdt:P4082 ?camera.
SERVICE <https://query.wikidata.org/sparql> {
?camera wdt:P1716 wd:Q63554165 . # brand = Canon
}
}
コモンズでよく使われているプロパティ
よく使われているダグラス・アダムズが題材のコモンズ項目上の述語
#most common predictates on Commons items that depict Douglas Adams
SELECT (COUNT(DISTINCT(CONCAT(str(?file),str(?value)))) AS ?count) ?prop (SAMPLE(?file) AS ?sample_file) (SAMPLE(?value) AS ?sample_value)
WHERE {
?file wdt:P180 wd:Q42 .
?file ?prop ?value .
} GROUP BY ?prop
ORDER BY DESC(?count)
Predicates/Properties and their natural language labels used on commons items that depict Douglas Adams
SELECT DISTINCT ?prop ?propLabel WHERE {
?file wdt:P180 wd:Q42 ;
schema:contentUrl ?contentURL ;
?p ?o .
SERVICE <https://query.wikidata.org/sparql> {
?prop wikibase:claim ?p ;
rdfs:label ?propLabel .
FILTER (lang(?propLabel) = "en")
}
}
サンプルのコモンズファイル5,000件上でよく使われている述語
This query uses federation to look up the properties' names on Wikidata (a task which is a little more involved for properties than for regular Wikidata items).
A series of named subqueries (i.e. the WITH blocks) are used to force the sequence of execution -- see on Wikidata for a little more about this.
#most common predictates on a sample of 5,000 Commons files
SELECT (COUNT(DISTINCT ?file) AS ?file_count) (COUNT(DISTINCT(CONCAT(str(?file),str(?val)))) AS ?stmt_count)
?prop ?prop_label (SAMPLE(?file) AS ?sample_file) (SAMPLE(?val) AS ?sample_val)
WITH {
SELECT ?file WHERE {
service bd:sample {
?file a wikibase:Mediainfo .
bd:serviceParam bd:sample.limit 5000 .
bd:serviceParam bd:sample.sampleType "RANDOM" .
}
}
} AS %files
WITH {
SELECT ?file ?prop ?val WHERE {
INCLUDE %files .
?file ?prop ?val .
}
} AS %triples
WITH {
SELECT DISTINCT ?prop WHERE {
INCLUDE %triples .
}
} AS %props
WITH {
SELECT ?prop ?prop_label WHERE {
INCLUDE %props .
service <https://query.wikidata.org/sparql> {
OPTIONAL {
?prop ^wikibase:directClaim ?prop_item .
?prop_item rdfs:label ?prop_label FILTER (lang(?prop_label) = 'en') .
}
OPTIONAL {
?prop ^wikibase:claim ?prop_item .
?prop_item rdfs:label ?prop_label FILTER (lang(?prop_label) = 'en') .
}
}
}
} AS %prop_labels
WHERE {
INCLUDE %triples .
INCLUDE %prop_labels .
} GROUP BY ?prop ?prop_label
ORDER BY DESC(?stmt_count)
- A later 5000:
tinyurl.com/yyuywxqm
"題材"文でよく使われている修飾子
# Most common qualifiers used on "depicts" statements
SELECT DISTINCT ?count ?qual ?qual_label ?sample_file ('depicts' AS ?prop) ?sample_stmt_value ?sample_stmt_valueLabel (?qual_label AS ?qualLabel1) ?sample_value ?sample_valueLabel
WITH {
SELECT ?qual WHERE {
service <https://query.wikidata.org/sparql> {
?prop wikibase:qualifier ?qual .
}
}
} AS %quals
WITH {
SELECT ?stmt ?qual WHERE {
INCLUDE %quals .
?stmt ?qual [] .
?file p:P180 ?stmt .
} LIMIT 30000
} AS %stmts
WITH {
SELECT (COUNT(DISTINCT(?stmt)) AS ?count) ?qual (SAMPLE(?stmt) AS ?example_stmt) WHERE {
INCLUDE %stmts .
} GROUP BY ?qual
} AS %top_quals
WITH {
SELECT ?count ?qual (SAMPLE(?example_file) AS ?sample_file) (SAMPLE(?example_stmt_value) AS ?sample_stmt_value) (SAMPLE(?example_value) AS ?sample_value) WHERE {
INCLUDE %top_quals .
?example_stmt ^p:P180 ?example_file .
?example_stmt ps:P180 ?example_stmt_value .
?example_stmt ?qual ?example_value .
} GROUP BY ?count ?qual
} AS %results
WHERE {
INCLUDE %results .
service <https://query.wikidata.org/sparql> {
?qual ^wikibase:qualifier ?qual_item
service wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?qual_item rdfs:label ?qual_label.
?sample_stmt_value rdfs:label ?sample_stmt_valueLabel .
?sample_value rdfs:label ?sample_valueLabel .
}
}
} ORDER BY DESC(?count)
"題材" (P180)文以外で、よく使われているプロパティ+修飾子の組み合わせ
# most common property + qualifier combinations, other than on "depicts" (P180) statements
SELECT ?count ?pred ?pred_label ?qual ?qual_label ?example
WITH {
SELECT ?qual WHERE {
service <https://query.wikidata.org/sparql> {
?prop wikibase:qualifier ?qual .
}
}
} AS %quals
WITH {
SELECT ?file ?stmt ?qual ?pred WHERE {
INCLUDE %quals .
?stmt ?qual [] .
OPTIONAL {?f1 p:P180 ?stmt} FILTER(!bound(?f1)) .
?file ?pred ?stmt .
} LIMIT 100000
} AS %stmts
WITH {
SELECT (COUNT(DISTINCT(?stmt)) AS ?count) ?qual ?pred (SAMPLE(?file) AS ?example) WHERE {
INCLUDE %stmts .
} GROUP BY ?qual ?pred
} AS %top_quals
WHERE {
INCLUDE %top_quals .
service <https://query.wikidata.org/sparql> {
OPTIONAL {
?pred ^wikibase:claim ?pred_item .
?pred_item rdfs:label ?pred_label FILTER (lang(?pred_label) = 'en') .
}
OPTIONAL {
?qual ^wikibase:qualifier ?qual_item .
?qual_item rdfs:label ?qual_label FILTER (lang(?qual_label) = 'en') .
}
}
} ORDER BY DESC(?count)
Most frequently used depicts statements from a slice of media files that don't have translations in Spanish
SELECT ?depicts ?count ?depictsLabel_en ?depictsLabel_es WITH {
SELECT ?depicts (count(?file) as ?count) WHERE
{
SERVICE bd:slice {
?file wdt:P180 ?depicts.
bd:serviceParam bd:slice.offset 10000000 . # Start at item number (not to be confused with QID)
bd:serviceParam bd:slice.limit 3000000 . # List this many items to prevent query timeout
}
} group by ?depicts having (?count>10 && ?count<4000000) } AS %I
WHERE
{
INCLUDE %I
SERVICE <https://query.wikidata.org/sparql> {
filter not exists {?depicts rdfs:label ?depictsLabel_es FILTER (lang(?depictsLabel_es) = 'es') }
OPTIONAL {?depicts rdfs:label ?depictsLabel_en FILTER (lang(?depictsLabel_en) = 'en') }
}
} order by desc(?count)
Generating edits
Generate Quickstatements directly from SPARQL
Here is a SPARQL query to search for Met images with a specific depiction statement and to delete those statements using Quickstatements. This method allows one to do mass search and removal, without needing to learn how to code a bot or to program.
Rationale: There are some low confidence P180/depiction statements to remove, so search for those statements with a specific qualifier "stated in" Met tagging initiative and with a reference statement. Then generate a list of Quickstatement directives we can copy/paste directly into Quickstatements using SPARQL string functions. Example:
- -STATEMENT | M58764743$D563143B-0F5C-403E-AE11-4BB744B7E267
Hint: When using Quickstatements to operate on Commons, make sure to select "Create new command batch for: Commons [batch mode only!]" as by default it works on Wikidata.
Useful reference for string functions can be found at: https://docs.cambridgesemantics.com/anzograph/v2.5/userdoc/string-functions.htm
SELECT DISTINCT ?quickstatement WHERE {
# Wikidata items of P180 terms we want to remove
VALUES ?exclude { wd:Q467 wd:Q8441 }
?file wdt:P3634 ?metid .
?file p:P180 ?fullstatement .
?fullstatement ps:P180 ?exclude .
# All depictions w/ determination method Met tagging initiative,
# and a specific reference statement.
# This avoid deleting a depiction statement that might have been
# added by another method (by hand, other source, etc)
?fullstatement ps:P180 []; pq:P459 wd:Q106429444 ; prov:wasDerivedFrom ?ref .
?ref pr:P248 ?statedin . # Met tagging initiative
?ref pr:P854 ?refurl . # Some type of URL, typically https://collectionapi.metmuseum.org/public/collection/v1/objects/...
# ?file schema:url ?image.
# Want to construct a Quickstatement to remove this exact claim/statement
BIND(REPLACE(STR(?file), "https://commons.wikimedia.org/entity/", "") as ?mid)
BIND(REPLACE(STR(?fullstatement), "https://commons.wikimedia.org/entity/statement/", "") as ?statement)
BIND(STRAFTER(?statement,"-") as ?statementdetail)
BIND(CONCAT("-STATEMENT", "|", ?mid, "$" , ?statementdetail) as ?quickstatement)
}