9

I am currently using the following code to merge two vector layers together:

def append_layers(layer1, layer2, output):
    from processing.core.Processing import Processing
    Processing.initialize()
    import processing.tools
    processing.tools.general.runalg("qgis:mergevectorlayers", layer1, layer2, output)

append_layers(layer1.shp, layer2.shp, output.shp)

This seems to work fine in QGIS 2.14, however it fails in 2.18.1 with the following message:

Error: Wrong number of parameters
ALGORITHM: Merge vector layers

Is there something that has changed between 2.14 and 2.18? Or am i just missing something really obvious?

2 Answers 2

11

Yes, if you type in the following code in the Python Console, the input layer parameter now incorporates multiple layers:

>>>import processing
>>>processing.alghelp("qgis:mergevectorlayers")
ALGORITHM: Merge vector layers
    LAYERS <ParameterMultipleInput>
    OUTPUT <OutputVector>

So one way of resolving this is to add both layers into the input parameter and split them:

processing.tools.general.runalg("qgis:mergevectorlayers", layer1 + ";" + layer2, output)
4
  • Thank you, that worked fine. Has the procedure for 'runalg("gdalogr:convertformat")' changed too? On 2.14 it works using 'processing.tools.general.runalg('gdalogr:convertformat', layer, filetype, "", save_location)' but on 2.18 no errors are flagged up but new files are not created. Commented Jan 6, 2017 at 14:19
  • @JamieTasker - Most welcome! Nope, should be the same parameters. Just tested it and it works fine. If you have problems, you should ask a new question and include all the details such as the input layer type, the output layer type, your code etc :)
    – Joseph
    Commented Jan 6, 2017 at 14:43
  • @Joseph I get the same error Jochen has described in his answer. I'm using version 2.18.16 Python console. It would be interesting to know since which version only a Python list works as vector layer input.
    – Stefan
    Commented Mar 7, 2018 at 6:34
  • @Stefan - I believe the developers changed how the inputs should be used. In the early 2.18 version, layers needed to be separated by a semi-colon which was eventually changed to allow lists. There was a post asking about this.
    – Joseph
    Commented Mar 7, 2018 at 10:14
4

Since trying above answer causes an exception (l1 and l2 are both of type QgsVectorLayer, QGIS 2.18.17 Python console) for me

import processing
[...]
processing.runalg('qgis:mergevectorlayers', l1 + ";" + l2, None)
Traceback (most recent call last):
    File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'QgsVectorLayer' and 'str'

I've been trying

>>> merged = processing.runalg('qgis:mergevectorlayers', [l1, l2], None)
>>> merged['OUTPUT']
u'C:\\Users\\Jochen\\AppData\\Local\\Temp\\processing30eee19448ef42a497403dd3660cafcf\\82e112158f194f59a53e18d5020a9a9d\\OUTPUT.shp'

successfully.

So passing <ParameterMultipleInput> as a python list is a solution to this.

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.