跳轉到內容

SPARQL/WIKIDATA 精度、單位和座標

來自華夏公益教科書
維基資料資料值

WIKIDATA 上的資料包含的資訊不僅僅是三元組。有關完整描述,請參閱維基資料:詞彙表.

維基資料上的值通常具有附加資訊,例如精度、單位等。維基資料對幾乎所有事物的解決方案都是更多三元組。這意味著更多字首

對於實體,沒有其他資訊。

對於字串,沒有其他資訊。

# 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". }
}

試試吧!

字首p:指向一個語句節點。
字首psv:在語句節點內檢索一個時間節點。
時間節點內的wikibase:timeValue檢索時間。
時間節點內的wikibase:timePrecision檢索時間的精度。

精度的程式碼為 0:十億年,1:一億年,3:百萬年,4:十萬年,5:一萬年,6:千年,7:世紀,8:十年,9:年,10:月,11:日,12:小時,13:分鐘,14:秒。

時間節點內的wikibase:timeTimezone檢索時區,以分鐘為單位的UTC偏移量。
時間節點內的wikibase:timeCalendarModel檢索日曆,一個常用的值為推算格里高利曆 (Q1985727).


關於日期過濾的評論。
在過濾日期時,應新增程式碼^^xsd:dateTime,例如

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

單語文字

[編輯 | 編輯原始碼]

對於單語文字,沒有其他資訊。文字表示為帶有語言標記的字串文字。它只有簡單的值。

#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)

試試吧!

# 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". } 
}

試試吧!

字首p:指向一個語句節點。
字首ps:在座標語句節點內檢索完整的座標,例如Point(2.1749 41.3834)。
字首psv:在語句節點內檢索一個座標節點。
座標節點內的wikibase:geoLongitude檢索經度值。
座標節點內的wikibase:geoLatitude檢索緯度值。
座標節點內的wikibase:geoGlobe檢索地球物體。對於地球上的座標,它將是地球 (Q2).
座標節點內的wikibase:geoPrecision檢索座標值的精度,以度為單位。乘以111000轉換為米。

以下是一些不在地球上的山脈的示例。

# 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

試試吧!

# 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

試試吧!

字首p:指向一個語句節點。
字首psv:在語句節點內檢索一個值節點。
值節點內的wikibase:quantityAmount檢索數量值。
值節點內的wikibase:quantityUnit檢索單位。並非所有數量都有單位。
wikibase:quantityLowerBoundwikibase:quantityUpperBound可用於指示精度。

在單位內,您可以檢索將單位轉換為SI單位等的語句。在示例中,密西西比河的長度以英里為單位,可以轉換為SI單位。如果某些長度以米為單位,而另一些以公里為單位,則也可能需要轉換。

一些變數僅用於演示。沒有它們,查詢將是

# 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

試試吧!

透過使用[ ] 語法,可以透過消除節點變數和未使用的變數來縮短程式碼。

# 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

試試吧!

標準化單位

[編輯 | 編輯原始碼]

使用字首psn:(標準化)代替psv:,可以自動轉換單位,而不是透過SPARQL程式碼進行轉換。

# 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

試試吧!

字首psv:在語句節點內檢索一個值節點。
字首psn:在語句節點內檢索一個標準化值節點。請注意,使用psn:的查詢將只為您提供此列表中存在的單位的值。[1]

注意,單位從公里和英里更改為米,所有值都相應地計算。上下限值也相應改變。現在的精度以米為單位。

標準化數量值是與原始資料節點平行的值節點,但以基本單位表示。它們透過字首具有“v”替換為“n”的謂詞連線到其父節點 - 即 psn:prn:(用於引用)和 pqn:(用於限定詞)。

透過使用 [ ] 語法,可以透過消除節點變數和未使用的變數來縮短程式碼。

# 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

試試吧!

專案 語句節點 值節點
p:Pxxx
psv:Pxxx
或 psn:Pxxx(標準化)
時間值
wikibase:timeValue
wikibase:timePrecision
wikibase:timeTimezone
wikibase:timeCalendarModel
座標值
wikibase:geoLongitude
wikibase:geoLatitude
wikibase:geoGlobe
wikibase:geoPrecision
數量值
wikibase:quantityAmount
wikibase:quantityUnit
wikibase:quantityLowerBound
wikibase:quantityUpperBound
wikibase:quantityAmount

SPARQL data representation, as used by Wikidata Query Service


  1. 例如,您可以看到這兩個查詢並沒有給出完全相同的值。第二個查詢中缺少一些值。
    #身高超過2.25米的男性 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) 
    試試看!
    #身高超過2.25米的男性 SELECT ?taille ?item WHERE {?item wdt:P31 wd:Q5 ; p:P2048/psn:P2048 [wikibase:quantityAmount ?taille ]. filter(?taille > 2.25) } order by desc (?taille) 
    試試看!
華夏公益教科書