Postgresql学习笔记之——数据类型之几何类型

一、几何类型概况

Postgresql主要支持一些二维的几何数据类型,最基本的时 “point” ,它是其他类型的基础。

几何类型如下:

类型名称存储空间描述表现形式
point16字节平面中的点(x,y)
line32字节直线((x1,y1),(x2,y2))
lseg32字节线段(有限长度)[(x1,y1),(x2,y2)]
box32字节矩形((x1,y1),(x2,y2))
path16+16*n闭合路径(与多边形类似)((x1,y1),(x2,y2),…)
path16+16*n开放路径[(x1,y1),(x2,y2),…]
polygon40+16n字节多边形(与闭合路径相似)((x1,y1),(x2,y2),…)
circle24字节<(x,y),r>
二、几何类型的输入
1.输入格式:

类型名称 ‘值’ 或者 ‘值’::类型名称

示例:

点:
postgres=# select point '1,2';
 point 
-------
 (1,2)
(1 row)

postgres=# select '(1,2)'::point;
 point 
-------
 (1,2)
(1 row)
线段 :
postgres=# select lseg '1,2,3,2';
     lseg      
---------------
 [(1,2),(3,2)]
(1 row)

postgres=# select lseg '(1,2),(3,2)';
  ?column?   
-------------
 [(1,2),(3,2)]
(1 row)

postgres=# select lseg '[(1,2),(3,2)]';
   ?column?    
---------------
 [(1,2),(3,2)]
(1 row)

postgres=# select '1,2,3,2'::lseg;
     lseg      
---------------
 [(1,2),(3,2)]
(1 row)

postgres=# select '(1,2),(3,2)'::lseg;
     lseg      
---------------
 [(1,2),(3,2)]
(1 row)

postgres=# select '[(1,2),(3,2)]'::lseg;
     lseg      
---------------
 [(1,2),(3,2)]
(1 row)
矩形:
postgres=# select box '1,1,2,2';
     box     
-------------
 (2,2),(1,1)
(1 row)

postgres=# select box '(1,1),(2,2)';
     box     
-------------
 (2,2),(1,1)
(1 row)

postgres=# select box '((1,1),(2,2))';
     box     
-------------
 (2,2),(1,1)
(1 row)

postgres=# select '((1,1),(2,2))'::box;
     box     
-------------
 (2,2),(1,1)
(1 row)

postgres=# select '1,1,2,2'::box;
     box     
-------------
 (2,2),(1,1)
(1 row)

注意:矩形不可以使用类似线段那种中括号输入方式,如:

postgres=# select box '[(1,1),(2,2)]';
2020-02-26 15:17:08.646 CST [49927] ERROR:  invalid input syntax for type box: "[(1,1),(2,2)]" at character 12
2020-02-26 15:17:08.646 CST [49927] STATEMENT:  select box '[(1,1),(2,2)]';
ERROR:  invalid input syntax for type box: "[(1,1),(2,2)]"
LINE 1: select box '[(1,1),(2,2)]';

路径:
postgres=# select path '1,1,2,2,3,3,4,4';
           path            
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select '1,1,2,2,3,3,4,4'::path;
           path            
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select path '(1,1),(2,2),(3,3),(4,4)';
           path            
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select '(1,1),(2,2),(3,3),(4,4)'::path;
           path            
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select path '((1,1),(2,2),(3,3),(4,4))';
           path            
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select '((1,1),(2,2),(3,3),(4,4))'::path;
           path            
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select path '[(1,1),(2,2),(3,3),(4,4)]';
           path            
---------------------------
 [(1,1),(2,2),(3,3),(4,4)]
(1 row)

postgres=# select '[(1,1),(2,2),(3,3),(4,4)]'::path;
           path            
---------------------------
 [(1,1),(2,2),(3,3),(4,4)]
(1 row)

注意:路径中使用方括号 “[ ]” 表示开发路径,使用圆括号 “( )" 表示闭合路径,闭合路径指最后一个点与第一个点时连接在一起的。

多边形:
postgres=# select polygon '1,1,2,2,3,3,4,4';
          polygon          
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select '1,1,2,2,3,3,4,4'::polygon;
          polygon          
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select polygon '(1,1),(2,2),(3,3),(4,4)';
          polygon          
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select '(1,1),(2,2),(3,3),(4,4)'::polygon;
          polygon          
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select polygon '((1,1),(2,2),(3,3),(4,4))';
          polygon          
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

postgres=# select '((1,1),(2,2),(3,3),(4,4))'::polygon;
          polygon          
---------------------------
 ((1,1),(2,2),(3,3),(4,4))
(1 row)

注意:多边形类型输入中不能使用中括号,例如:

