2

I am currently working on a code that adds a field to a shapefile and inputs its filepath. I understand that the processing.run has changed from qgis 2 to qgis 3 but I am not sure why I am getting this error. Below is the code that I am working on.

enter code here
from qgis.core import *
import sys
import glob, os, shutil, time, string
import qgis.utils
import processing
from qgis.analysis import *

Location = r"\shapefile"


for root,d_names,f_names in os.walk(Location):
    for f in f_names:

        if f.endswith('.shp'):
            if ".kml" in f:
            continue
            src = os.path.join(root,f)
            layer = QgsVectorLayer(src, f, "ogr")
            param = {layer, 'Filepath' , 2 , 150 ,0 , True, root, src}

This is the error I get:

TypeError: QgsProcessingAlgorithm.parameterAsBool(): argument 1 has unexpected type 'set'

1

1 Answer 1

6

You're passing processing.run a python set instead of a dict.

Instead of:

param = {layer, 'Filepath' , 2 , 150 ,0 , True, root, src}
processing.run('qgis:fieldcalculator', param)

Use something like:

param = {
'INPUT': layer, 'FIELD_NAME': 'Filepath', 'FIELD_TYPE': 2 , FIELD_LENGTH': 150 ,'FIELD_PRECISION': 0 , 
'NEW_FIELD':True, 'FORMULA': 'Your calculator expression', 'OUTPUT':'your output file'}
processing.run('qgis:fieldcalculator', param , feedback=feedback, context=context)
4
  • That is good to know, thank you very much. I am still having the same error as before though. I updated the code to match what I have now.
    – Apmartin
    Jan 30, 2019 at 22:20
  • @Apmartin You still have the original param = {layer, 'Filepath' , 2 , 150 ,0 , True, root, src} in there which is causing the same error.
    – user2856
    Jan 30, 2019 at 22:41
  • ok that was silly of me. Thank you for pointing that error to me. I changed the post back to the original post. One more question, what is the use of "feedback=feedback" and "context=context"? I get the error that they are not defined. Am I supposed to import another library for that?
    – Apmartin
    Jan 30, 2019 at 22:58
  • @Apmartin you can remove them if you're running a standalone script. If you're writing a processing plugin algorithm, those variables will get passed to the processAlgorithm method of your algorithm class and then you can pass them to the processing.run function.
    – user2856
    Jan 30, 2019 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.