Jump to content

SPARQL/WIKIDATA Precision, Units and Coordinates

From Wikibooks, open books for an open world
Wikidata data values

The data on WIKIDATA contains more info than only the triples. For a full description see Wikidata:Glossary.

Values on Wikidata often have additional info like precision, units etc. Wikidata's solution for almost everything is more triples. And it means more prefixes.

Entities

[edit | edit source]

For entities there is no additional information.

Strings

[edit | edit source]

For strings there is no additional information.

# examples of dates, precision, time zones and calendars
SELECT ?time ?timeprecision ?timezone ?timecalendar ?timecalendarLabel
WHERE
{
     { wd:Q5598  p:P569/psv:P569 ?timenode. }  # Jul 15, 1606
     UNION 
     { wd:Q220   p:P571/psv:P571 ?timenode. } # 13 April 753 BCE
     UNION 
     { wd:Q1     p:P580/psv:P580 ?timenode. } # 13798 million years BCE
  
     ?timenode wikibase:timeValue         ?time.
     ?timenode wikibase:timePrecision     ?timeprecision.
     ?timenode wikibase:timeTimezone      ?timezone.
     ?timenode wikibase:timeCalendarModel ?timecalendar.
  
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it!

The prefix p: points to a statement node.
The prefix psv: within a statement node retrieves a time node.
The wikibase:timeValue within the time node retrieves the time.
The wikibase:timePrecision within a time node retrieves the precision of the time.

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.

The wikibase:timeTimezone within a time node retrieves the timezone, as an offset from UTC in minutes.
The wikibase:timeCalendarModel within a time node retrieves the calendar, a common used value is proleptic Gregorian calendar (Q1985727).


Comment on filtering on dates.
On filtering dates the code ^^xsd:dateTime should be added, like:

FILTER("2015-01-01"^^xsd:dateTime <= ?dob && ?dob < "2016-01-01"^^xsd:dateTime).

Monolingual texts

[edit | edit source]

For monolingual text there is no additional information. The text is represented as a string literal with language tag. It has only simple value.

#Countries in European Union with native name and language
SELECT ?country ?countryLabel ?nativename ?language
{
  wd:Q458 wdt:P150 ?country.   # European Union  contains administrative territorial entity
  OPTIONAL { ?country wdt:P1705 ?nativename.
              BIND( LANG(?nativename) AS ?language). }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?language)

Try it!

Coordinates

[edit | edit source]
# Museums in Barcelona with coordinates
SELECT ?item ?itemLabel ?coord ?lon ?lat
WHERE
{
 ?item wdt:P131 wd:Q1492.   # in the administrative territory of Barcelona
 ?item wdt:P31 wd:Q33506.   # is a museum
 ?item p:P625 ?coordinate.
 ?coordinate ps:P625 ?coord.
 ?coordinate psv:P625 ?coordinate_node.
 ?coordinate_node wikibase:geoLongitude ?lon.
 ?coordinate_node wikibase:geoLatitude ?lat.  
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } 
}

Try it!

The prefix p: points to a statement node.
The prefix ps: within a statement node for a coordinate retrieves the full coordinate, like Point(2.1749 41.3834).
The prefix psv: within a statement node retrieves a coordinate node.
The wikibase:geoLongitude within the coordinate node retrieves the longitude value.
The wikibase:geoLatitude within a coordinate node retrieves the latitude value.
The wikibase:geoGlobe within a coordinate node retrieves the globe object. For coordinates on earth it will be Earth (Q2).
The wikibase:geoPrecision within a coordinate node retrieves the precision of the coordinate values, measured in degrees. Multiply by 111000 to convert to meters.

Here an example of mountains not located on Earth.

# Mountains, with coordinates, not located on Earth
SELECT ?item ?name ?coord ?lon ?lat ?globe ?globeLabel
{
   ?item wdt:P31 wd:Q8502;                 # is a mountain
         p:P625 [
           ps:P625 ?coord;
           psv:P625 [
             wikibase:geoLongitude ?lon;
             wikibase:geoLatitude ?lat;
             wikibase:geoGlobe ?globe;
           ] ;
         ]
  FILTER ( ?globe != wd:Q2 )              # globe is not earth
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .
                           ?item  rdfs:label ?name.
                           ?globe rdfs:label ?globeLabel.
                         }
}
ORDER BY ?globeLabel ?name

Try it!

Quantities

