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
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())
- First load all the shapefiles that you want to add a new field for them in QGIS using
add feature
- Click on Open
Python Console
from thePlugins toolbar
- Click on
Show editor
, as shown below:
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