支援的版本:目前 (17) / 16 / 15 / 14 / 13
開發版本:devel
不支援的版本:12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1

9.11. 幾何函數和運算子 #

幾何類型 pointboxlseglinepathpolygoncircle 有大量的原生支援函數和運算子,如表 9.36表 9.37表 9.38 所示。

表 9.36. 幾何運算子

運算子

描述

範例

geometric_type + pointgeometric_type

將第二個 point 的座標加到第一個參數的每個點的座標上,從而執行平移。適用於 pointboxpathcircle

box '(1,1),(0,0)' + point '(2,0)'(3,1),(2,0)

path + pathpath

串連兩個開放路徑 (如果任一路徑封閉,則傳回 NULL)。

path '[(0,0),(1,1)]' + path '[(2,2),(3,3),(4,4)]'[(0,0),(1,1),(2,2),(3,3),(4,4)]

geometric_type - pointgeometric_type

從第一個參數的每個點的座標中減去第二個 point 的座標,從而執行平移。適用於 pointboxpathcircle

box '(1,1),(0,0)' - point '(2,0)'(-1,1),(-2,0)

geometric_type * pointgeometric_type

將第一個引數的每個點乘以第二個 point(將點視為由實部和虛部表示的複數,並執行標準複數乘法)。如果將第二個 point 解釋為向量,則這相當於將物件的大小和與原點的距離按向量的長度縮放,並圍繞原點逆時針旋轉向量與x軸的角度。適用於 pointbox[a] pathcircle

path '((0,0),(1,0),(1,1))' * point '(3.0,0)'((0,0),(3,0),(3,3))

path '((0,0),(1,0),(1,1))' * point(cosd(45), sind(45))((0,0),​(0.7071067811865475,0.7071067811865475),​(0,1.414213562373095))

geometric_type / pointgeometric_type

將第一個引數的每個點除以第二個 point (將點視為由實部和虛部表示的複數,並執行標準複數除法)。如果將第二個 point 解釋為向量,這相當於將物件的大小和與原點的距離縮小向量的長度,並將其繞原點順時針旋轉向量與 x 軸的角度。適用於 pointbox[a] pathcircle

path '((0,0),(1,0),(1,1))' / point '(2.0,0)'((0,0),(0.5,0),(0.5,0.5))

path '((0,0),(1,0),(1,1))' / point(cosd(45), sind(45))((0,0),​(0.7071067811865476,-0.7071067811865476),​(1.4142135623730951,0))

@-@ geometric_typedouble precision

計算總長度。適用於 lsegpath

@-@ path '[(0,0),(1,0),(1,1)]'2

@@ geometric_typepoint

計算中心點。適用於 boxlsegpolygoncircle

@@ box '(2,2),(0,0)'(1,1)

# geometric_typeinteger

傳回點的數量。適用於 pathpolygon

# path '((1,0),(0,1),(-1,0))'3

geometric_type # geometric_typepoint

計算交點,如果沒有交點則傳回 NULL。適用於 lsegline

lseg '[(0,0),(1,1)]' # lseg '[(1,0),(0,1)]'(0.5,0.5)

box # boxbox

計算兩個方塊的交集,如果沒有交集則傳回 NULL。

box '(2,2),(-1,-1)' # box '(1,1),(-2,-2)'(1,1),(-1,-1)

geometric_type ## geometric_typepoint

計算在第二個物件上最靠近第一個物件的點。適用於以下類型配對:(point, box), (point, lseg), (point, line), (lseg, box), (lseg, lseg), (line, lseg)。

point '(0,0)' ## lseg '[(2,0),(0,2)]'(1,1)

geometric_type <-> geometric_typedouble precision

計算物件之間的距離。適用於所有七種幾何類型,所有 point 與其他幾何類型的組合,以及以下額外的類型配對:(box, lseg), (lseg, line), (polygon, circle) (以及交換律的情況)。

circle '<(0,0),1>' <-> circle '<(5,0),1>'3

geometric_type @> geometric_typeboolean

第一個物件是否包含第二個物件?適用於以下類型配對:(box, point), (box, box), (path, point), (polygon, point), (polygon, polygon), (circle, point), (circle, circle)。

circle '<(0,0),2>' @> point '(1,1)'t

geometric_type <@ geometric_typeboolean

第一個物件是否包含在或位於第二個物件上?適用於以下類型配對:(point, box), (point, lseg), (point, line), (point, path), (point, polygon), (point, circle), (box, box), (lseg, box), (lseg, line), (polygon, polygon), (circle, circle)。

point '(1,1)' <@ circle '<(0,0),2>'t

geometric_type && geometric_typeboolean

這些物件是否重疊?(有一個共同點就為真。)適用於 boxpolygoncircle

box '(1,1),(0,0)' && box '(2,2),(0,0)'t

geometric_type << geometric_typeboolean

第一個物件是否嚴格位於第二個物件的左側?適用於 pointboxpolygoncircle

circle '<(0,0),1>' << circle '<(5,0),1>'t

geometric_type >> geometric_typeboolean

第一個物件是否嚴格位於第二個物件的右側?適用於 pointboxpolygoncircle