[edit | edit source]
# Longest rivers in the USA
SELECT ?item ?itemLabel ?length ?unitLabel ?lowerbound ?upperbound ?precision ?length2 ?conversion ?length_in_m 
WHERE
{
  ?item          wdt:P31/wdt:P279*           wd:Q4022.    # rivers
  ?item          wdt:P17                     wd:Q30.      # country USA
  ?item          p:P2043                     ?stmnode.    # length
  ?stmnode       psv:P2043                   ?valuenode.
  ?valuenode     wikibase:quantityAmount     ?length.
  ?valuenode     wikibase:quantityUnit       ?unit.
  ?valuenode     wikibase:quantityLowerBound ?lowerbound.
  ?valuenode     wikibase:quantityUpperBound ?upperbound.
  BIND((?upperbound-?lowerbound)/2 AS ?precision).
  BIND(IF(?precision=0, ?length, (CONCAT(str(?length), "±", str(?precision)))) AS ?length2). 

  # conversion to SI unit
  ?unit          p:P2370                 ?unitstmnode.   # conversion to SI unit
  ?unitstmnode   psv:P2370               ?unitvaluenode. 
  ?unitvaluenode wikibase:quantityAmount ?conversion.
  ?unitvaluenode wikibase:quantityUnit   wd:Q11573.      # meter
  BIND(?length * ?conversion AS ?length_in_m).
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 
ORDER BY DESC(?length_in_m)
LIMIT 10

Try it!

The prefix p: points to a statement node.
The prefix psv: within a statement node retrieves a value node.
The wikibase:quantityAmount within the value node retrieves a quantity value.
The wikibase:quantityUnit within a value node retrieves a unit. Not all quantities have units.
The wikibase:quantityLowerBound and wikibase:quantityUpperBound can be used to indicate the precision.

Within the Unit you can retrieve the statement for converting the unit to the SI unit etc. See in the example the length of the Mississippi River is in miles, and can be converted to SI units. The conversion could also be needed if some lengths are in meters and some in kilometers to so.

Some variables are used only for demonstration. Without those the query will be:

# Longest rivers in the USA
SELECT ?item ?itemLabel ?length2 ?unitLabel ?length_in_m 
WHERE
{
  ?item          wdt:P31/wdt:P279*           wd:Q4022.    # rivers
  ?item          wdt:P17                     wd:Q30.      # country USA
  ?item          p:P2043                     ?stmnode.    # length
  ?stmnode       psv:P2043                   ?valuenode.
  ?valuenode     wikibase:quantityAmount     ?length.
  ?valuenode     wikibase:quantityUnit       ?unit.
  ?valuenode     wikibase:quantityLowerBound ?lowerbound.
  ?valuenode     wikibase:quantityUpperBound ?upperbound.
  BIND((?upperbound-?lowerbound)/2 AS ?precision).
  BIND(IF(?precision=0, ?length, (CONCAT(str(?length), "±", str(?precision)))) AS ?length2). 

  # conversion to SI unit
  ?unit          p:P2370                 ?unitstmnode.   # conversion to SI unit
  ?unitstmnode   psv:P2370               ?unitvaluenode. 
  ?unitvaluenode wikibase:quantityAmount ?conversion.
  ?unitvaluenode wikibase:quantityUnit   wd:Q11573.      # meter
  BIND(?length * ?conversion AS ?length_in_m).
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 
ORDER BY DESC(?length_in_m)
LIMIT 10

Try it!

The code can be abbreviated a lot with the [ ] syntax by eliminating the variables for the nodes and unused variables.

# Longest rivers in the USA
SELECT ?item ?itemLabel ?length2 ?unitLabel ?length_in_m 
WHERE
{
  ?item  wdt:P31/wdt:P279* wd:Q4022.    # rivers
  ?item  wdt:P17           wd:Q30.      # country USA
  ?item  p:P2043/psv:P2043 [            # length
     wikibase:quantityAmount     ?length;
     wikibase:quantityUnit       ?unit;
     wikibase:quantityLowerBound ?lowerbound;
     wikibase:quantityUpperBound ?upperbound;
  ]
  BIND((?upperbound-?lowerbound)/2 AS ?precision).
  BIND(IF(?precision=0, ?length, (CONCAT(str(?length), "±", str(?precision)))) AS ?length2). 

  # conversion to SI unit
  ?unit p:P2370/psv:P2370 [                # conversion to SI unit
     wikibase:quantityAmount ?conversion;
     wikibase:quantityUnit wd:Q11573;      # meter
  ]
  BIND(?length * ?conversion AS ?length_in_m).
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 
ORDER BY DESC(?length_in_m)
LIMIT 10

Try it!

Normalised units

[edit | edit source]

Instead of converting by SPARQL code as above the units can be converted automatically by using prefix psn: (normalised) instead of psv:.

# Longest rivers in the USA, normalised units
SELECT ?item ?itemLabel ?length ?unitLabel ?lowerbound ?upperbound ?precision ?length2
WHERE
{
  ?item          wdt:P31/wdt:P279*           wd:Q4022.    # rivers
  ?item          wdt:P17                     wd:Q30.      # country USA
  ?item          p:P2043                     ?stmnode.    # length
  ?stmnode       psn:P2043                   ?valuenode.  # normalised value
  ?valuenode     wikibase:quantityAmount     ?length.
  ?valuenode     wikibase:quantityUnit       ?unit.
  ?valuenode     wikibase:quantityLowerBound ?lowerbound.
  ?valuenode     wikibase:quantityUpperBound ?upperbound.
  BIND((?upperbound-?lowerbound)/2 AS ?precision).
  BIND(IF(?precision=0, ?length, (CONCAT(str(?length), "±", str(?precision)))) AS ?length2). 
    
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 
ORDER BY DESC(?length)
LIMIT 10

Try it!

The prefix psv: within a statement node retrieves a value node.
The prefix psn: within a statement node retrieves a normalised value node. Note that a query with psn: will give you only the values with units presents in this list.[1]

Notice that the units changed from km and miles into meters, and that the values are all calculated accordingly. Also the lower- and upper-bound values change accordingly. The precision now is in meters.

Normalized quantity values are value nodes that are parallel to the original data nodes but represented in base units. They are connected to their parent nodes by predicates with prefix having "v" replaced with "n" - i.e. psn:, prn: (for references) and pqn: (for qualifiers).

The code can be abbreviated a lot with the [ ] syntax by eliminating the variables for the nodes and unused variables.

# Longest rivers in the USA, normalised units
SELECT ?item ?itemLabel ?length2 ?unitLabel
WHERE
{
  ?item  wdt:P31/wdt:P279* wd:Q4022.    # rivers
  ?item  wdt:P17           wd:Q30.      # country USA
  ?item  p:P2043/psn:P2043 [            # length, normalised
     wikibase:quantityAmount     ?length;
     wikibase:quantityUnit       ?unit;
     wikibase:quantityLowerBound ?lowerbound;
     wikibase:quantityUpperBound ?upperbound;
  ]
  BIND((?upperbound-?lowerbound)/2 AS ?precision).
  BIND(IF(?precision=0, ?length, (CONCAT(str(?length), "±", str(?precision)))) AS ?length2). 

  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} 
ORDER BY DESC(?length)
LIMIT 10

Try it!

Summary

[edit | edit source]
item Statement node Value node
p:Pxxx
psv:Pxxx
or psn:Pxxx (normalised)
Time values
wikibase:timeValue
wikibase:timePrecision
wikibase:timeTimezone
wikibase:timeCalendarModel
Coordinate values
wikibase:geoLongitude
wikibase:geoLatitude
wikibase:geoGlobe
wikibase:geoPrecision
Quantity values
wikibase:quantityAmount
wikibase:quantityUnit
wikibase:quantityLowerBound
wikibase:quantityUpperBound
wikibase:quantityAmount

SPARQL data representation, as used by Wikidata Query Service


  1. You can see, by example, that these 2 queries don't give exactly the same values. Some values are missing in the second one.
    #Humans with height > 2,25 m
    SELECT ?taillem ?item
    WHERE
    {?item wdt:P31 wd:Q5 ;
           p:P2048 [psv:P2048 ?t ] .
      ?t wikibase:quantityAmount ?taille .
      ?t wikibase:quantityUnit/p:P2370/psv:P2370 [wikibase:quantityAmount ?conversion ; wikibase:quantityUnit wd:Q11573] .
      BIND(?taille * ?conversion AS ?taillem).
     filter(?taillem > 2.25)
     }
    order by desc (?taillem)
    
    Try it!
    #Humans with height > 2,25 m
    SELECT ?taille ?item
    WHERE
    {?item wdt:P31 wd:Q5 ;
           p:P2048/psn:P2048 [wikibase:quantityAmount ?taille ].
     filter(?taille > 2.25)
     }
    order by desc (?taille)
    
    Try it!