Text Size: Normal / Large
This page in other versions: 9.2 / 9.1 / 9.0 / 8.4  |  Unsupported versions: 9.3 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1 / devel

# 8.8. Geometric Types

Geometric data types represent two-dimensional spatial objects. Table 8-20 shows the geometric types available in PostgreSQL. The most fundamental type, the point, forms the basis for all of the other types.

Table 8-20. Geometric Types

Name Storage Size Representation Description
point 16 bytes Point on a plane (x,y)
line 32 bytes Infinite line (not fully implemented) ((x1,y1),(x2,y2))
lseg 32 bytes Finite line segment ((x1,y1),(x2,y2))
box 32 bytes Rectangular box ((x1,y1),(x2,y2))
path 16+16n bytes Closed path (similar to polygon) ((x1,y1),...)
path 16+16n bytes Open path [(x1,y1),...]
polygon 40+16n bytes Polygon (similar to closed path) ((x1,y1),...)
circle 24 bytes Circle <(x,y),r> (center point and radius)

A rich set of functions and operators is available to perform various geometric operations such as scaling, translation, rotation, and determining intersections. They are explained in Section 9.11.

## 8.8.1. Points

Points are the fundamental two-dimensional building block for geometric types. Values of type point are specified using either of the following syntaxes:

```( x , y )
x , y
```

where x and y are the respective coordinates, as floating-point numbers.

Points are output using the first syntax.

## 8.8.2. Line Segments

Line segments (lseg) are represented by pairs of points. Values of type lseg are specified using any of the following syntaxes:

```[ ( x1 , y1 ) , ( x2 , y2 ) ]
( ( x1 , y1 ) , ( x2 , y2 ) )
( x1 , y1 ) , ( x2 , y2 )
x1 , y1   ,   x2 , y2
```

where (x1,y1) and (x2,y2) are the end points of the line segment.

Line segments are output using the first syntax.

## 8.8.3. Boxes

Boxes are represented by pairs of points that are opposite corners of the box. Values of type box are specified using any of the following syntaxes:

```( ( x1 , y1 ) , ( x2 , y2 ) )
( x1 , y1 ) , ( x2 , y2 )
x1 , y1   ,   x2 , y2
```

where (x1,y1) and (x2,y2) are any two opposite corners of the box.

Boxes are output using the second syntax.

Any two opposite corners can be supplied on input, but the values will be reordered as needed to store the upper right and lower left corners, in that order.

## 8.8.4. Paths

Paths are represented by lists of connected points. Paths can be open, where the first and last points in the list are considered not connected, or closed, where the first and last points are considered connected.

Values of type path are specified using any of the following syntaxes:

```[ ( x1 , y1 ) , ... , ( xn , yn ) ]
( ( x1 , y1 ) , ... , ( xn , yn ) )
( x1 , y1 ) , ... , ( xn , yn )
( x1 , y1   , ... ,   xn , yn )
x1 , y1   , ... ,   xn , yn
```

where the points are the end points of the line segments comprising the path. Square brackets ([]) indicate an open path, while parentheses (()) indicate a closed path. When the outermost parentheses are omitted, as in the third through fifth syntaxes, a closed path is assumed.

Paths are output using the first or second syntax, as appropriate.

## 8.8.5. Polygons

Polygons are represented by lists of points (the vertexes of the polygon). Polygons are very similar to closed paths, but are stored differently and have their own set of support routines.

Values of type polygon are specified using any of the following syntaxes:

```( ( x1 , y1 ) , ... , ( xn , yn ) )
( x1 , y1 ) , ... , ( xn , yn )
( x1 , y1   , ... ,   xn , yn )
x1 , y1   , ... ,   xn , yn
```

where the points are the end points of the line segments comprising the boundary of the polygon.

Polygons are output using the first syntax.

## 8.8.6. Circles

Circles are represented by a center point and radius. Values of type circle are specified using any of the following syntaxes:

```< ( x , y ) , r >
( ( x , y ) , r )
( x , y ) , r
x , y   , r
```

where (x,y) is the center point and r is the radius of the circle.

Circles are output using the first syntax.

2011-10-10 09:00:12.466118

test=# CREATE TABLE my_table(my_circles CIRCLE);

test=# \d my_table;
Table «public.my_table»
Column | Type | Modificators
------------+--------+---------------
my_circles | circle |

test=# INSERT INTO my_table(my_circles) VALUES('((0,0),1)');

test=# SELECT * FROM my_table;
my_circles
------------
<(0,0),1>
(1 row)

test=# SELECT area(my_circles) FROM my_table;
area
------------------
3.14159265358979
(1 row)

2011-10-10 12:52:52.402576

test=# CREATE TABLE my_table(my_opened_paths PATH);
CREATE TABLE

test=# INSERT INTO my_table(my_opened_paths) VALUES('[(0,0),(2,0),(2,3),(6,3),(6,5)]');
INSERT 0 1

test=# SELECT length(my_opened_paths) FROM my_table;
length
--------
11
(1 row)

2011-10-10 12:54:31.509161

test=# CREATE TABLE my_table(my_closed_paths PATH);
CREATE TABLE

test=# INSERT INTO my_table(my_closed_paths) VALUES('((0,0),(2,0),(2,3),(6,3),(6,5))');
INSERT 0 1

test=# SELECT length(my_closed_paths) FROM my_table;
length
------------------
18.8102496759067
(1 row)

2011-10-10 12:57:18.742434

test=# CREATE TABLE my_table(my_boxes BOX);
CREATE TABLE

test=# INSERT INTO my_table(my_boxes) VALUES('(0,0),(1,1)');
INSERT 0 1

test=# INSERT INTO my_table(my_boxes) VALUES('(0,0),(2,2)');
INSERT 0 1

test=# INSERT INTO my_table(my_boxes) VALUES('(-1,-1),(1,1)');
INSERT 0 1

test=# SELECT area(my_boxes) FROM my_table;
area
------
1
4
4
(3 rows)

2011-10-10 13:00:34.114148

test=# CREATE TABLE my_table(my_boxes BOX);
CREATE TABLE

test=# INSERT INTO my_table(my_boxes) VALUES('(0,0),(3,2)');
INSERT 0 1

test=# SELECT width(my_boxes), height(my_boxes) FROM my_table;
width | height
-------+--------
3 | 2
(1 row)