Skip to content

Commit c95a935

Browse files
github-actions[bot]nyalldawson
authored andcommittedDec 1, 2020
Allow specifying lists of values for parameters in qgis_process tool
1 parent 3a32e80 commit c95a935

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed
 

‎src/process/qgsprocess.cpp

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,28 @@ int QgsProcessingExec::run( const QStringList &constArgs )
324324
else
325325
{
326326
const QString value = parts.mid( 1 ).join( '=' );
327-
params.insert( name, value );
327+
if ( params.contains( name ) )
328+
{
329+
// parameter specified multiple times, store all of them in a list...
330+
if ( params.value( name ).type() == QVariant::StringList )
331+
{
332+
// append to existing list
333+
QStringList listValue = params.value( name ).toStringList();
334+
listValue << value;
335+
params.insert( name, listValue );
336+
}
337+
else
338+
{
339+
// upgrade previous value to list
340+
QStringList listValue = QStringList() << params.value( name ).toString()
341+
<< value;
342+
params.insert( name, listValue );
343+
}
344+
}
345+
else
346+
{
347+
params.insert( name, value );
348+
}
328349
}
329350
}
330351
else
@@ -343,7 +364,28 @@ int QgsProcessingExec::run( const QStringList &constArgs )
343364
{
344365
const QString name = parts.first();
345366
const QString value = parts.mid( 1 ).join( '=' );
346-
params.insert( name, value );
367+
if ( params.contains( name ) )
368+
{
369+
// parameter specified multiple times, store all of them in a list...
370+
if ( params.value( name ).type() == QVariant::StringList )
371+
{
372+
// append to existing list
373+
QStringList listValue = params.value( name ).toStringList();
374+
listValue << value;
375+
params.insert( name, listValue );
376+
}
377+
else
378+
{
379+
// upgrade previous value to list
380+
QStringList listValue = QStringList() << params.value( name ).toString()
381+
<< value;
382+
params.insert( name, listValue );
383+
}
384+
}
385+
else
386+
{
387+
params.insert( name, value );
388+
}
347389
}
348390
}
349391

@@ -369,7 +411,7 @@ void QgsProcessingExec::showUsage( const QString &appName )
369411
<< "\tplugins\tlist available and active plugins\n"
370412
<< "\tlist\tlist all available processing algorithms\n"
371413
<< "\thelp\tshow help for an algorithm. The algorithm id or a path to a model file must be specified.\n"
372-
<< "\trun\truns an algorithm. The algorithm id or a path to a model file and parameter values must be specified. Parameter values are specified after -- with PARAMETER=VALUE syntax.\n"
414+
<< "\trun\truns an algorithm. The algorithm id or a path to a model file and parameter values must be specified. Parameter values are specified after -- with PARAMETER=VALUE syntax. Ordered list values for a parameter can be created by specifying the parameter multiple times, e.g. --LAYERS=layer1.shp --LAYERS=layer2.shp\n"
373415
<< "\t\tIf required, the ellipsoid to use for distance and area calculations can be specified via the \"--ELLIPSOID=name\" argument.\n"
374416
<< "\t\tIf required, an existing QGIS project to use during the algorithm execution can be specified via the \"--PROJECT_PATH=path\" argument.\n";
375417

‎tests/src/python/test_qgsprocessexecutable.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,33 @@ def testAlgorithmRunJson(self):
203203
self.assertTrue(os.path.exists(output_file))
204204
self.assertEqual(rc, 0)
205205

206+
def testAlgorithmRunListValue(self):
207+
"""
208+
Test an algorithm which requires a list of layers as a parameter value
209+
"""
210+
output_file = self.TMP_DIR + '/package.gpkg'
211+
rc, output, err = self.run_process(['run', '--json', 'native:package', '--',
212+
'LAYERS={}'.format(TEST_DATA_DIR + '/polys.shp'),
213+
'LAYERS={}'.format(TEST_DATA_DIR + '/points.shp'),
214+
'LAYERS={}'.format(TEST_DATA_DIR + '/lines.shp'),
215+
'OUTPUT={}'.format(output_file)])
216+
res = json.loads(output)
217+
218+
self.assertIn('gdal_version', res)
219+
self.assertIn('geos_version', res)
220+
self.assertIn('proj_version', res)
221+
self.assertIn('qt_version', res)
222+
self.assertIn('qgis_version', res)
223+
224+
self.assertEqual(res['algorithm_details']['name'], 'Package layers')
225+
self.assertEqual(len(res['inputs']['LAYERS']), 3)
226+
self.assertEqual(res['inputs']['OUTPUT'], output_file)
227+
self.assertEqual(res['results']['OUTPUT'], output_file)
228+
self.assertEqual(len(res['results']['OUTPUT_LAYERS']), 3)
229+
230+
self.assertTrue(os.path.exists(output_file))
231+
self.assertEqual(rc, 0)
232+
206233
def testModelHelp(self):
207234
rc, output, err = self.run_process(['help', TEST_DATA_DIR + '/test_model.model3'])
208235
if os.environ.get('TRAVIS', '') != 'true':

0 commit comments

Comments
 (0)
Please sign in to comment.