如何从多边形中删除相同多边形的多个条目?

How to remove multiple entries of same polygon from a multipolygon?

我正在尝试从 multipolygon 几何体中删除完全相似的多边形。我试过 ST_RemoveRepeatedPoints 但它似乎没有删除任何几何图形。谁能告诉我如何删除它们?

如果使用单个 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;