circle '<(5,0),1>' >> circle '<(0,0),1>'t

geometric_type &< geometric_typeboolean

第一個物件是否未延伸到第二個物件的右側?適用於 boxpolygoncircle

box '(1,1),(0,0)' &< box '(2,2),(0,0)'t

geometric_type &> geometric_typeboolean

第一個物件是否未延伸到第二個物件的左側?適用於 boxpolygoncircle

box '(3,3),(0,0)' &> box '(2,2),(0,0)'t

geometric_type <<| geometric_typeboolean

第一個物件是否完全低於第二個物件?適用於 pointboxpolygoncircle

box '(3,3),(0,0)' <<| box '(5,5),(3,4)'t

geometric_type |>> geometric_typeboolean

第一個物件是否完全高於第二個物件?適用於 pointboxpolygoncircle

box '(5,5),(3,4)' |>> box '(3,3),(0,0)'t

geometric_type &<| geometric_typeboolean

第一個物件是否未超出第二個物件的上方?適用於 boxpolygoncircle

box '(1,1),(0,0)' &<| box '(2,2),(0,0)'t

geometric_type |&> geometric_typeboolean

第一個物件是否未超出第二個物件的下方?適用於 boxpolygoncircle

box '(3,3),(0,0)' |&> box '(2,2),(0,0)'t

box <^ boxboolean

第一個物件是否低於第二個物件 (允許邊緣接觸)?

box '((1,1),(0,0))' <^ box '((2,2),(1,1))'t

box >^ boxboolean

第一個物件是否高於第二個物件 (允許邊緣接觸)?

box '((2,2),(1,1))' >^ box '((1,1),(0,0))'t

geometric_type ?# geometric_typeboolean

這些物件是否相交?適用於下列類型組合:(boxbox)、(lsegbox)、(lseglseg)、(lsegline)、(linebox)、(lineline)、(pathpath)。

lseg '[(-1,0),(1,0)]' ?# box '(2,2),(-2,-2)'t

?- lineboolean

?- lsegboolean

線是否為水平線?

?- lseg '[(-1,0),(1,0)]'t

point ?- pointboolean

點是否為水平對齊(即,具有相同的 y 座標)?

point '(1,0)' ?- point '(0,0)'t

?| lineboolean

?| lsegboolean

線是否為垂直線?

?| lseg '[(-1,0),(1,0)]'f

point ?| pointboolean

點是否為垂直對齊(即,具有相同的 x 座標)?

point '(0,1)' ?| point '(0,0)'t

line ?-| lineboolean

lseg ?-| lsegboolean

線是否互相垂直?

lseg '[(0,0),(0,1)]' ?-| lseg '[(0,0),(1,0)]'t

line ?|| lineboolean

lseg ?|| lsegboolean

線是否互相平行?

lseg '[(-1,0),(1,0)]' ?|| lseg '[(-1,2),(1,2)]'t

geometric_type ~= geometric_typeboolean

這些物件是否相同?適用於 pointboxpolygoncircle

polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'t

[a] 旋轉 使用這些運算子的方塊只會移動其角點:該方塊仍被視為其邊與軸平行。因此,方塊的大小不會被保留,如同真正的旋轉那樣。


注意

請注意,「相同於」運算子 ~= 代表 pointboxpolygoncircle 類型的通常相等概念。某些幾何類型也有一個 = 運算子,但 = 僅比較相等的面積。其他純量比較運算子(<= 等等),如果可用於這些類型,也同樣比較面積。

注意

PostgreSQL 14 之前,點的完全低於/高於比較運算子 point <<| pointpoint |>> point 分別稱為 <^>^。這些名稱仍然可用,但已被棄用,並最終將被移除。

表 9.37. 幾何函數

函數

描述

範例

area ( geometric_type ) → double precision

計算面積。適用於 boxpathcirclepath 輸入必須是封閉的,否則將傳回 NULL。此外,如果 path 是自相交的,則結果可能沒有意義。

area(box '(2,2),(0,0)')4

center ( geometric_type ) → point

計算中心點。適用於 boxcircle

center(box '(1,2),(0,0)')(0.5,1)

diagonal ( box ) → lseg

將方塊的對角線提取為線段(與 lseg(box) 相同)。

diagonal(box '(1,2),(0,0)')[(1,2),(0,0)]

diameter ( circle ) → double precision

計算圓的直徑。

diameter(circle '<(0,0),2>')4

height ( box ) → double precision

計算方塊的垂直大小。

height(box '(1,2),(0,0)')2

isclosed ( path ) → boolean

路徑是否為封閉?

isclosed(path '((0,0),(1,1),(2,0))')t

isopen ( path ) → boolean

路徑是否為開放?

isopen(path '[(0,0),(1,1),(2,0)]')t

length ( geometric_type ) → double precision

計算總長度。適用於 lsegpath

length(path '((-1,0),(1,0))')4

npoints ( geometric_type ) → integer

傳回點的數量。適用於 pathpolygon

npoints(path '[(0,0),(1,1),(2,0)]')3

pclose ( path ) → path

