天天看點

postgresql----幾何類型和函數postgresql----幾何類型和函數

postgresql----幾何類型和函數

postgresql支援的幾何類型如下表:

名字 存儲空間 描述 表現形式
point 16位元組 平面上的點 (x,y)
line 32位元組 直線 {A,B,C}
lseg 32位元組 線段 ((x1,y1),(x2,y2))
box 32位元組 矩形 ((x1,y1),(x2,y2))
path 16+16n位元組 閉合路徑 ((x1,y1),...)
path 16+16n位元組 開放路徑 [(x1,y1),...]
polygon 40+16n位元組 多邊形 ((x1,y1),...)
circle 24位元組 <(x,y),r> 

示例:

test=# select point'(1,1)';
 point 
-------
 (1,1)
(1 row)

test=# select line'{1,1,1}';
  line   
---------
 {1,1,1}
(1 row)

test=# select lseg'(1,1),(2,2)';
     lseg      
---------------
 [(1,1),(2,2)]
(1 row)

test=# select box'(1,1),(2,2)';
     box     
-------------
 (2,2),(1,1)
(1 row)

test=# select path'(1,1),(2,2),(2,1)';
        path         
---------------------
 ((1,1),(2,2),(2,1))
(1 row)

test=# select path'[(1,1),(2,2),(2,1)]';
        path         
---------------------
 [(1,1),(2,2),(2,1)]
(1 row)

test=# select polygon'((1,1),(2,2),(2,1))';
       polygon       
---------------------
 ((1,1),(2,2),(2,1))
(1 row)

test=# select circle'<(0,0),1>';
  circle   
-----------
 <(0,0),1>
(1 row)
           

操作符

操作符 描述 示例 結果
+ 平移 select box '((0,0),(1,1))' + point '(2.0,0)'; (3,1),(2,0)
- 平移 select box '((0,0),(1,1))' - point '(2.0,0)'; (-1,1),(-2,0)
* 伸縮/旋轉 select box '((0,0),(1,1))' * point '(2.0,0)'; (2,2),(0,0)
/ 伸縮/旋轉 select box '((0,0),(2,2))' / point '(2.0,0)'; (1,1),(0,0)
# 交點或者交面 select box'((1,-1),(-1,1))' # box'((1,1),(-1,-1))'; (1,1),(-1,-1)
# path或polygon的頂點數 select #path'((1,1),(2,2),(2,1))'; 3
@-@ 長度或周長 select @-@ path'((1,1),(2,2),(2,1))'; 3.41421356237309
@@ 中心 select @@ circle'<(0,0),1>'; (0,0)
## 第一個操作數和第二個操作數的最近點 select point '(0,0)' ## lseg '((2,0),(0,2))'; (1,1)
<-> 間距 select circle '<(0,0),1>' <-> circle '<(5,0),1>'; 3
&& 是否有重疊 select box '((0,0),(1,1))' && box '((0,0),(2,2))'; t
<< 是否嚴格在左 select circle '((0,0),1)' << circle '((5,0),1)'; t
>> 是否嚴格在右 select circle '((0,0),1)' >> circle '((5,0),1)'; f
&< 是否沒有延伸到右邊 select box '((0,0),(1,1))' &< box '((0,0),(2,2))'; t
&> 是否沒有延伸到左邊 select box '((0,0),(3,3))' &> box '((0,0),(2,2))'; t
<<| 是否嚴格在下 select box '((0,0),(3,3))' <<| box '((3,4),(5,5))'; t
|>> 是否嚴格在上 select box '((3,4),(5,5))' |>> box '((0,0),(3,3))'; t
&<| 是否沒有延伸到上面 select box '((0,0),(1,1))' &<| box '((0,0),(2,2))'; t
|&> 是否沒有延伸到下面 select box '((0,0),(3,3))' |&> box '((0,0),(2,2))'; t
<^ 是否低于(允許接觸) select box '((0,0),(3,3))' <^ box '((3,3),(4,4))'; t
>^ 是否高于(允許接觸) select box '((0,0),(3,3))' >^ box '((3,3),(4,4))'; f
?# 是否相交 select lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'; t
?- 是否水準對齊 select ?- lseg '((-1,1),(1,1))'; t
?- 兩邊圖形是否水準對齊 select point '(1,0)' ?- point '(0,0)'; t
?| 是否豎直對齊 select ?| lseg '((-1,0),(1,0))'; f
?| 兩邊圖形是否豎直對齊 select point '(0,1)' ?| point '(0,0)'; t
?-| 是否垂直 select lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'; t
?|| 是否平行 select lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'; t
@> 是否包含 select circle '((0,0),2)' @> point '(1,1)'; t
<@ 是否包含于或在圖形上 select point '(1,1)' <@ circle '((0,0),2)'; t
~= 是否相同 select polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'; t

函數