postgres=# select '[(1,1),(2,2),(3,3),(4,4)]'::polygon;
2020-02-26 15:31:51.449 CST [49927] ERROR:  invalid input syntax for type polygon: "[(1,1),(2,2),(3,3),(4,4)]" at character 8
2020-02-26 15:31:51.449 CST [49927] STATEMENT:  select '[(1,1),(2,2),(3,3),(4,4)]'::polygon;
ERROR:  invalid input syntax for type polygon: "[(1,1),(2,2),(3,3),(4,4)]"
LINE 1: select '[(1,1),(2,2),(3,3),(4,4)]'::polygon;

圆形:
postgres=# select circle '1,1,5';
  circle   
-----------
 <(1,1),5>
(1 row)

postgres=# select circle '((1,1)5)';
  circle   
-----------
 <(1,1),5>
(1 row)

postgres=# select circle '<(1,1)5>';
  circle   
-----------
 <(1,1),5>
(1 row)

postgres=# select '1,1,5'::circle;
  circle   
-----------
 <(1,1),5>
(1 row)

postgres=# select '((1,1)5)'::circle;
  circle   
-----------
 <(1,1),5>
(1 row)

postgres=# select '<(1,1)5>'::circle;
  circle   
-----------
 <(1,1),5>
(1 row)

注意:圆形不能使用一下输入方式:

postgres=# select '(1,1),5'::circle;
2020-02-26 15:35:05.073 CST [49927] ERROR:  invalid input syntax for type circle: "(1,1),5" at character 8
2020-02-26 15:35:05.073 CST [49927] STATEMENT:  select '(1,1),5'::circle;
ERROR:  invalid input syntax for type circle: "(1,1),5"
LINE 1: select '(1,1),5'::circle;
               ^
postgres=# select circle '(1,1),5';
2020-02-26 15:35:16.025 CST [49927] ERROR:  invalid input syntax for type circle: "(1,1),5" at character 15
2020-02-26 15:35:16.025 CST [49927] STATEMENT:  select circle '(1,1),5';
ERROR:  invalid input syntax for type circle: "(1,1),5"
LINE 1: select circle '(1,1),5';

三、几何类型的操作符

Postgresql提供了丰富的几何类型的操作符,如下:

1.平移运算符:”+“、”-“,缩放 / 旋转运算符:” * “、” / “

这四个运算符都是二次元运算符,运算符左侧值的类型可以时point、box、path、circle。运算符右侧值的类型只能时 point ,例如:

点和点之间的加减乘除相当于两个复数之间的加减乘除:

postgres=# select point '(1,2)' + point '(10,20)';
 ?column? 
----------
 (11,22)
(1 row)

postgres=# select point '(1,2)' - point '(10,20)';
 ?column? 
----------
 (-9,-18)
(1 row)

postgres=# select point '(1,2)' * point '(10,20)';
 ?column? 
----------
 (-30,40)
(1 row)

postgres=# select point '(1,2)' / point '(10,20)';
 ?column? 
----------
 (0.1,0)
(1 row)

矩形与点之间的运算:
postgres=# select box '((0,0),(1,1))' + point '(2,2)';
  ?column?   
-------------
 (3,3),(2,2)
(1 row)

postgres=# select box '((0,0),(1,1))' - point '(2,2)';
    ?column?     
-----------------
 (-1,-1),(-2,-2)
(1 row)


路径与点之间的运算:
postgres=# select path '(0,0),(1,1),(2,2)' + point '(2,2)';
      ?column?       
---------------------
 ((2,2),(3,3),(4,4))
(1 row)

postgres=# select path '(0,0),(1,1),(2,2)' - point '(2,2)';
        ?column?         
-------------------------
 ((-2,-2),(-1,-1),(0,0))
(1 row)

圆与点的运算:
postgres=# select circle '((0,0),1)' + point '(10,20)';
  ?column?   
-------------
 <(10,20),1>
(1 row)

postgres=# select circle '((0,0),1)' - point '(10,20)';
   ?column?    
---------------
 <(-10,-20),1>
(1 row)

对于乘法,如果乘数的y值为0,比如 point(x,0),则相当于几何对象缩放了x倍,具体如下:
postgres=# select circle '((0,0),1)' - point '(10,20)';
   ?column?    
---------------
 <(-10,-20),1>
(1 row)

postgres=# select point '(1,2)' * point '(2,0)';
 ?column? 
----------
 (2,4)
(1 row)

postgres=# select point '(1,2)' * point '(3,0)';
 ?column? 
----------
 (3,6)
(1 row)

postgres=# select circle '((1,2),1)' * point '(3,0)';
 ?column?  
-----------
 <(3,6),3>
(1 row)

postgres=# select circle '((0,0),1)' * point '(3,0)';
 ?column?  
-----------
 <(0,0),3>
(1 row)

