2

QGIS 2.18.9 In a large project, I have more than 50 layers to which I want to add a new real number field. The default values which are displayed in Field Calculator for field type, size and precision are Integer, 10 and 0 respectively. To change these for each individual layer, I seem to have to enter Decimal Number, 6 and 2 every time which is very time-consuming. Is there a way I can change the default values?

1 Answer 1

1

You can use Add field to attributes table tool either in patch process or using python pyqgis. The following small code will add a new field of Type real, length of 6, and precision of 2:

import processing
layers = qgis.utils.iface.legendInterface().layers()
for layer in layers:
    processing.runalg('qgis:addfieldtoattributestable', layer, "TestField",1,6,2, r"C:\Path\To\Output\Folder\\"+layer.name()) 
  1. First load all the shapefiles that you want to add a new field for them in QGIS using add feature
  2. Click on Open Python Console from the Plugins toolbar
  3. Click on Show editor, as shown below:

enter image description here

  1. Paste the above code, but before running the code, you need to change the following:

    • Replace "TestField" with a new field name
    • Define the output path for the newly created shapefile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.