TransWikia.com

How to set the "LAYERS" parameter of the QGIS 3 algorithm "native:mergevectorlayers"

Geographic Information Systems Asked by Comrade Che on January 23, 2021

I do not understand how to set the LAYERS parameter for the QGIS 3 native: mergevectorlayers algorithm (for standalone python code).

Merge vector layers (native:mergevectorlayers)
...
----------------
Input parameters
----------------    
LAYERS:  <QgsProcessingParameterMultipleLayers>
    Input layers

CRS:  <QgsProcessingParameterCrs>
    Destination CRS    

OUTPUT:  <QgsProcessingParameterFeatureSink>
    Merged    
----------------
Outputs
----------------    
OUTPUT:  <QgsProcessingOutputVectorLayer>

The help processing.algorithmHelp('native:mergevectorlayers') says that this parameter has the data type QgsProcessingParameterMultipleLayers, but I do not understand how to use it.

Is this a list of the QgsVectorLayer objects?

Seems not because I can’t execute the following code:

import sys
import qgis.core
qgis.core.QgsApplication.setPrefixPath(r'C:OSGeo4W64appsqgis', True)
qgs = qgis.core.QgsApplication([], False)
qgs.initQgis()
sys.path.append(r'C:OSGeo4W64appsqgispythonplugins')
import processing
processing.core.Processing.Processing.initialize()
qgis.core.QgsApplication.processingRegistry().addProvider(qgis.analysis.QgsNativeAlgorithms())


input_files_list = [r"W:MODISshp_filesMOD03_2018-01-01.shp",
                    r"W:MODISshp_filesMOD03_2018-01-02.shp",]
layers_list = []
for num, file in enumerate(input_files_list):
    layer_name = 'layer_' + str(num)
    print(layer_name, file)
    layer = qgis.core.QgsVectorLayer(file, layer_name, 'ogr')
    print(layer)
    layers_list.append(layer)
    print('')

print('layers_list=', layers_list)
print('It seems that the script is stopped in the next line')
in_layers = qgis.core.QgsProcessingParameterMultipleLayers(layers_list)
print('This will not be printed')

output_file_path = r"W:MODISshp_filesMerged.shp"
out_layer = qgis.core.QgsVectorLayer(output_file_path, 'out_layer', 'ogr')

params = {'LAYERS': in_layers,
          'OUTPUT': out_layer,}

feedback = qgis.core.QgsProcessingFeedback()

alg_name = 'native:mergevectorlayers'

print('='*50)
print(processing.algorithmHelp(alg_name))
print('='*50)
res = processing.run(alg_name, params, feedback=feedback)
print('='*50)
print(res['OUTPUT'])

qgs.exitQgis()

UPDATE 1:
I changed my stand-alone python code to check if I can pass parameters as a clear shapefile pathes, code execution gives this error:

Unable to execute algorithm
Incorrect parameter value for LAYERS

Here is new code:

import sys
import qgis.core
qgis.core.QgsApplication.setPrefixPath(r'C:OSGeo4W64appsqgis', True)
qgs = qgis.core.QgsApplication([], False)
qgs.initQgis()
sys.path.append(r'C:OSGeo4W64appsqgispythonplugins')
import processing
processing.core.Processing.Processing.initialize()
qgis.core.QgsApplication.processingRegistry().addProvider(qgis.analysis.QgsNativeAlgorithms())

in_layers = [r"W:MODISshp_filesMOD03_2018-01-01.shp",
             r"W:MODISshp_filesMOD03_2018-01-02.shp",]
output_file_path = r"W:MODISshp_filesMerged.shp"
params = {'LAYERS': in_layers,
          'OUTPUT': output_file_path,}
feedback = qgis.core.QgsProcessingFeedback()
alg_name = 'native:mergevectorlayers'    
res = processing.run(alg_name, params, feedback=feedback)    
print(res)    
qgs.exitQgis()

One Answer

It's literally just a list of QgsVectorLayer objects, or a list of layer paths, E.g.

in_layers =  [r"W:MODISshp_filesMOD03_2018-01-01.shp",
                r"W:MODISshp_filesMOD03_2018-01-02.shp",]

Answered by ndawson on January 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP