Advanced Python Field Calculator

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²:

sextante_pythonfieldcalc_area

As you can see, the feature geometry can be accessed using $geom.

If you want to access an existing attribute, that’s possible using <attribute_name>.

Anyway, this is the resulting layer’s attribute table including the new areaKM2 field:

sextante_pythonfieldcalc_area_results

Thanks to Victor for pointing me to the documentation of FieldPyculator which Advanced Python field calculator is based on.

About these ads
3 comments
  1. “it doesn’t use the syntax of QGIS’ default field calculator”

    Not yet, but hopefully it will in 2.1. I’m going to add Python support into the expression engine.

    • That’s going to be great! Looking forward to having the power of Python available in the expression engine. Are you planning to add an option to switch between “normal”/old syntax and Python or will the engine figure it out automagically?

      Thanks for the update Nathan!

Follow

Get every new post delivered to your Inbox.

Join 2,600 other followers

%d bloggers like this: