Quantcast
Channel: Question and Answer » postgresql
Viewing all articles
Browse latest Browse all 1138

Postgres: plpgsql combine ST_Overlaps and ST_Intersects

$
0
0

I have a question regarding creating a trigger function in postgres with 2 conditions:

when creating a linestring in QGIS their must either overlap nor intersect.
So i wrote 2 function, each working with either ST_Overlaps or ST_Intersects.

CREATE OR REPLACE FUNCTION check_linesintersecting() RETURNS TRIGGER AS
$BODY$
DECLARE
BEGIN 
IF TG_OP = 'INSERT'
THEN
IF
(SELECT COUNT(*)
FROM
(SELECT gid
FROM line_layer AS t
WHERE st_intersects(NEW.geom, t.geom)) AS foo)
THEN
RAISE EXCEPTION 'Speicherung abgebrochen: Ueberschneidende Linien!';
RETURN NULL;
ELSE
RETURN NEW;
END IF;
ELSIF TG_OP = 'UPDATE'
THEN
IF
(SELECT COUNT(*)
FROM
(SELECT gid
FROM line_layer AS t
WHERE st_intersects(NEW.geom, t.geom)
AND (t.gid <> OLD.gid)) AS foo) > 0
THEN
RAISE EXCEPTION 'Speicherung abgebrochen: Ueberschneidende Linien!';
RETURN NULL;
ELSE RETURN NEW;
END IF;
END IF;

END;
$BODY$
LANGUAGE plpgsql;

All I had to to in oder to make it detect overlapping was to change the ST_Intersects into ST_Overlaps.

But how can I combinde ST_Overlaps and ST_Intersects?
I’ve tried using the same loop right after each other but only the the first function then is working. Any clues? Thanks in advance!!


Viewing all articles
Browse latest Browse all 1138

Trending Articles