如果乘法为point(0,1),则相当于几何对象逆时针旋转90度,如果乘数为point(0,-1),则表示顺时针旋转90度,例如:
postgres=# select point '(1,2)' * point '(0,1)';
 ?column? 
----------
 (-2,1)
(1 row)

postgres=# select point '(1,2)' * point '(0,-1)';
 ?column? 
----------
 (2,-1)
(1 row)

postgres=# select circle '((0,0),1)' * point '(0,1)';
 ?column?  
-----------
 <(0,0),1>
(1 row)

postgres=# select circle '((1,1),1)' * point '(0,-1)';
  ?column?  
------------
 <(1,-1),1>
(1 row)

2.运算符 ”#“

用法:
(1)对于两个线段,计算出线段交点。
(2)对于两个矩形,计算出相交的矩形。
(3)对于路径或多边形,则计算出顶点数。

两个线段:

postgres=# select lseg '(0,0),(2,2)' # lseg '(0,2),(2,0)';
 ?column? 
----------
 (1,1)
(1 row)

如果两个线段不相交,返回空:

postgres=# select lseg '(0,0),(2,2)' # lseg '(3,2),(4,5)';
 ?column? 
----------
 
(1 row)

两个矩形:

postgres=# select box '(0,0),(2,2)' # box '(1,0),(3,1)';
  ?column?   
-------------
 (2,1),(1,0)
(1 row)

3.运算符 ”@-@“

一元运算符,参数类型只能是lseg(线段)、path(路径)。一般用于计算几何对象的长度,例如:

postgres=# select @-@ lseg '(1,0),(2,3)';
      ?column?      
--------------------
 3.1622776601683795
(1 row)

postgres=# select @-@ path '(1,0),(2,3),(5,6)';
      ?column?      
--------------------
 14.616020898215645
(1 row)

注意:开放式路径和闭合式路径的长度时不一样的,例如:

postgres=# select @-@ path '((1,0),(2,3),(5,6))';
      ?column?      
--------------------
 14.616020898215645
(1 row)

postgres=# select @-@ path '[(1,0),(2,3),(5,6)]';
     ?column?      
-------------------
 7.404918347287666
(1 row)
4.运算符 ”@@“

运算符 ”@@“ 为一元运算符,用于计算中心点,例如:

# 圆
postgres=# select @@ circle '((1,1),4)';
 ?column? 
----------
 (1,1)
(1 row)

# 矩形
postgres=# select @@ box '(0,0),(1,1)';
 ?column?  
-----------
 (0.5,0.5)
(1 row)

# 线段
postgres=# select @@ lseg '(0,0),(2,2)';
 ?column? 
----------
 (1,1)
(1 row)

5.运算符 ”##“

运算符 ”##“为二元运算符,用于计算两个几何对象上离的最近的点,例如:

postgres=# select point '(0,0)' ## lseg '(2,0),(0,2)';
 ?column? 
----------
 (1,1)
(1 row)

postgres=# select lseg '(0,0),(2,4)' ## lseg '(1,3),(4,5)';
  ?column?  
------------
 (1.75,3.5)
(1 row)

6.运算符 ”<->“

运算符 ”<->“为二元运算符,用于计算两个几何对象之间的间距,例如:

postgres=# select lseg '(0,0),(2,2)' <-> lseg '(2,3),(4,5)';
 ?column? 
----------
        1
(1 row)

postgres=# select circle '<(0,0),4>' <-> circle '<(6,7),2>';
     ?column?      
-------------------
 3.219544457292887
(1 row)

7.运算符 ”&&“

运算符 ”&&“ 为二元运算符,用于计算两个几何对象之间是否重叠,只要有一个共同点,则为真,否则返回false。例如:

postgres=# select box '(0,0),(2,2)' && box '(1,0),(4,3)';
 ?column? 
----------
 t
(1 row)

postgres=# select box '(0,0),(2,2)' && box '(3,4),(4,3)';
 ?column? 
----------
 f
(1 row)

8.判断两个对象相对位置的运算符

判断左右位置的4个运算符:
1.<< : 是否严格在左
2. >> :是否严格在右
3. &< :没有延展到左边
4. &> :没有延展到右边

判断上下位置的6个运算符:
1.<<| :严格在下
2.|>> :严格在上
3.&<| :没有延展到上面
4.|&> :没有延展到下面
5.<^ :在下面(允许接触)
6.>^ :在上面(允许接触)

额外一些运算符:
1.?# :是否相交
2.?- :是否水平或水平对齐
3.?| :是否竖直或竖直对齐
4.?-| :两个对象是否垂直
5.?|| :两个对象是否平行
6.@> :是否包含
7.<@ :包含或在其上

9.判断两个几何对象是否相同的运算符 ”~=“

对于多边形,如果表示的七点不一样,但是实现它们的是相同的两个多边形,只是位置不同而已,那么对应的判断方式如下:

