After recently updating from an outdated version of QGIS to the latest Long Term Release, 3.16.13, the behavior of Batch Processing for the Refactor Fields vector tool has changed. Each time a layer is added, the prompt above appears asking, "Do you want to reset the field mapping?" The answer in my case is always "Yes," since I would be using "Calculate field expression" to populate the Fields mapping column afterwards. However, in some cases, I may be using "Add layers by expression" to add hundreds of vector layers to the Input layer column, and pressing "Yes" hundreds of times even using "Y" as a keyboard shortcut is cumbersome. Is there any way to turn off these "Are you sure?" type prompts for Batch Processing functions, or set a default behavior?
QGIS 3.16.13 Batch Processing – Turning Off Dialog Prompts
batchqgisqgis-3qgis-processing
Related Solutions
Just happens that I created a script for a very similar thing recently (my script merges multiple layers into a new layer, while adding a new attribute to each feature describing where it stems from; this would be more in line with what you apparently intend to do in the future). I slashed the write-to-new-merged-layer part so it solves your question.
The script iterates over multiple vector layers of your choice, then adds the new field with field_name
, unless this field already exists.
After making sure this field exists, it then iterates over all features, applying the layer.name()
to it. This is done inside an edit session, so all the changes are only applied when layer.commitChanges()
is reached. This is both a safety and a performance measure.
NOTE: This is on QGIS 2.18.5.
##input_layers=multiple vector
##field_name=string
##General Tools=group
##layername as field=name
from qgis.core import *
from PyQt4.QtCore import QVariant
input_layers = input_layers.split(';')
for i in input_layers:
layer = processing.getObject(i)
if layer.fieldNameIndex(field_name) == -1:
layer.dataProvider().addAttributes([QgsField(field_name, QVariant.String)])
layer.updateFields()
name = layer.name()
layer.startEditing()
for f in layer.getFeatures():
#f[field_name] = layer.name() # NOTE: This should work, but for some reason doesn't. To manipulate feature attributes, always use the below method! Maybe QGIS3 will fix this :) .
layer.changeAttributeValue(f.id(), layer.fieldNameIndex(field_name), name)
layer.commitChanges()
Your flag
is okay. The error message says Deleted column
is missing.
Slightly scroll to the right-hand side. You will find ellipsis [...] button.
Click on this three-dots button and give a Shapefile name for the new layer. It will be created without the column you have set to Field to delete
.
Best Answer
AFAIK, it's not possible.
You could file an issue report or a feature request in the QGIS repository on GitHub https://github.com/qgis/QGIS/issues.