Advanced Python field calculator is one of the numerous tools in Sextante for QGIS. It’s extremely powerful but it doesn’t use the syntax of QGIS’ default field calculator (the one you can access via attribute table). Therefore, here comes a short introduction:
If you want to reproduce this example, I used a dataset of town areas from the new open government data site of Lower Austria.
The upper half of the Advanced Python field calculator is rather self-explanatory but the lower half is where it gets interesting: Code in the global expression section will be executed only once before the calculator starts iterating through all the features of the input layer. Therefore, this is the correct place to import necessary modules or to calculate variables that will be used in subsequent calculations. Code in the formula section will be evaluated for each feature in the input layer. As shown in the following example, this is where we can calculate new values, e.g. the area of the polygons in km²:
As you can see, the feature geometry can be accessed using
If you want to access an existing attribute, that’s possible using
Anyway, this is the resulting layer’s attribute table including the new areaKM2 field:
Thanks to Victor for pointing me to the documentation of FieldPyculator which Advanced Python field calculator is based on.