Tuesday 25 April 2017

QGIS Function editor calculate area after transform


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?



Answer




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'))

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...