If you are working with PostGIS as back-end, you could create a new column area_auto
and create a trigger to keep it up-to-date with your features' areas.
First add a column to your table (here table=poly_area
, new column=area_auto
)
ALTER TABLE poly_area ADD COLUMN area_auto double precision;
Then populate the existing polygons with their areas:
UPDATE poly_area SET area_auto = ST_Area(geom::geography);
(assuming geom is in EPSG:4326, it must be cast to geography).
Then create a function to update column area_auto
:
CREATE OR REPLACE FUNCTION update_polygon_area_auto()
RETURNS TRIGGER AS $$
BEGIN
NEW.area_auto = ST_Area(NEW.geom::geography);
RETURN NEW;
END;
$$ language 'plpgsql';
Finally, create a trigger to automatically update area_auto
:
CREATE TRIGGER update_area_auto
BEFORE INSERT OR UPDATE
ON public.poly_area
FOR EACH ROW
EXECUTE PROCEDURE public.update_polygon_area_auto();
Now area_auto
is automatically updated (in square meters) when you commit your changes in QGIS. You can label your features with it, and it's available in the attribute table.

$area
in your virtual field? – thibautg Jun 12 '15 at 12:02