↑↓ 选择 ↵ 打开 ⌫ 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 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
历史版本PostgreSQL 13 已于 2025 年 11 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本。

9.11. 几何函数和操作符 #

几何类型 point、box、lseg、line、path、polygon 和 circle 有大量内置支持函数和操作符,如表 9.35、表 9.36 和表 9.37 中所示。

表 9.35. 几何操作符

操作符

描述

示例

geometric_type + point → geometric_type

将第二个 point 的坐标加到第一个参数各点的坐标上,从而执行平移。适用于 point、box、path、circle。

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

path + path → path

连接两个开放路径(如果任一路径是封闭的,则返回 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 - point → geometric_type

从第一个参数各点的坐标中减去第二个 point 的坐标,从而执行平移。适用于 point、box、path、circle。

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

geometric_type * point → geometric_type

将第一个参数的每个点乘上第二个 point(将点视为由实部和虚部表示的复数,并执行标准的复数乘法)。如果将第二个 point 解释为向量,这等价于将对象的大小和到原点的距离按向量的长度缩放,并以向量与 x 轴的夹角绕原点逆时针旋转。适用于 point、box、[a] path、circle。

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 / point → geometric_type

将第一个参数的每个点除以第二个 point(将点视为由实部和虚部表示的复数,并执行标准的复数除法)。如果将第二个 point 解释为向量,这等价于将对象的大小和到原点的距离按向量长度的倒数缩放,并以向量与 x 轴的夹角围绕原点顺时针旋转。适用于 point, box,[a] path,circle。

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_type → double precision

计算总长度。适用于 lseg、path。

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

@@ geometric_type → point

计算中心点。适用于 box,lseg,polygon,circle。

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

# geometric_type → integer

返回点的数量。适用于 path、polygon。

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

geometric_type # geometric_type → point

计算交点,如果没有则为 NULL。适用于 lseg, line。

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

box # box → box

计算两个矩形框的交集,如果没有则为 NULL。

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

geometric_type ## geometric_type → point

计算第二个对象上距离第一个对象最近的点。可用于以下类型的配对:(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_type → double precision

计算对象之间的距离。适用于除 polygon 之外的所有几何类型,所有 point 与另一种几何类型的组合,以及以下这些额外的类型对:(box, lseg)、(lseg, line)、(polygon, circle)(以及交换位置的情况)。

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

geometric_type @> geometric_type → boolean

第一个对象包含第二个对象吗?适用于这些类型对:(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_type → boolean

第一个对象是否位于第二个对象内部或其上?适用于这些类型对:(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_type → boolean

这些对象有重叠吗?(一个共同点使之为真。)适用于 box、polygon、circle。

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

geometric_type << geometric_type → boolean

第一个对象完全位于第二个对象的左边吗?适用于 point, box, polygon, circle。

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

geometric_type >> geometric_type → boolean

第一个对象完全位于第二个对象的右边吗?适用于 point, box, polygon, circle。

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

geometric_type &< geometric_type → boolean

第一个对象没有延伸到第二个对象的右侧吗?适用于 box, polygon, circle。

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

geometric_type &> geometric_type → boolean

第一个对象没有延伸到第二个对象的左侧吗?适用于 box, polygon, circle。

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

geometric_type <<| geometric_type → boolean

第一个对象是否严格位于第二个对象下面?适用于 box, polygon, circle。

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

geometric_type |>> geometric_type → boolean

第一个对象是否严格位于第二个对象上面?适用于 box、polygon、circle。

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

geometric_type &<| geometric_type → boolean

第一个对象是否没有扩展到第二个对象上面?适用于 box、polygon、circle。

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

geometric_type |&> geometric_type → boolean

第一个对象是否没有扩展到第二个对象下面?适用于 box、polygon、circle。

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

box <^ box → boolean

第一个对象是否位于第二个对象下面(允许边缘相切)?

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

point <^ point → boolean

第一个对象是否严格位于第二个对象下方?(这个操作符的名称有误;应为 <<|。)

point '(1,0)' <^ point '(1,1)' → t

box >^ box → boolean

第一个对象是否位于第二个对象上面(允许边缘相切)?

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

point >^ point → boolean

第一个对象是否严格位于第二个对象上方?(这个操作符的名称有误;应为 |>>。)

point '(1,1)' >^ point '(1,0)' → t

geometric_type ?# geometric_type → boolean

这些对象是否相交?适用于这些类型对:(box, box)、(lseg, box)、(lseg, lseg)、(lseg, line)、(line, box)、(line, line)、(path, path)。

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

?- line → boolean

?- lseg → boolean

线是否水平?

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

point ?- point → boolean

点是否水平对齐(即具有相同的 y 坐标)?

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

?| line → boolean

?| lseg → boolean

线是否竖直?

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

point ?| point → boolean

点是否垂直对齐(即具有相同的 x 坐标)?

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

line ?-| line → boolean

lseg ?-| lseg → boolean

两条线是否互相垂直?

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

line ?|| line → boolean

lseg ?|| lseg → boolean

两条线是否平行?

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

geometric_type ~= geometric_type → boolean

这些对象是相同的吗?适用于 point、box、polygon、circle。

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

[a] 用这些操作符“旋转”一个矩形框,只会移动它的角点:这个矩形框仍然被认为有平行于轴的边。因此,矩形框的大小并没有像真正的旋转那样得到保留。


小心

注意,“相同”操作符~=表示 point、box、polygon 和 circle 类型通常意义上的相等。某些几何类型还有=操作符,但=仅比较面积是否相等。对于提供其他标量比较操作符(<=等)的类型,这些操作符同样比较面积。

注意

在 PostgreSQL 8.2 之前,包含操作符 @> 和 <@ 分别称为 ~ 和 @。这些名称仍然可用,但已被弃用,最终将被移除。

表 9.36. 几何函数

函数

描述

示例

area ( geometric_type ) → double precision

计算面积。适用于 box、path、circle。path 输入必须封闭,否则返回 NULL。同样,如果 path 是自交叉的,结果可能是没有意义的。

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

center ( geometric_type ) → point

计算中心点。适用于 box、circle。

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

计算总长度。适用于 lseg、path。

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

npoints ( geometric_type ) → integer

返回点的数量。适用于 path、polygon。

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.37. 几何类型转换函数

函数

描述

示例

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


可以把一个 point 当作下标为 0 和 1 的数组,访问它的两个数值分量。例如,如果 t.p 是一个 point 列,那么 SELECT p[0] FROM t 检索 X 坐标而 UPDATE t SET p[1] = ...改变 Y 坐标。同样,box 或者 lseg 类型的值可以当作两个 point 值组成的数组看待。

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。