postgres=# select polygon '((1,1),(0,0))' ~= polygon '((2,2),(3,3))';
 ?column? 
----------
 f
(1 row)

postgres=# select polygon '((1,1),(0,0))' ~= polygon '((0,0),(1,1))';
 ?column? 
----------
 t
(1 row)

postgres=# select box '(0,0),(1,1)' ~= box '(1,1),(0,0)'
postgres-# ;
 ?column? 
----------
 t
(1 row)

四、几何类型的函数

函数如下:

函数返回类型描述例子结果
area(object)double precision计算几何对象面积select area(box ‘(2,2),(4,4)’);4
center(object)point中心select center(box ‘(2,2),(4,4)’);3
diameter(circle)double precision计算圆的直径select diameter(circle ‘<(1,1),2>’)4
height(box)double precision矩形的高度select height(box ‘(2,4),(5,9)’);5
witdth(box)double precision矩形的宽度select width(box ‘(2,4),(5,9)’);3
isclosed(path)blooean是否是闭合路径select isclosed(path ‘((1,1),(2,2),(3,3))’);t
isopen(path)blooean是否是开发路径select isclosed(path ‘((1,1),(2,2),(3,3))’);f
length(object)double precision长度select length(path ‘((1,1),(2,2),(3,3))’);5.656854
npoints(path)int计算路径上的点select npoints(path ‘((1,1),(2,2),(3,3))’);3
npoints(polygon)int计算多边形上的点select npoints(polygon ‘((1,1),(2,2),(3,3),(4,4))’);4
pclose(path)path把路径转换为闭合路径(不管参数路径是否是闭合)select pclose(path ‘[(1,1),(2,2),(3,3)]’);((1,1),(2,2),(3,3))
popen(path)path把路径转换为开放路径(不管参数路径是否是开放)select popen(path ‘((1,1),(2,2),(3,3))’);[(1,1),(2,2),(3,3)]
radius(circle)double precision圆的半径select radius(circle ‘<(1,1),3>’);3

不同的几何类型还可以相互转换,转换函数如下:

函数返回类型描述
box(circle)box将圆形转换为矩形
box(point,point)box将两个点转换成矩形
box(polygon)box将多边形转换成矩形
circle(box)circle将矩形转换成圆
circle(point,double precision)circle将圆心和半径转换为圆
circle(polygon)circle将多边形转换成圆
lseg(box)lseg将矩形转换为对角线线段
lseg(point,point)lseg将两个点转化成线段
path(polygon)path将多边形转换成路径
point(double precision,double precision)point构建一个点
point(box)point矩形的中心点
point(circle)point圆的圆心
point(lseg)point线段的中心点
point(polygon)point多边形的中心点
polygon(box)polygon将矩形转换成4个点的多边形
ploygon(circle)polygon将圆形转换成12个点的多边形
polygon(npts,circle)polygon将圆形转换成 npts 个点的多边形
polygon(path)polygon将路径转换成多边形
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以和你分享一些关于 PostgreSQL学习笔记。 1. PostgreSQL 是什么? PostgreSQL 是一种开源的关系型数据库管理系统,它支持 SQL 语言和 ACID 事务处理,并且具有丰富的特性和扩展性,可用于大型企业应用、Web 应用和嵌入式应用。 2. PostgreSQL 的特点 PostgreSQL 具有以下特点: - 开源免费 - 支持 ACID 事务处理 - 支持 SQL 语言 - 支持复杂查询和数据类型 - 支持存储过程和触发器 - 可以扩展和定制 - 可以跨平台运行 3. PostgreSQL 的安装 在 Windows 平台下,可以从官网下载 Windows 安装程序进行安装。在 Linux 平台下,可以使用包管理器安装,例如 Ubuntu 可以使用以下命令进行安装: ``` sudo apt-get install postgresql ``` 4. PostgreSQL 的基本操作 创建数据库: ``` CREATE DATABASE databasename; ``` 创建表: ``` CREATE TABLE tablename ( column1 datatype, column2 datatype, column3 datatype, .... ); ``` 插入数据: ``` INSERT INTO tablename (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); ``` 查询数据: ``` SELECT column1, column2, ... FROM tablename WHERE condition; ``` 更新数据: ``` UPDATE tablename SET column1 = value1, column2 = value2, ... WHERE condition; ``` 删除数据: ``` DELETE FROM tablename WHERE condition; ``` 5. PostgreSQL 的扩展 PostgreSQL 可以通过扩展来增强其功能,例如: - PostGIS:空间数据处理扩展 - PL/Python:Python 语言扩展 - pgAdmin:可视化管理工具 - pgpool-II:连接池和负载均衡工具 以上就是一些关于 PostgreSQL 的基本学习笔记,希望对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Major_ZYH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值