9

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?

10
  1. Create a new script from:

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

    ##Example=name
    ##Layer=vector
    
    from qgis.core import QgsField
    from PyQt4.QtCore import QVariant
    
    layer = processing.getObject(Layer)
    idx = layer.fieldNameIndex('Source')
    
    if idx != -1:
        pass
    else:
        layer.startEditing()
        layer.dataProvider().addAttributes( [ QgsField("Source", QVariant.Int) ] )
        layer.updateFields()
        for feat in layer.getFeatures():
            layer.changeAttributeValue(feat.id(), layer.fieldNameIndex('Source'), '0')
        layer.commitChanges()
    
  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.

  • 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.