1

Why am I getting the error message

The following layers were not correctly generated: Output

when writing a script that uses the QGIS geoalgorithm "Select by attribute"?

Here's my script (My "Input" and "Output" parameters are set in the GUI when the script is run.):

processing.runalg('qgis:selectbyattribute', Input, "STRNAME", 0, 'Robie', Output)

I used this format after looking at the HELP tab of the "Select by attribute" tool and seeing the following instruction:

processing.runalg('qgis:selectbyattribute', input, field, operator, value, output)

Confusing then, to find the answer (helped by @StephenKay) is to remove the final parameter ('output'). Would have been less misleading if the tool's own HELP tab did not show it as the 5th and final parameter, or at the very least indicated that it is an optional parameter (as the tool clearly works without it).

2 Answers 2

2

Tip - if running under Linux at least, stating QGIS from a terminal gives a lot more debugging info, including some errors which don't get as far as the gui.

In your case, it says that the number of parameters is incorrect.

If you look at the answer @gene gave, you'll see that final parameter isn't needed.

Removing the final output parameter gets rid of that, but you still get the 'unable to load /tmp/processing/....'.

The reason is the output isn't being generated.

It seems that this particular algorithm creates a selection on the input layer, but doesn't actually create the output layer for you.

But you can then copy the selection to the output layer using runalg('qgis:saveselectedfeatures',input,output), which fixes the problem.

I've switched it to using == (operator 0) which I think was your original intent. Also added a text value you can use to specify the value you want to compare to.

##Vector=group
##input=vector
##fieldname=field input
##fieldvalue=string ""
##selection=output vector

from qgis.core import *

# note.. no 'output' at end. This seems to create selection on the input layer
# I'm using equals (operator 0)
processing.runalg('qgis:selectbyattribute',input, fieldname, 0, fieldvalue)

# copy selection over to output layer
processing.runalg('qgis:saveselectedfeatures', input, selection)

# clear selection on input layer
processing.runalg('qgis:selectbyexpression', input, 'True', 3)

The last line is optional but it clears the selection on the input layer. Without this, running a second time will only run against the selection from the previous run, so you'd need to manually "deselect all" on the layer.

It might be that you need to add the final "output" param if running from the console, but in this case you're writing a user script. Hopefully someone can clarify why you need to remove the output parameter, and whether it's just for this particular algorithm.

Now you should be able to run it from the processing panel, or drop it into a model.

1
  • Thanks for pointing out the 5th parameter should be removed! Once I did so my script worked as it should. Your example script with 2 extra methods is also much appreciated!
    – Waterman
    Commented Oct 18, 2015 at 2:13
1

1) Processing has a log file located in the /.../.qgis2/processing folder (processing.log). if you open this file, you find the result of the process (or the resulting errors that allow you to solve them theoretically...)

The result of the "Select by attribute" Toolbox command is

Started logging at Sat Oct 17 2015 18:57:39
ALGORITHM|Sat Oct 17 2015    
19:23:13|processing.runalg("qgis:selectbyattribute","/Users/me/test.shp","DIP",2,"40")

This shows you the correct order and the format of the parameters

2) The GUI of the the "Select by attribute" Toolbox shows you that the operator field is missing in your script/GUI

enter image description here

Just my own 2 cents after scouring the internet for QGIS scripting resources similar to the excellent ArcGIS Help page.

ESRI has a long history, many developers and a lot of money when QGIS is young and depends on the goodwill and efforts of the QGIS community. For the moment, the only solution is to search into the documentation, GIS SE, blogs or use books as The PyQGIS Programmer's Guide, QGIS 2 Python Programming Cookbook and other books of Packt Publishing) and you can participate if you want.

1
  • My GUI's lack of "Operator" input wasn't the problem because my script had it "hard-wired". Happy I asked this question, despite losing a rep point, because as I said the tool's own "Console Usage" calls for 5 parameters, the 5th being "Output", the source of my error. Seems misleading for the tool to suggest 5 parameters are needed when it only works with 4 but as @Steven Kay says hopefully someone can clarify. Yes, I realize ESRI has time and $ on their side, just pointing out areas for improvement in the spirit of making Q even better! Cheers.
    – Waterman
    Commented Oct 18, 2015 at 2:23

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.