Is there a way to apply field calculator for multiple layers at once?

For instance, I have 80 layers as the picture shown below. If I want to apply $area for all the layers, is there a quicker way to do it except manually creating 'area' field?

enter image description here

this is how it looks after applying recommendation using pyqgis enter image description here

4 Answers 4


You can do this by making 2 loops: the first on the layers of your project and the second on the features of each layer.

# Project layers 
project_layers = QgsProject.instance().mapLayers().values()

# Loop on each layer 
for layer in project_layers :
    layer.dataProvider().addAttributes([QgsField("area", QVariant.Double)])

    # Loop on each feature 
    for feature in layer.getFeatures():
        area = feature.geometry().area()

    # Apply changes
  • this works as it gives new column but values are not applied. (I've attached how it looks in original post)
    – jinha kim
    Dec 18, 2019 at 3:46
  • If the area field already exists, QGIS will create the fields area_1, area_2, area_3, ... Except that in the code you put the attributes in the area field so there is no value in the area_1 and area_2 fields Dec 18, 2019 at 8:02

An alternative solution to using written Python code would be to use the Graphical modeller to create a small script. Just set a Vector-Layer as input and add the field calculator as step with $area as a formula. Something like this:

A small script for a field calculator operation

You can then run the script in Batchmode and do the same operation for several layers at once.


You can run the Field Calculator in Batch Processing Mode directly from the Processing Toolbox.

Right click on the tool name and choose "batch processing mode."

enter image description here

Or you can run add geometry attributes in batch processing mode from the processing toolbox. This method will add more geometry attributes than just area.


Other solution but it will make a new layer with the result and will not modify your current layers.

The steps : First use merge vector layers and select all the layers. This will merge all your vector layer in one new layer and add two fields "layer" with the source layer name and "path" with the source layer path. Then you can make your new field "area" to store $area.

Then if you want back your 80 layers you can use split vector layers based on "layer" field.

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.