將路徑轉換為封閉形式。

pclose(path '[(0,0),(1,1),(2,0)]')((0,0),(1,1),(2,0))

popen ( path ) → path

將路徑轉換為開放形式。

popen(path '((0,0),(1,1),(2,0))')[(0,0),(1,1),(2,0)]

radius ( circle ) → double precision

計算圓的半徑。

radius(circle '<(0,0),2>')2

slope ( point, point ) → double precision

計算通過兩點的線的斜率。

slope(point '(0,0)', point '(2,1)')0.5

width ( box ) → double precision

計算盒子的水平尺寸。

width(box '(1,2),(0,0)')1


表格 9.38. 幾何類型轉換函數

函數

描述

範例

box ( circle ) → box

計算圓內接的盒子。

box(circle '<(0,0),2>')(1.414213562373095,1.414213562373095),​(-1.414213562373095,-1.414213562373095)

box ( point ) → box

將點轉換為空盒子。

box(point '(1,0)')(1,0),(1,0)

box ( point, point ) → box

將任意兩個角點轉換為盒子。

box(point '(0,1)', point '(1,0)')(1,1),(0,0)

box ( polygon ) → box

計算多邊形的邊界框。

box(polygon '((0,0),(1,1),(2,0))')(2,1),(0,0)

bound_box ( box, box ) → box

計算兩個盒子的邊界框。

bound_box(box '(1,1),(0,0)', box '(4,4),(3,3)')(4,4),(0,0)

circle ( box ) → circle

計算包圍盒子的最小圓。

circle(box '(1,1),(0,0)')<(0.5,0.5),0.7071067811865476>

circle ( point, double precision ) → circle

從中心和半徑構造圓。

circle(point '(0,0)', 2.0)<(0,0),2>

circle ( polygon ) → circle

將多邊形轉換為圓。圓的中心是多邊形點的位置的平均值,半徑是多邊形點與該中心的平均距離。

circle(polygon '((0,0),(1,3),(2,0))')<(1,1),1.6094757082487299>

line ( point, point ) → line

將兩個點轉換為通過它們的線。

line(point '(-1,0)', point '(1,0)'){0,-1,0}

lseg ( box ) → lseg

將盒子的對角線提取為線段。

lseg(box '(1,0),(-1,0)')[(1,0),(-1,0)]

lseg ( point, point ) → lseg

從兩個端點構造線段。

lseg(point '(-1,0)', point '(1,0)')[(-1,0),(1,0)]

path ( polygon ) → path

將多邊形轉換為具有相同點列表的封閉路徑。

path(polygon '((0,0),(1,1),(2,0))')((0,0),(1,1),(2,0))

point ( double precision, double precision ) → point

從其坐標構造點。

point(23.4, -44.5)(23.4,-44.5)

point ( box ) → point

計算盒子的中心。

point(box '(1,0),(-1,0)')(0,0)

point ( circle ) → point

計算圓的中心。

point(circle '<(0,0),2>')(0,0)

point ( lseg ) → point

計算線段的中心。

point(lseg '[(-1,0),(1,0)]')(0,0)

point ( polygon ) → point

計算多邊形的中心(多邊形點的位置的平均值)。

point(polygon '((0,0),(1,1),(2,0))')(1,0.3333333333333333)

polygon ( box ) → polygon

將盒子轉換為 4 點多邊形。

polygon(box '(1,1),(0,0)')((0,0),(0,1),(1,1),(1,0))

polygon ( circle ) → polygon

將圓轉換為 12 點多邊形。

polygon(circle '<(0,0),2>')((-2,0),​(-1.7320508075688774,0.9999999999999999),​(-1.0000000000000002,1.7320508075688772),​(-1.2246063538223773e-16,2),​(0.9999999999999996,1.7320508075688774),​(1.732050807568877,1.0000000000000007),​(2,2.4492127076447545e-16),​(1.7320508075688776,-0.9999999999999994),​(1.0000000000000009,-1.7320508075688767),​(3.673819061467132e-16,-2),​(-0.9999999999999987,-1.732050807568878),​(-1.7320508075688767,-1.0000000000000009))

polygon ( integer, circle ) → polygon

將圓轉換為 n 點多邊形。

polygon(4, circle '<(3,0),1>')((2,0),​(3,1),​(4,1.2246063538223773e-16),​(3,-1))

polygon ( path ) → polygon

將封閉路徑轉換為具有相同點列表的多邊形。

polygon(path '((0,0),(1,1),(2,0))')((0,0),(1,1),(2,0))


您可以像存取陣列一樣,使用索引 0 和 1 來存取 point(點)的兩個組成數字。例如,如果 t.p 是一個 point 欄位,那麼 SELECT p[0] FROM t 會取得 X 坐標,而 UPDATE t SET p[1] = ... 會變更 Y 坐標。同樣地,box(盒子)或 lseg(線段)類型的值也可以被視為由兩個 point 值組成的陣列。

提交更正

如果您在文件中發現任何不正確、與您特定功能的使用經驗不符,或需要進一步澄清的地方,請使用此表單回報文件問題。