9.11. Geometric Functions and Operators
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 Geometric Operators, Geometric Functions, and Geometric Type Conversion Functions.
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 seven geometric types, for all combinations of
|
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 Is first object strictly below second? Available for
|
geometric_type Is first object strictly above second? Available for
|
geometric_type Does first object not extend above second? Available for
|
geometric_type Does first object not extend below second? Available for
|
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)?
|
Is line vertical?
|
Are points vertically aligned (that is, have same x coordinate)?
|
Are lines perpendicular?
|
Are lines parallel?
|
geometric_type Are these objects the same? Available for
|
: Geometric Operators
Note that the "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.
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.
Function Description Example(s) |
|---|
Computes area. Available for
|
Computes center point. Available for
|
Extracts box's diagonal as a line segment (same as
|
Computes diameter of circle.
|
Computes vertical size of box.
|
Is path closed?
|
Is path open?
|
Computes the total length. Available for
|
Returns the number of points. Available for
|
Converts path to closed form.
|
Converts path to open form.
|
Computes radius of circle.
|
Computes slope of a line drawn through the two points.
|
Computes horizontal size of box.
|
: Geometric Functions
Function Description Example(s) |
|---|
Computes box inscribed within the circle.
|
Converts point to empty box.
|
Converts any two corner points to box.
|
Computes bounding box of polygon.
|
Computes bounding box of two boxes.
|
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.
|
Converts two points to the line through them.
|
Extracts box's diagonal as a line segment.
|
Constructs line segment from two endpoints.
|
Converts polygon to a closed path with the same list of points.
|
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).
|
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.
|
: Geometric Type Conversion Functions
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.
-
"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. ↩︎