Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)

      Clustering:聚集、分类归并的意思。在PostGIS中有两个地方会涉及到这个词,虽然这两处使用相同的术语,但表示不同的意思,一个是Index clustering,另一个就是下面要介绍的Spatial Clustering。

       Spatial Clustering:这个通常用在点类型上,可以把一些点通过邻近性或其他特点聚集到一起。实现这个主要用到了ST_SnapToGrid这个方法,下面就介绍下ST_SnapToGrid:   

将用到以下几个方法:

    boolean ST_Overlaps(geometry A, geometry B); 判断Geometries是否有交汇并不是完全包含。

    boolean ST_Touches(geometry g1, geometry g2);判断geometries之间至少有一个公共点并内部不相交。

    geometry ST_Translate(geometry g1, float deltax, float deltay);几何转换到一个新的位置。

    geometry ST_SnapToGrid(geometry geomA, float size);Snap all points of the input geometry to the grid defined by its origin and cell size. Remove consecutive points falling on the same cell, eventually returning NULL if output points are not enough to define a geometry of the given type.Collapsed geometries in a collection are stripped from it.(英语不咋地就不翻了)

先建个表:

-- 创建translate_polygons表
CREATE TABLE translate_polygons (
"id" SERIAL
NOT NULL PRIMARY KEY,
"name"
char(1) NOT NULL,
"the_geom" geometry
NOT NULL
);
-- 添加数据
INSERT INTO translate_polygons (name, the_geom) VALUES (
'A','POLYGON((2 1, 0 3, 2 3, 2 1))'::geometry
), (
'B', 'POLYGON((0 3, 2 5, 2 3, 0 3))'::geometry
), (
'C', 'POLYGON((2 5, 4 3, 2 3, 2 5))'::geometry
), (
'D', 'POLYGON((4 3, 2 1, 2 3, 4 3))'::geometry
);

在QGIS中打开,编辑下样式后如下图:

Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)_第1张图片

SELECT t1.name || ' overlaps ' || t2.name || ' = ' || ST_Overlaps(t1.the_geom, t2.the_geom)::text AS overlap_test,
t1.name
|| ' touches ' || t2.name || ' = ' || ST_Touches(t1.the_geom, t2.the_geom)::text AS touch_test
FROM translate_polygons AS t1,
translate_polygons
AS t2
WHERE t1.name <> t2.name
ORDER BY t1.name, t2.name;

 查看相交情况:

Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)_第2张图片

使用ST_Translate移动两个三角形(A和C):

-- A,C往左下角移动

UPDATE translate_polygons
SET the_geom = ST_Translate(the_geom, -0.4, -0.2)
WHERE name = 'A';

UPDATE translate_polygons
SET the_geom = ST_Translate(the_geom, -0.1, -0.4)
WHERE name = 'C';

 查看相交情况:

Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)_第3张图片

图形显示:

Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)_第4张图片

使用ST_SnapToGrid:

UPDATE translate_polygons
SET the_geom = ST_SnapToGrid(the_geom, 1)

 Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)_第5张图片

如果把A往左移动的距离改为0.6,再做ST_SnapToGrid:

-- A,C往左下角移动

UPDATE translate_polygons
SET the_geom = ST_Translate(the_geom, -0.6, -0.2)
WHERE name = 'A';

UPDATE translate_polygons
SET the_geom = ST_Translate(the_geom, -0.1, -0.4)
WHERE name = 'C';

Introduction to PostGIS 之 Spatial clustering(介绍ST_SnapToGrid)_第6张图片

对ST_SnapToGrid有了大概了解,下一篇研究下具体的使用了。

你可能感兴趣的:(cluster)