4

In the QGIS function editor, it is possible to transform geometry using transform(geometry,'EPSG:xxx','EPSG:yyy') - however there isn't an area() function to recalculate the area of this.

The reason I am looking to do this is to have area calculated when a map is produced (labelled and as a virtual field in the attribute table); rather than store the area within the database as it is likely to change frequently as users modify the polygons?

4
  • Can't you just use $area in your virtual field? – thibautg Jun 12 '15 at 12:02
  • My data is in geographic coordinates, so I need it in square meters hence the transform. – lolcat Jun 12 '15 at 12:03
  • That's an interesting question. I don't know if you can calculate the area without first transforming your layer to a projected coordinates system. – thibautg Jun 12 '15 at 12:24
  • I'll see if I can use psycopg2 to get the value from my database using python instead of the editor. I can imagine that slowing things down a fair bit the more polygons there is. – lolcat Jun 12 '15 at 12:44
4

What you are aiming is a quite nice outcome about being able to transform the geometry. You can do it using an expression but you will need an extra plugin.

  1. Install reffunctions plugin
  2. Then use the following expression:

    WKTarea(geomToWKT( transform( $geometry , 'EPSG:xxxx', 'EPSG:yyyy')))

UPDATE

Transforming the geometry to WKT and then using an external plugin to solve the problem was bugging me. So I came up with a very simple better (I think) solution.

All that was needed was to add the following function to the function editor:

@qgsfunction(args="auto", group='Custom')
def area(geometry, feature, parent):
    return geometry.area()

The basic usage is:

area($geometry)

And the on-the-fly area calculation expression became:

area(transform(  $geometry , 'EPSG:xxxx', 'EPSG:yyyy'))
1
  • Thank you for mentioning this very useful plugin. It's simpler than my answer. – thibautg Jun 12 '15 at 14:55
1

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_autois 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.

Automatic area calculation

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.