While trying to deal with GEOMETRYCOLLECTION in PostGIS, I've found here what seems to address the conversion of GEOMETRYCOLLECTION into simple GEOMETRY, but it only works with the ST_Intersect internal function.
As I'm quite lazy, I thought it would be nice to make it more powerful by giving the function to be executed as an argument, but it doesn't work this way.
CREATE OR REPLACE FUNCTION ST_GeoCollConv_(geofunction, geometry, geometry) RETURNS boolean AS $$
DECLARE
is1collection boolean;
is2collection boolean;
BEGIN
is1collection := GeometryType($1) = 'GEOMETRYCOLLECTION';
is2collection := GeometryType($2) = 'GEOMETRYCOLLECTION';
IF NOT is1collection AND NOT is2collection THEN
return PERFORM geofunction($1, $2);
END IF;
IF NOT is1collection THEN
FOR i IN 1..ST_NumGeometries($2) LOOP
IF PERFORM geofunction($1, ST_GeometryN($2, i)) THEN
RETURN true;
END IF;
END LOOP;
ELSIF NOT is2collection THEN
FOR i IN 1..ST_NumGeometries($1) LOOP
IF PERFORM geofunction(ST_GeometryN($1, i), $2) THEN
RETURN true;
END IF;
END LOOP;
ELSE
FOR i IN 1..ST_NumGeometries($1) LOOP
IF ST_GeoCollConv_(geofunction, $1, $2) THEN
RETURN true;
END IF;
END LOOP;
END IF;
RETURN false;
END;
$$ LANGUAGE 'plpgsql';
How can I achieve this ?