I want to check a vector file if there is a column "Source".

If yes, nothing to do. If there is no column "Source" it should be created and filled up with '0'.

Is this possible to do via the modeler?

  1. Create a new script from:

    Processing Toolbox > Scripts > Tools > Create new script
  2. Use something like the following:

    from qgis.core import QgsField
    from PyQt4.QtCore import QVariant
    layer = processing.getObject(Layer)
    idx = layer.fieldNameIndex('Source')
    if idx != -1:
        layer.dataProvider().addAttributes( [ QgsField("Source", QVariant.Int) ] )
        for feat in layer.getFeatures():
            layer.changeAttributeValue(feat.id(), layer.fieldNameIndex('Source'), '0')
  3. Save the script in your C:/Users/You/.qgis2/processing/scripts/ directory. Then you should be able to call this script in your modeler.

| improve this answer | |
  • 1
    @Pimpel - Most welcome, glad it helped =) – Joseph Nov 24 '16 at 13:31

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.