函數 傳回值類型 描述 示例 結果
area(object) double precision 面積 select area(circle'((0,0),1)'); 3.14159265358979
center(object) point 中心 select center(box'(0,0),(1,1)'); (0.5,0.5)
diameter(circle) double precision 圓周長 select diameter(circle '((0,0),2.0)'); 4
height(box) double precision 矩形豎直高度 select height(box '((0,0),(1,1))'); 1
isclosed(path) boolean 是否為閉合路徑 select isclosed(path '((0,0),(1,1),(2,0))'); t
isopen(path) boolean 是否為開放路徑 select isopen(path '[(0,0),(1,1),(2,0)]'); t
length(object) double precision 長度 select length(path '((-1,0),(1,0))'); 4
npoints(path) int path中的頂點數 select npoints(path '[(0,0),(1,1),(2,0)]'); 3
npoints(polygon) int 多邊形的頂點數 select npoints(polygon '((1,1),(0,0))'); 2
pclose(path) path 将開放path轉換為閉合path select pclose(path '[(0,0),(1,1),(2,0)]');  ((0,0),(1,1),(2,0))
popen(path) path 将閉合path轉換為開放path select popen(path '((0,0),(1,1),(2,0))'); [(0,0),(1,1),(2,0)]
radius(circle) double precision 圓半徑 select radius(circle '((0,0),2.0)'); 2
width(box) double precision 矩形的水準長度 select width(box '((0,0),(1,1))'); 1

類型轉換函數

函數 傳回類型 描述 示例 結果
box(circle) box 圓形轉矩形 select box(circle '((0,0),2.0)'); (1.41421356237309,1.41421356237309),(-1.41421356237309,-1.41421356237309)
box(point) box 點轉空矩形 select box(point '(0,0)'); (0,0),(0,0)
box(point, point) box 點轉矩形 select box(point '(0,0)', point '(1,1)'); (1,1),(0,0)
box(polygon) box 多邊形轉矩形 select box(polygon '((0,0),(1,1),(2,0))'); (2,1),(0,0)
bound_box(box, box) box 将兩個矩形轉換成一個邊界矩形 select bound_box(box '((0,0),(1,1))', box '((3,3),(4,4))'); (4,4),(0,0)
circle(box) circle 矩形轉圓形 select circle(box '((0,0),(1,1))'); <(0.5,0.5),0.707106781186548>
circle(point, double precision) circle 圓心與半徑轉圓形 select circle(point '(0,0)', 2.0); <(0,0),2>
circle(polygon) circle 多邊形轉圓形 select circle(polygon '((0,0),(1,1),(2,0))'); <(1,0.333333333333333),0.924950591148529>
line(point, point) line 點轉直線 select line(point '(-1,0)', point '(1,0)'); {0,-1,0}
lseg(box) lseg 矩形轉線段 select lseg(box '((-1,0),(1,0))'); [(1,0),(-1,0)]
lseg(point, point) lseg 點轉線段 select lseg(point '(-1,0)', point '(1,0)'); [(-1,0),(1,0)]
path(polygon) path 多邊形轉path select path(polygon '((0,0),(1,1),(2,0))'); ((0,0),(1,1),(2,0))

point

(double precision, double precision)
point select point(23.4, -44.5); (23.4,-44.5)
point(box) point 矩形轉點 select point(box '((-1,0),(1,0))'); (0,0)
point(circle) point 圓心 select point(circle '((0,0),2.0)'); (0,0)
point(lseg) point 線段中心 select point(lseg '((-1,0),(1,0))'); (0,0)
point(polygon) point 多邊形的中心 select point(polygon '((0,0),(1,1),(2,0))'); (1,0.333333333333333)
polygon(box) polygon 矩形轉4點多邊形 select polygon(box '((0,0),(1,1))'); ((0,0),(0,1),(1,1),(1,0))
polygon(circle) polygon 圓形轉12點多邊形 select polygon(circle '((0,0),2.0)');

((-2,0),(-1.73205080756888,1),(-1,1.73205080756888),(-1.22460635382238e-16,2),(1,1.73205080756888),(1.73205080756888,1),(2,2.4492127

0764475e-16),(1.73205080756888,-0.999999999999999),(1,-1.73205080756888),(3.67381906146713e-16,-2),(-0.999999999999999,-1.73205080756

888),(-1.73205080756888,-1))

polygon(npts, circle) polygon 圓形轉npts點多邊形 select polygon(12, circle '((0,0),2.0)');

((-2,0),(-1.73205080756888,1),(-1,1.73205080756888),(-1.22460635382238e-16,2),(1,1.73205080756888),(1.73205080756888,1),(2,2.4492127

0764475e-16),(1.73205080756888,-0.999999999999999),(1,-1.73205080756888),(3.67381906146713e-16,-2),(-0.999999999999999,-1.73205080756

888),(-1.73205080756888,-1))

polygon(path) polygon 将path轉多邊形 select polygon(path '((0,0),(1,1),(2,0))'); ((0,0),(1,1),(2,0))