[postgis-users] geomunion/plpgsql catch exception

Seth Williams swilliams at wsi.com
Fri May 9 13:05:39 PDT 2008


Has anyone tried catching an exception in plpgsql when running into
"ERROR:  GEOS union() threw an error!". 

I am running into this and would like to retrying using a gradually
increasing tolerance "geomunion(simplify(the_geom), tolerance)" until I
am able to get a return value. I have not yet been able to catch the
exception in PLPSQL however. Does anyopne know what the [CONDITION]
should be in the following sql? 

 CREATE OR REPLACE FUNCTION safe_geomunion(geom1 geometry, geom2
geometry) RETURNS geometry
    AS $$
DECLARE
    ret_geom public.geometry := NULL;
    tolerance FLOAT;
    max_tolerance FLOAT := 0.005;

BEGIN
    tolerance := 0.0001;
    IF geom1 IS NULL THEN
        ret_geom := geom2;
    ELSIF geom2 IS NULL THEN
        ret_geom := geom1;
    ELSE
        BEGIN
            SELECT geomunion(geom1, geom2) INTO ret_geom;
            EXCEPTION 
                WHEN [CONDITION] THEN
                    -- Try with geomunion(simplify(region), 0,001)
                    RAISE WARNING 'FAILED TO CONSTRUCT UNION';
        END;
    END IF;

    RETURN ret_geom;
END;
$$
    LANGUAGE plpgsql;

DROP AGGREGATE IF EXISTS safe_geomunion(geometry);
CREATE AGGREGATE safe_geomunion (
        sfunc    = safe_geomunion,
        basetype = geometry,
        stype    = geometry
);





More information about the postgis-users mailing list