Geometry types in PostgreSQL

Posted by swebajen on Tue, 17 Dec 2019 17:02:53 +0100

PostgreSQL mainly supports some two-dimensional geometric data types, such as point, line, lseg rectangle, path, polygon, circle, etc.

This article will introduce the input of geometry type in detail. Note: each type has several equivalent input forms.

1. point

select '1,1'::point;
select '(1,1)'::point;

  

2. line

select '1,1,2,2'::line;
select '(1,1),(2,2)'::line;
select '((1,1),(2,2))'::line;
select '[(1,1),(2,2)]'::line;

select line'1,1,2,2';
select line'(1,1),(2,2)';
select line'((1,1),(2,2))';
select line'[(1,1),(2,2)]';

  

3. Line segment (lseg)

select '1,1,2,2'::lseg;
select '(1,1),(2,2)'::lseg;
select '((1,1),(2,2))'::lseg;
select '[(1,1),(2,2)]'::lseg;

select lseg'1,1,2,2';
select lseg'(1,1),(2,2)';
select lseg'((1,1),(2,2))';
select lseg'[(1,1),(2,2)]';

4. Rectangle (box)

Bracket [] cannot be used for rectangle type.

select '1,1,2,2'::box;
select '(1,1),(2,2)'::box;
select '((1,1),(2,2))'::box;

select box'1,1,2,2';
select box'(1,1),(2,2)';
select box'((1,1),(2,2))';

5. path

In a path, square brackets [] are used to indicate an open path, while parentheses () are used to indicate a closed path. Closed path means that the last point is connected with the first point.

select '1,1,2,2,3,3,4,4'::path;
select '(1,1),(2,2),(3,3),(4,4)'::path;
select '((1,1),(2,2),(3,3),(4,4))'::path;

select path'1,1,2,2,3,3,4,4';
select path'(1,1),(2,2),(3,3),(4,4)';
select path'((1,1),(2,2),(3,3),(4,4))';

   

select '[(1,1),(2,2),(3,3),(4,4)]'::path;
select path'[(1,1),(2,2),(3,3),(4,4)]';

  

6. polygon

Bracket [] cannot be used for polygon type.

select '1,1,2,2,3,3,4,4'::polygon;
select '(1,1),(2,2),(3,3),(4,4)'::polygon;
select '((1,1),(2,2),(3,3),(4,4))'::polygon;

select polygon'1,1,2,2,3,3,4,4';
select polygon'(1,1),(2,2),(3,3),(4,4)';
select polygon'((1,1),(2,2),(3,3),(4,4))';

  

7. circle

select '1,1,5'::circle;
select '((1,1),5)'::circle;
select '<(1,1),5>'::circle;

select circle'1,1,5';
select circle'((1,1),5)';
select circle'<(1,1),5>';

  

 

Topics: PostgreSQL