I have a PostGIS trigger assuring that records with a Point-geometry inserted in a table fall within Polygons in another table.
-- Trigger function CREATE OR REPLACE FUNCTION check_pointWithinPolygon() RETURNS trigger AS $body$ BEGIN IF TG_OP = 'INSERT' THEN IF ((SELECT count(*) FROM (SELECT poly.id FROM poly WHERE st_Within(NEW.geom, poly.geom)) as foo) > 0) THEN RETURN NEW; ELSE RAISE EXCEPTION 'inserted feature ID:% is not within a poly', NEW.id; END IF; END IF; END; $body$ LANGUAGE plpgsql; COMMIT; -- apply the trigger function in a trigger CREATE TRIGGER myPointInPoly BEFORE INSERT OR UPDATE ON points FOR EACH ROW EXECUTE PROCEDURE check_pointWithinPolygon(); COMMIT;
All this works well. I am using QGIS to edit the layer. Some point fall within polygons ( ok) some outside ( and are caught by the trigger). The problem comes when QGIS "save edits" committs the changes.
Currently, all edits are discarded if even a single record is not accepted. I would like to commit per row, so that those that are correct, are inserted. I am not sure whether this is something that can be caught in the trigger logic ( likely), or also needs reflection in the QGIS commit code.