5

I'm trying to merge all shapefiles in a directory using the following Python script from the console in QGIS 3.4.

The script was sourced from the following question:

Merging all vector files within directory with QGIS?

gis.stackexchange.com/questions/327398/merging-all-vector-files-within-directory-with-qgis

import glob, os, processing
path_to_shp = "P:/GIS/Basemapping/Topographic Area Schema 1/"
os.chdir(path_to_shp)
filelist = []
for fname in glob.glob("*.shp"):
    uri = path_to_shp + fname
    filelist.append(uri)

fileliststring = ';'.join(filelist)
processing.run("qgis:mergevectorlayers", fileliststring, "P:/GIS/Basemapping/Topographic Area Schema 1/merged.shp")

Running this results in:

TypeError: QgsProcessingAlgorithm.checkParameterValues(): argument 1 has unexpected type 'str'

The first time I ran into this I checked the input parameters for the algorithm using:

processing.algorithmHelp("qgis:mergevectorlayers")

Based on the algorithmHelp I assumed I should be passing a list of filepaths as input:

list[str]: list of layer sources

Running the following modification of the original script:

import glob, os, processing
path_to_shp = "P:/GIS/Basemapping/Topographic Area Schema 1/"
os.chdir(path_to_shp)
filelist = []
for fname in glob.glob("*.shp"):
    uri = path_to_shp + fname
    filelist.append(uri)

processing.run("qgis:mergevectorlayers", filelist, "P:/GIS/Basemapping/Topographic Area Schema 1/merged.shp")

results in the following error:

TypeError: QgsProcessingAlgorithm.checkParameterValues(): argument 1 has unexpected type 'list'

I feel I must be missing something obvious here. I've tried passing a list and a string of filepaths but neither seems to work. Can anyone help?

0

1 Answer 1

5

That's not how you call processing.run. You should check out the official QGIS documentation

Basically, processing.run expects the algorithm identifier, and a dictionary containing the processing parameters. This should work:

processing.run(
    "qgis:mergevectorlayers",
    {
        "LAYERS": filelist,
        "OUTPUT": "P:/GIS/Basemapping/Topographic Area Schema 1/merged.shp",
    },
)
3
  • Works perfectly. Thanks for the help.
    – Stuart
    Commented Jan 24, 2020 at 15:17
  • I'm trying to replicate this code, but I receive "Incorrect parameter value for LAYERS" error. The script is exactly the same (safe for directories of course). I use QGIS 3.12.3 - I don't understand what's wrong.
    – RobVizhy
    Commented Jun 7, 2020 at 9:57
  • How do you create your "filelist"? It must be a python list of str Commented Jun 8, 2020 at 16:00

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.