2025年9月25日: PostgreSQL 18 发布!
支持的版本: 当前 (18) / 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,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

计算两个 box 的交集,如果没有则返回 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

这些对象是否相交?适用于以下类型对:(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

?- 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] “旋转” box 使用这些运算符只会移动其角点:box 仍被视为具有与坐标轴平行的边。因此,box 的尺寸不会被保留,就像真正的旋转一样。


注意

请注意,“相等”运算符 ~= 代表 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

提取 box 的对角线作为线段(与 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

计算 box 的垂直尺寸。

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

计算 box 的水平尺寸。

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


表 9.38. 几何类型转换函数

函数

描述

示例

box ( circle ) → box

计算内接于圆的 box。

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

box ( point ) → box

将点转换为空 box。

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

box ( point, point ) → box

将任意两个角点转换为 box。

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

box ( polygon ) → box

计算多边形的边界 box。

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

bound_box ( box, box ) → box

计算两个 box 的边界 box。

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

circle ( box ) → circle

计算包围 box 的最小圆。

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

提取 box 的对角线作为线段。

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

计算 box 的中心。

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

将 box 转换为 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 坐标。同样,boxlseg 类型的值也可以视为两个 point 值的数组。

提交更正

如果您在文档中发现任何不正确之处、与您实际体验不符之处或需要进一步说明之处,请使用 此表单 报告文档问题。