2FECA797DF3745ADAEDD44F913FD9372.jpg
將相同屬性的多邊形融合在一起,一般會(huì)用到st_Union痕檬,ST_Accum
SELECT st_Union(ST_Accum(geom))),name from grid_hex gh where group by gh.name
當(dāng)我以為皆大歡喜的時(shí)候,事實(shí)給了我一盆冷水
XGCHPEHZVWR$)TJGB9W(8EM.png
看圖规辱,有些區(qū)域能夠很好的融合谆棺,但有些區(qū)域卻未得到想要的結(jié)果。查看幫助文檔罕袋,也沒有這問題的描述改淑。借助Google,能找到一些相同的問題浴讯。
link
link
總結(jié)起來朵夏,就是一個(gè)問題,這些多邊形因?yàn)榫葐栴}榆纽,存在肉眼不可見的拓?fù)溴e(cuò)誤仰猖,并不像你看到的那樣規(guī)整的數(shù)據(jù)。
這里需要用到ST_SnapToGrid(捕捉)函數(shù)奈籽。
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
問題解決了饥侵,這個(gè)精度問題根據(jù)具體數(shù)據(jù)進(jìn)行調(diào)整。