将相同属性的多边形融合在一起,一般会用到st_Union,ST_Accum
SELECT st_Union(ST_Accum(geom))),name from grid_hex gh where group by gh.name
当我以为皆大欢喜的时候,事实给了我一盆冷水
XGCHPEHZVWR$)TJGB9W(8EM.png看图,有些区域能够很好的融合,但有些区域却未得到想要的结果。查看帮助文档,也没有这问题的描述。借助Google,能找到一些相同的问题。
link
link
总结起来,就是一个问题,这些多边形因为精度问题,存在肉眼不可见的拓扑错误,并不像你看到的那样规整的数据。
这里需要用到ST_SnapToGrid(捕捉)函数。
geometry ST_SnapToGrid(geometry geomA, float originX, float originY, float sizeX, float sizeY);
geometry ST_SnapToGrid(geometry geomA, float sizeX, float sizeY);
geometry ST_SnapToGrid(geometry geomA, float size);
geometry ST_SnapToGrid(geometry geomA, geometry pointOrigin, float sizeX, float sizeY, float sizeZ, float sizeM);
Variant 1,2,3: 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. Useful for reducing precision.
代码就修改以下:
SELECT st_Union(ST_Accum(ST_SnapToGrid(geom,0.001)))),name from grid_hex gh where group by gh.name
问题解决了,这个精度问题根据具体数据进行调整。
网友评论