CG_Simplify — 在保留几何对象关键特征以及 Z/M 值的前提下,降低其复杂度。
geometry CG_Simplify(geometry geom, double precision threshold, boolean preserveTopology = false);
使用 SFCGAL 的简化算法对几何进行简化,在保留几何关键特征的前提下减少点或顶点数量,并在简化过程中保留 Z 和 M 值。
该算法基于约束三角剖分,使用 CGAL Polyline Simplification 2 库,并额外处理以保留 Z、M 坐标。当要求保持拓扑且几何间存在相交时,会在相交点对 Z 和 M 值进行插值处理。
此函数适用于 类地形的 3D(2.5D)几何,但并不适合处理如墙体等垂直表面。
可用性: 3.6.0 - 需要 SFCGAL >= 2.1.0
该方法需要SFCGAL后端。
该函数支持 3d 并且不会丢失 z-index。
该功能支持M坐标。
geom
输入几何
threshold
最大距离阈值(以几何单位计),用于控制简化程度。值越大,输出几何被简化得就越多。
preserveTopology
如果设为 true,函数会确保几何的拓扑结构被保留;在此模式下,当几何发生相交时,相交点的 Z 和 M 值会进行插值处理。默认值为 false。
返回一个保留了 Z 和 M 值的简化几何对象。
This example simplifies a polygon with a threshold of 0.5.
SELECT CG_Simplify(
'POLYGON((0 0,0 1,0.1 1,0.2 1,0.3 1,0.4 1,0.5 1,1 1,1 0,0 0))',
0.5
);
POLYGON((0 0,0 1,1 1,1 0,0 0))
Preserving topology can keep additional support points in a mixed ZM collection.
SELECT CG_Simplify(
'GEOMETRYCOLLECTION ZM(
LINESTRING ZM(-1 -1 3 4,0 0 10 100,1 1 20 200,0 2 15 150,0 5 30 300,2 19 25 250,-4 20 15 150),
POLYGON ZM((0 0 10 100,1 1 20 200,0 2 15 150,0 5 30 300,2 19 25 250,-4 20 15 150,0 0 10 100))
)',
2,
false
);
GEOMETRYCOLLECTION ZM (LINESTRING ZM (-1 -1 3 4,2 19 25 250,-4 20 15 150),POLYGON ZM ((0 0 10 100,2 19 25 250,-4 20 15 150,0 0 10 100)))
The topology-preserving variant keeps the shared anchor point at the collection intersection.
SELECT CG_Simplify(
'GEOMETRYCOLLECTION ZM(
LINESTRING ZM(-1 -1 3 4,0 0 10 100,1 1 20 200,0 2 15 150,0 5 30 300,2 19 25 250,-4 20 15 150),
POLYGON ZM((0 0 10 100,1 1 20 200,0 2 15 150,0 5 30 300,2 19 25 250,-4 20 15 150,0 0 10 100))
)',
2,
true
);
GEOMETRYCOLLECTION ZM (LINESTRING ZM (-1 -1 3 4,0 0 10 100,2 19 25 250,-4 20 15 150),POLYGON ZM ((0 0 10 100,2 19 25 250,-4 20 15 150,0 0 10 100)))
Compare simplification with and without topology preservation, using a threshold of 50.
WITH data AS (
SELECT ST_GeomFromText('GEOMETRYCOLLECTION(
POLYGON((88.46 158.85,90.77 171.54,147.31 173.85,146.15 145,173.85 119.62,146.15 103.46,112.69 118.46,91.92 93.08,65.38 101.15,34.23 121.92,41.15 142.69,49.23 143.85,88.46 158.85)),
POLYGON((112.69 118.46,146.15 103.46,190 60.77,185.38 43.46,126.54 26.15,83.85 28.46,67.69 64.23,43.46 58.46,10 83.85,34.23 121.92,65.38 101.15,91.92 93.08,112.69 118.46)))
') AS geom
)
SELECT geom AS input_geometry,
CG_Simplify(geom, 50, true) AS topology_preserved,
CG_Simplify(geom, 50, false) AS topology_not_preserved
FROM data;
GEOMETRYCOLLECTION(POLYGON((88.46 158.85,90.77 171.54,147.31 173.85,146.15 145,173.85 119.62,146.15 103.46,112.69 118.46,91.92 93.08,65.38 101.15,34.23 121.92,41.15 142.69,49.23 143.85,88.46 158.85)), POLYGON((112.69 118.46,146.15 103.46,190 60.77,185.38 43.46,126.54 26.15,83.85 28.46,67.69 64.23,43.46 58.46,10 83.85,34.23 121.92,65.38 101.15,91.92 93.08,112.69 118.46))) | GEOMETRYCOLLECTION(POLYGON((88.46 158.85,147.31 173.85,146.15 103.46,112.69 118.46,34.23 121.92,88.46 158.85)), POLYGON((112.69 118.46,146.15 103.46,185.38 43.46,10 83.85,34.23 121.92,112.69 118.46))) | GEOMETRYCOLLECTION(POLYGON((88.46 158.85,173.85 119.62,34.23 121.92,88.46 158.85)), POLYGON((112.69 118.46,190 60.77,10 83.85,112.69 118.46)))