我正在尝试从multipolygon几何体中删除完全相似的多边形。我试过了ST_RemoveRepeatedPoints
,但它似乎没有删除任何几何体。任何人都可以告诉我如何删除它们?
答案 0 :(得分:1)
如果单个sql查询很难处理,但使用plpgsql函数很容易。
函数ST_Dump()将多几何体扩展为单个几何体。您可以迭代单个几何并检查唯一性:
CREATE or REPLACE FUNCTION clean_multipoly(input_multipoly geometry)
RETURNS GEOMETRY AS $$
DECLARE
single_poly geometry;
polygons_array geometry[];
poly_array_element GEOMETRY;
is_contained BOOLEAN;
BEGIN
-- initialize the array to a empty array
polygons_array = array[]::geometry[];
-- now iterate over the single polygons
FOR single_poly IN SELECT (ST_Dump(input_multipoly)).geom LOOP
is_contained = false;
-- Now you need the iterate again over the array you are building
-- and check every element if is equal to the actual single polygon.
-- You cannot use a array operator for checking if a element is already contained in the array,
-- because this would eliminate polygons which are different but have the same extent.
-- Only ST_Equals checks properly for geometries equality
FOREACH poly_array_element IN ARRAY polygons_array LOOP
IF ST_equals(single_poly, poly_array_element) THEN
is_contained = TRUE;
END IF;
END LOOP;
IF is_contained = FALSE THEN
polygons_array = array_append(polygons_array, single_poly);
END IF;
END LOOP;
RETURN ST_collect(polygons_array);
END;
$$
LANGUAGE plpgsql;
使用此功能:
SELECT clean_multipoly(your_geom) FROM your_table;