Skip to content

Commit

Permalink
[processing] Add multi layer outputs to Package Layers and Vector Spl…
Browse files Browse the repository at this point in the history
…it algorithms
  • Loading branch information
nyalldawson committed Feb 11, 2018
1 parent 4bcc9df commit 54a99c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
7 changes: 6 additions & 1 deletion python/plugins/processing/algs/qgis/VectorSplit.py
Expand Up @@ -33,6 +33,7 @@
QgsProcessingParameterField,
QgsProcessingParameterFolderDestination,
QgsProcessingOutputFolder,
QgsProcessingOutputMultipleLayers,
QgsExpression,
QgsFeatureRequest)

Expand All @@ -47,6 +48,7 @@ class VectorSplit(QgisAlgorithm):
INPUT = 'INPUT'
FIELD = 'FIELD'
OUTPUT = 'OUTPUT'
OUTPUT_LAYERS = 'OUTPUT_LAYERS'

def group(self):
return self.tr('Vector general')
Expand All @@ -66,6 +68,7 @@ def initAlgorithm(self, config=None):

self.addParameter(QgsProcessingParameterFolderDestination(self.OUTPUT,
self.tr('Output directory')))
self.addOutput(QgsProcessingOutputMultipleLayers(self.OUTPUT_LAYERS, self.tr('Output layers')))

def name(self):
return 'splitvectorlayer'
Expand All @@ -89,6 +92,7 @@ def processAlgorithm(self, parameters, context, feedback):
geomType = source.wkbType()

total = 100.0 / len(uniqueValues) if uniqueValues else 1
output_layers = []

for current, i in enumerate(uniqueValues):
if feedback.isCanceled():
Expand All @@ -108,8 +112,9 @@ def processAlgorithm(self, parameters, context, feedback):
sink.addFeature(f, QgsFeatureSink.FastInsert)
count += 1
feedback.pushInfo(self.tr('Added {} features to layer').format(count))
output_layers.append(fName)
del sink

feedback.setProgress(int(current * total))

return {self.OUTPUT: directory}
return {self.OUTPUT: directory, self.OUTPUT_LAYERS: output_layers}
5 changes: 5 additions & 0 deletions src/analysis/processing/qgsalgorithmpackage.cpp
Expand Up @@ -53,6 +53,7 @@ void QgsPackageAlgorithm::initAlgorithm( const QVariantMap & )
addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Destination GeoPackage" ), QObject::tr( "GeoPackage files (*.gpkg)" ) ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "OVERWRITE" ), QObject::tr( "Overwrite existing GeoPackage" ), false ) );
addOutput( new QgsProcessingOutputFile( QStringLiteral( "OUTPUT" ), QObject::tr( "GeoPackage" ) ) );
addOutput( new QgsProcessingOutputMultipleLayers( QStringLiteral( "OUTPUT_LAYERS" ), QObject::tr( "Layers within new package" ) ) );
}

QString QgsPackageAlgorithm::shortHelpString() const
Expand Down Expand Up @@ -97,6 +98,7 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters

QgsProcessingMultiStepFeedback multiStepFeedback( layers.count(), feedback );

QStringList outputLayers;
int i = 0;
for ( QgsMapLayer *layer : layers )
{
Expand All @@ -123,6 +125,8 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters
if ( !packageVectorLayer( qobject_cast< QgsVectorLayer * >( layer ), packagePath,
context, &multiStepFeedback ) )
errored = true;
else
outputLayers.append( QStringLiteral( "%1|layername=%2" ).arg( packagePath, layer->name() ) );
break;
}

Expand All @@ -147,6 +151,7 @@ QVariantMap QgsPackageAlgorithm::processAlgorithm( const QVariantMap &parameters

QVariantMap outputs;
outputs.insert( QStringLiteral( "OUTPUT" ), packagePath );
outputs.insert( QStringLiteral( "OUTPUT_LAYERS" ), outputLayers );
return outputs;
}

Expand Down

0 comments on commit 54a99c2

Please sign in to comment.