Reputation: 39
How do I get unit type and date precision in the Wikidata Query service (https://query.wikidata.org) with SPARQL?
Below is example query looking at dimensions and inception of tapestries.
SELECT ?item ?itemLabel ?height ?width ?inception
WHERE
{
?item wdt:P31 wd:Q184296 .
OPTIONAL {
?item wdt:P2048 ?height .
?item wdt:P2049 ?width .
?item wdt:P571 ?inception .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} LIMIT 1
I see on website that Bayeux Tapestry (https://www.wikidata.org/wiki/Q187483) is measured in metres and was made in 1070s. But when I use Query Service, I get numbers only for height and width. And Inception is 1070s on website, but Jan 1, 1070 on Query Service. I looked at documentation but can't figure out.
How can I get units and date precision on Query Service?
Upvotes: 2
Views: 598
Reputation: 8465
Getting information about statements and units works this way in Wikidata:
SELECT ?item ?itemLabel
?height ?unitHeightLabel
?width ?unitWidthLabel
?inception ?precisionLabel WHERE {
?item wdt:P31 wd:Q184296 .
OPTIONAL {
?item p:P2048 ?stmnodeHeight . # height
?stmnodeHeight psv:P2048 ?valuenodeHeight.
?valuenodeHeight wikibase:quantityAmount ?height.
?valuenodeHeight wikibase:quantityUnit ?unitHeight.
?item p:P2049 ?stmnodeWidth . # width
?stmnodeWidth psv:P2049 ?valuenodeWidth.
?valuenodeWidth wikibase:quantityAmount ?width.
?valuenodeWidth wikibase:quantityUnit ?unitWidth.
?item p:P571/psv:P571 ?timenode .
?timenode wikibase:timeValue ?inception.
?timenode wikibase:timePrecision ?timeprecision.
# get the label of time precision (by @StanislavKralin)
{
SELECT ?precision (xsd:integer(?precisionDecimal) AS ?timeprecision) {
?precision wdt:P2803 ?precisionDecimal .
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} LIMIT 1
A slightly more compact version using SPARQL 1.1 property paths + Turtle shortcuts:
SELECT ?item ?itemLabel
?height ?unitHeightLabel
?width ?unitWidthLabel
?inception ?precisionLabel WHERE {
?item wdt:P31 wd:Q184296 .
OPTIONAL {
# height
?item p:P2048/psv:P2048 [ wikibase:quantityAmount ?height ;
wikibase:quantityUnit ?unitHeight ].
# width
?item p:P2049/psv:P2049 [ wikibase:quantityAmount ?width ;
wikibase:quantityUnit ?unitWidth ].
# inception
?item p:P571/psv:P571 [ wikibase:timeValue ?inception;
wikibase:timePrecision ?timeprecision ]
# get the label of time precision (by @StanislavKralin)
{
SELECT ?precision (xsd:integer(?precisionDecimal) AS ?timeprecision) {
?precision wdt:P2803 ?precisionDecimal .
}
}
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
} LIMIT 1
Result:
+-------------+-----------------+--------+-----------------+-------+----------------+-------------+---------------+
| item | itemLabel | height | unitHeightLabel | width | unitWidthLabel | inception | precisionLabel |
+-------------+-----------------+--------+-----------------+-------+----------------+-------------+---------------+
| wd:Q187483 | Bayeux Tapestry | 0.5 | metre | 68.38 | metre | Jan 1, 1070 | 8 |
+-------------+-----------------+--------+-----------------+-------+----------------+-------------+---------------+
The codes for precision are 0: billion years, 1: hundred million years, 3: million years, 4: hundred thousand years, 5: ten thousand years, 6: millennium, 7: century, 8: decade, 9: year, 10: month, 11: day, 12: hour, 13: minute, 14: second.
Upvotes: 3