Introduction : I created (with GIS.SE help) a script to batch edit many layers to add one or many fields. The initial aim was to easily add 3 fields to many layers to quicken my labelling process. Example below :
Below the initial code to start the script
##Perso=group ##Add_Labelling_Fields=name ##layer=multiple vector ##Field_ETIKT_X=string ETIKT_X ##Field_ETIKT_Y=string ETIKT_Y ##Field_ETIKT_ROT=string ETIKT_ROTATE from osgeo import ogr layers= layer.split(";") for i in layers: driver = ogr.GetDriverByName('ESRI Shapefile') dataSource = driver.Open(i, 1) #1 is read/write
Actual problem : I'm now trying to find a way to create another similar script to batch update a field on many layers with the following expression : @layer_name to keep a track on where my data comes from, prior to merging multiple layers. It should also be a good way to add various calculations to many fields at once.
My problem here is similar as before : batch processing on editing a field does work with QGIS (through the processing toolboxes), but it creates new layers. I'd like to find the same type of script where you select many layers and it updates a field on all layers without creating new layers.
I tried a few things but I'm not really good with pyqgis yet and I get mixed up with the ways of updating fields depending on where the layer is declared from. There seems to be lots of different ways to do the same thing. No hurry for me, i can manage with other softwares (ex : FME) to do the trick but I think this could be useful for the many people that don't have those expensive softwares.
A this point, I don't exactly know how to call my layer and edit the attributes. I found this topic, but I don't exactly know how to fit them in my code...
it = layer.getFeatures()
for feat in it:
layer.changeAttributeValue(feat.id(), 2, 30)