2

I need help with writing a trigger to automatically update and insert into a column the area of a polygon being created using PostgreSQL/PostGIS and QGIS.

This is what I have but its not updating when I add new features.

CREATE OR REPLACE FUNCTION calc_area()
RETURNS trigger AS
$BODY$
BEGIN
NEW.area_ha := ROUND((st_area(NEW.geom::geography)/10000)::numeric,2);
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;

CREATE TRIGGER area_calculate AFTER INSERT OR UPDATE ON public."Guyana_Coastal_Zone"
    FOR EACH ROW EXECUTE PROCEDURE calc_area();
  • 2
    Welcome to GIS SE. As a new user please take the Tour. Trigger maintenance is more of a DBA SE specialty; you may have better luck there. – Vince Jan 20 '17 at 14:57
4

change the trigger to BEFORE INSERT. Then the value will be calculated before the values are inserted into the new row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.