postgreSQL的插件postGIS的函数应用

postgreSQL没有安装postGIS插件的自行安装,网上有教程

创建数据表

CREATE TABLE info (
	id serial PRIMARY KEY,
	name varchar, 
	geom geometry);

插入数据

INSERT INTO geometries VALUES 
	('Point', 'POINT(0 0)'), --插入一个点
	('Linestring','LINESTRING(0 0, 1 1,2 1,2 2)'), --插入一条线段
	('Polygon', 'POLYGON((0 0, 1 0, 1 1,0 1,0 0))'),--插入一个多边形必须首位相接
	('PolygonWithHole', 'POLYGON((0 0, 10 0, 10 10,0 10, 0 0),(1 1, 1 2,2 2,2 1,1 1))'), --中间有个洞的多边形
	('Collection', 'GEOMETRYCOLLECTION(POINT(2 0),POLYGON((0 0, 1 0,1 1,0 1, 0 0)))'); --一个点和多边形
insert into info(name , geom) values('Polygon' , 'POLYGON((0 0, 3 0, 3 3,0 3,0 0))');--插入一个面

ST_Intersects:如果两个图形有相同的部分,即如果它们的边界或内部相交,可以用来判断一个点是否在一个图形内部

在内部返回true,否则返回false

select ST_Intersects(geom , ST_GeometryFromText('point(0 0)')) from info;

ST_Within:第一个几何图形完全在第二个几何图形内则返回true

select ST_Within(ST_GeometryFromText('point(1 1)'),geom) from info;

ST_Contains:第二个几何图形完全在第一个几何图形内则返回true

select ST_Contains(geom , ST_GeometryFromText('point(1 1)')) from info;

ST_Distance:计算两个几何图形的最短距离    

select ST_Distance(ST_GeometryFromText('point(1 1)') , ST_GeometryFromText('point(0 0)'));

ST_DWithin:测量某个范围内的物体,在数据库中查找在点(0,0)一米范围内的物体

select name , ST_AsText(geom) from info where ST_DWithin(ST_GeometryFromText(ST_AsText(geom),4326) , ST_GeometryFromText('point(0 0)' , 4326) , 1);	
select name , ST_AsText(geom) from info where ST_DWithin(geom, ST_GeometryFromText('point(0 0)') , 1);	

ST_Buffer(geometry, distance):几何对象和缓冲区距离作为参数,并输出一个多边形,多边形的边界与输入的几何图形之间的距离与输入的缓冲区距离相等。

select ST_AsText(ST_Buffer(ST_GeometryFromText('point(0 0)') , 2)::geometry(POLYGON , 0));--扩大两米转化为多边形

ST_Intersection:获取两个图形的重叠部分,取交集

select ST_AsText(ST_Intersection(ST_Buffer(ST_GeometryFromText('point(0 0)') , 2) , ST_Buffer(ST_GeometryFromText('point(3 0)'),2)));
select ST_AsText(ST_Intersection(ST_Buffer('point(0 0)' , 2) , ST_Buffer('point(3 0)',2)));

ST_Union:将两个几何图形合并起来,取并集。

SELECT ST_AsText(ST_Union(ST_Buffer('POINT(0 0)', 2),ST_Buffer('POINT(3 0)', 2)));

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值