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)));