PostgreSQL
9.11. Geometric Functions and Operators
The geometric types point
, box
, lseg
, line
, path
, polygon
, and circle
have a large set of native support functions and operators, shown in Table 9.35, Table 9.36, and Table 9.37.
Table 9.35. Geometric Operators
Operator Description Example(s) |
---|
`geometric_type Adds the coordinates of the second
|
Concatenates two open paths (returns NULL if either path is closed).
|
`geometric_type Subtracts the coordinates of the second
|
`geometric_type Multiplies each point of the first argument by the second
|
`geometric_type Divides each point of the first argument by the second
|
Computes the total length. Available for
|
Computes the center point. Available for
|
Returns the number of points. Available for
|
`geometric_type Computes the point of intersection, or NULL if there is none. Available for
|
Computes the intersection of two boxes, or NULL if there is none.
|
`geometric_type Computes the closest point to the first object on the second object. Available for these pairs of types: (
|
`geometric_type Computes the distance between the objects. Available for all geometric types except
|
`geometric_type Does first object contain second? Available for these pairs of types: (
|
`geometric_type Is first object contained in or on second? Available for these pairs of types: (
|
`geometric_type Do these objects overlap? (One point in common makes this true.) Available for
|
`geometric_type Is first object strictly left of second? Available for
|
`geometric_type Is first object strictly right of second? Available for
|
`geometric_type Does first object not extend to the right of second? Available for
|
`geometric_type Does first object not extend to the left of second? Available for
|
`geometric_type` `+<< |
` _`+geometric_type Is first object strictly below second? Available for `+box '(3,3),(0,0)' << |
box '(5,5),(3,4)'` → `+t` |
`geometric_type` `+ |
>>` _`+geometric_type Is first object strictly above second? Available for `+box '(5,5),(3,4)' |
>> box '(3,3),(0,0)'` → `+t` |
`geometric_type` `+&< |
` _`+geometric_type Does first object not extend above second? Available for `+box '(1,1),(0,0)' &< |
box '(2,2),(0,0)'` → `+t` |
`geometric_type` `+ |
&>` _`+geometric_type Does first object not extend below second? Available for `+box '(3,3),(0,0)' |
&> box '(2,2),(0,0)'` → `+t` |
Is first object below second (allows edges to touch)?
|
Is first object above second (allows edges to touch)?
|
`geometric_type Do these objects intersect? Available for these pairs of types: (
|
Is line horizontal?
|
Are points horizontally aligned (that is, have same y coordinate)?
|
`+? |
` `+line` → `+? |
` `+lseg` → Is line vertical? `+? |
lseg '[(-1,0),(1,0)]'` → `+f` |
|
` `+point` → Are points vertically aligned (that is, have same x coordinate)? `+point '(0,1)' ? |
point '(0,0)'` → `+t` |
|
` `+line` →
|
` `+lseg` → Are lines perpendicular? `+lseg '[(0,0),(0,1)]' ?- |
lseg '[(0,0),(1,0)]'` → `+t` |
|
` `+line` →
|
` `+lseg` → Are lines parallel? `+lseg '[(-1,0),(1,0)]' ? |
lseg '[(-1,2),(1,2)]'` → `+t` |
`geometric_type Are these objects the same? Available for
|
[a] “[.quote]#Rotating”# a box with these operators only moves its corner points: the box is still considered to have sides parallel to the axes. Hence the box’s size is not preserved, as a true rotation would do. |
+
Caution
Note that the “[.quote]#same as”# operator, ~=
, represents the usual notion of equality for the point
, box
, polygon
, and circle
types. Some of the geometric types also have an =
operator, but =
compares for equal areas only. The other scalar comparison operators (<=
and so on), where available for these types, likewise compare areas.
Note
Before PostgreSQL 14, the point is strictly below/above comparison operators point
<<|
point
and point
|>>
point
were respectively called <^
and >^
. These names are still available, but are deprecated and will eventually be removed.
Table 9.36. Geometric Functions
Function Description Example(s) |
---|
`+area+` ( _`+geometric_type+`_ ) → `+double precision+` Computes area. Available for
|
`+center+` ( _`+geometric_type+`_ ) → `+point+` Computes center point. Available for
|
`+diagonal+` ( `+box+` ) → `+lseg+` Extracts box’s diagonal as a line segment (same as
|
`+diameter+` ( `+circle+` ) → `+double precision+` Computes diameter of circle.
|
`+height+` ( `+box+` ) → `+double precision+` Computes vertical size of box.
|
`+isclosed+` ( `+path+` ) → `+boolean+` Is path closed?
|
`+isopen+` ( `+path+` ) → `+boolean+` Is path open?
|
`+length+` ( _`+geometric_type+`_ ) → `+double precision+` Computes the total length. Available for
|
`+npoints+` ( _`+geometric_type+`_ ) → `+integer+` Returns the number of points. Available for
|
`+pclose+` ( `+path+` ) → `+path+` Converts path to closed form.
|
`+popen+` ( `+path+` ) → `+path+` Converts path to open form.
|
`+radius+` ( `+circle+` ) → `+double precision+` Computes radius of circle.
|
`+slope+` ( `+point+`, `+point+` ) → `+double precision+` Computes slope of a line drawn through the two points.
|
`+width+` ( `+box+` ) → `+double precision+` Computes horizontal size of box.
|
+
Table 9.37. Geometric Type Conversion Functions
Function Description Example(s) |
---|
`+box+` ( `+circle+` ) → `+box+` Computes box inscribed within the circle.
|
Converts point to empty box.
|
Converts any two corner points to box.
|
Computes bounding box of polygon.
|
`+bound_box+` ( `+box+`, `+box+` ) → `+box+` Computes bounding box of two boxes.
|
`+circle+` ( `+box+` ) → `+circle+` Computes smallest circle enclosing box.
|
Constructs circle from center and radius.
|
Converts polygon to circle. The circle’s center is the mean of the positions of the polygon’s points, and the radius is the average distance of the polygon’s points from that center.
|
`+line+` ( `+point+`, `+point+` ) → `+line+` Converts two points to the line through them.
|
`+lseg+` ( `+box+` ) → `+lseg+` Extracts box’s diagonal as a line segment.
|
Constructs line segment from two endpoints.
|
`+path+` ( `+polygon+` ) → `+path+` Converts polygon to a closed path with the same list of points.
|
`+point+` ( `+double precision+`, `+double precision+` ) → `+point+` Constructs point from its coordinates.
|
Computes center of box.
|
Computes center of circle.
|
Computes center of line segment.
|
Computes center of polygon (the mean of the positions of the polygon’s points).
|
`+polygon+` ( `+box+` ) → `+polygon+` Converts box to a 4-point polygon.
|
Converts circle to a 12-point polygon.
|
Converts circle to an `n`-point polygon.
|
Converts closed path to a polygon with the same list of points.
|
+
It is possible to access the two component numbers of a point
as though the point were an array with indexes 0 and 1. For example, if t.p
is a point
column then SELECT p[0] FROM t
retrieves the X coordinate and UPDATE t SET p[1] = ...
changes the Y coordinate. In the same way, a value of type box
or lseg
can be treated as an array of two point
values.
Prev | Up | Next |
---|---|---|
9.10. Enum Support Functions |
9.12. Network Address Functions and Operators |
Submit correction
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.
Copyright © 1996-2023 The PostgreSQL Global Development Group