34#include <QRegularExpression>
37QgsProcessingModelAlgorithm::QgsProcessingModelAlgorithm(
const QString &name,
const QString &group,
const QString &groupId )
38 : mModelName( name.isEmpty() ? QObject::tr(
"model" ) : name )
39 , mModelGroup( group )
40 , mModelGroupId( groupId )
43void QgsProcessingModelAlgorithm::initAlgorithm(
const QVariantMap & )
47QString QgsProcessingModelAlgorithm::name()
const
52QString QgsProcessingModelAlgorithm::displayName()
const
57QString QgsProcessingModelAlgorithm::group()
const
62QString QgsProcessingModelAlgorithm::groupId()
const
67QIcon QgsProcessingModelAlgorithm::icon()
const
72QString QgsProcessingModelAlgorithm::svgIconPath()
const
77QString QgsProcessingModelAlgorithm::shortHelpString()
const
79 if ( mHelpContent.empty() )
85QString QgsProcessingModelAlgorithm::shortDescription()
const
87 return mHelpContent.value( QStringLiteral(
"SHORT_DESCRIPTION" ) ).toString();
90QString QgsProcessingModelAlgorithm::helpUrl()
const
92 return mHelpContent.value( QStringLiteral(
"HELP_URL" ) ).toString();
95QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm(
const QgsProcessingModelChildAlgorithm &child,
const QVariantMap &modelParameters,
const QVariantMap &results,
const QgsExpressionContext &expressionContext, QString &error,
const QgsProcessingContext *context )
const
100 const QgsProcessingModelChildParameterSources paramSources = child.parameterSources().value( def->name() );
102 QString expressionText;
103 QVariantList paramParts;
104 for (
const QgsProcessingModelChildParameterSource &source : paramSources )
106 switch ( source.source() )
108 case QgsProcessingModelChildParameterSource::StaticValue:
109 paramParts << source.staticValue();
112 case QgsProcessingModelChildParameterSource::ModelParameter:
113 paramParts << modelParameters.value( source.parameterName() );
116 case QgsProcessingModelChildParameterSource::ChildOutput:
118 QVariantMap linkedChildResults = results.value( source.outputChildId() ).toMap();
119 paramParts << linkedChildResults.value( source.outputName() );
123 case QgsProcessingModelChildParameterSource::Expression:
126 paramParts << exp.evaluate( &expressionContext );
127 if ( exp.hasEvalError() )
129 error = QObject::tr(
"Could not evaluate expression for parameter %1 for %2: %3" ).arg( def->name(), child.description(), exp.evalErrorString() );
133 case QgsProcessingModelChildParameterSource::ExpressionText:
139 case QgsProcessingModelChildParameterSource::ModelOutput:
144 if ( ! expressionText.isEmpty() )
146 return expressionText;
148 else if ( paramParts.count() == 1 )
149 return paramParts.at( 0 );
155 QVariantMap childParams;
156 const QList< const QgsProcessingParameterDefinition * > childParameterDefinitions = child.algorithm()->parameterDefinitions();
159 if ( !def->isDestination() )
161 if ( !child.parameterSources().contains( def->name() ) )
164 const QVariant value = evaluateSources( def );
165 childParams.insert( def->name(), value );
172 bool isFinalOutput =
false;
173 QMap<QString, QgsProcessingModelOutput> outputs = child.modelOutputs();
174 QMap<QString, QgsProcessingModelOutput>::const_iterator outputIt = outputs.constBegin();
175 for ( ; outputIt != outputs.constEnd(); ++outputIt )
177 if ( outputIt->childOutputName() == destParam->
name() )
179 QString paramName = child.childId() +
':' + outputIt.key();
180 bool foundParam =
false;
184 if ( modelParameters.contains( paramName ) )
186 value = modelParameters.value( paramName );
195 if ( modelParameters.contains( modelParam->name() ) )
197 value = modelParameters.value( modelParam->name() );
205 if ( value.userType() == QMetaType::type(
"QgsProcessingOutputLayerDefinition" ) )
210 value = QVariant::fromValue( fromVar );
213 childParams.insert( destParam->
name(), value );
215 isFinalOutput =
true;
220 bool hasExplicitDefinition =
false;
221 if ( !isFinalOutput && child.parameterSources().contains( def->name() ) )
224 const QVariant value = evaluateSources( def );
225 if ( value.isValid() )
227 childParams.insert( def->name(), value );
228 hasExplicitDefinition =
true;
232 if ( !isFinalOutput && !hasExplicitDefinition )
237 bool required =
true;
240 required = childOutputIsRequired( child.childId(), destParam->
name() );
252const QgsProcessingParameterDefinition *QgsProcessingModelAlgorithm::modelParameterFromChildIdAndOutputName(
const QString &childId,
const QString &childOutputName )
const
256 if ( !definition->isDestination() )
259 const QString modelChildId = definition->
metadata().value( QStringLiteral(
"_modelChildId" ) ).toString();
260 const QString modelOutputName = definition->metadata().value( QStringLiteral(
"_modelChildOutputName" ) ).toString();
262 if ( modelChildId == childId && modelOutputName == childOutputName )
268bool QgsProcessingModelAlgorithm::childOutputIsRequired(
const QString &childId,
const QString &outputName )
const
271 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
272 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
274 if ( childIt->childId() == childId || !childIt->isActive() )
278 QMap<QString, QgsProcessingModelChildParameterSources> candidateChildParams = childIt->parameterSources();
279 QMap<QString, QgsProcessingModelChildParameterSources>::const_iterator childParamIt = candidateChildParams.constBegin();
280 for ( ; childParamIt != candidateChildParams.constEnd(); ++childParamIt )
282 const auto constValue = childParamIt.value();
283 for (
const QgsProcessingModelChildParameterSource &source : constValue )
285 if ( source.source() == QgsProcessingModelChildParameterSource::ChildOutput
286 && source.outputChildId() == childId
287 && source.outputName() == outputName )
299 QSet< QString > toExecute;
300 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
301 QSet< QString > broken;
302 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
304 if ( childIt->isActive() )
306 if ( childIt->algorithm() )
307 toExecute.insert( childIt->childId() );
309 broken.insert( childIt->childId() );
313 if ( !broken.empty() )
314 throw QgsProcessingException( QCoreApplication::translate(
"QgsProcessingModelAlgorithm",
"Cannot run model, the following algorithms are not available on this system: %1" ).arg(
qgsSetJoin( broken, QLatin1String(
", " ) ) ) );
316 QElapsedTimer totalTime;
322 QVariantMap childResults;
323 QVariantMap childInputs;
327 QVariantMap finalResults;
328 QSet< QString > executed;
329 bool executedAlg =
true;
330 while ( executedAlg && executed.count() < toExecute.count() )
333 for (
const QString &childId : std::as_const( toExecute ) )
338 if ( executed.contains( childId ) )
341 bool canExecute =
true;
342 const QSet< QString > dependencies = dependsOnChildAlgorithms( childId );
343 for (
const QString &dependency : dependencies )
345 if ( !executed.contains( dependency ) )
357 const QgsProcessingModelChildAlgorithm &child = mChildAlgorithms[ childId ];
358 std::unique_ptr< QgsProcessingAlgorithm > childAlg( child.algorithm()->create( child.configuration() ) );
361 if ( feedback && !skipGenericLogging )
362 feedback->
pushDebugInfo( QObject::tr(
"Prepare algorithm: %1" ).arg( childId ) );
366 << createExpressionContextScopeForChildAlgorithm( childId, context, parameters, childResults );
370 QVariantMap childParams = parametersForChildAlgorithm( child, parameters, childResults, expContext, error, &context );
371 if ( !error.isEmpty() )
374 if ( feedback && !skipGenericLogging )
375 feedback->
setProgressText( QObject::tr(
"Running %1 [%2/%3]" ).arg( child.description() ).arg( executed.count() + 1 ).arg( toExecute.count() ) );
379 for (
auto childParamIt = childParams.constBegin(); childParamIt != childParams.constEnd(); ++childParamIt )
381 params << QStringLiteral(
"%1: %2" ).arg( childParamIt.key(),
382 child.algorithm()->parameterDefinition( childParamIt.key() )->valueAsPythonString( childParamIt.value(), context ) );
385 if ( feedback && !skipGenericLogging )
387 feedback->
pushInfo( QObject::tr(
"Input Parameters:" ) );
388 feedback->
pushCommandInfo( QStringLiteral(
"{ %1 }" ).arg( params.join( QLatin1String(
", " ) ) ) );
391 QElapsedTimer childTime;
396 QThread *modelThread = QThread::currentThread();
398 auto prepareOnMainThread = [modelThread, &ok, &childAlg, &childParams, &context, &modelFeedback]
400 Q_ASSERT_X( QThread::currentThread() == qApp->thread(),
"QgsProcessingModelAlgorithm::processAlgorithm",
"childAlg->prepare() must be run on the main thread" );
401 ok = childAlg->prepare( childParams, context, &modelFeedback );
402 context.pushToThread( modelThread );
406 if ( modelThread == qApp->thread() )
407 ok = childAlg->prepare( childParams, context, &modelFeedback );
410 context.pushToThread( qApp->thread() );
411 QMetaObject::invokeMethod( qApp, prepareOnMainThread, Qt::BlockingQueuedConnection );
414 Q_ASSERT_X( QThread::currentThread() == context.thread(),
"QgsProcessingModelAlgorithm::processAlgorithm",
"context was not transferred back to model thread" );
428 auto runOnMainThread = [modelThread, &context, &modelFeedback, &results, &childAlg, &childParams]
430 Q_ASSERT_X( QThread::currentThread() == qApp->thread(),
"QgsProcessingModelAlgorithm::processAlgorithm",
"childAlg->runPrepared() must be run on the main thread" );
431 results = childAlg->runPrepared( childParams, context, &modelFeedback );
432 context.pushToThread( modelThread );
435 if ( feedback && !skipGenericLogging && modelThread != qApp->thread() )
436 feedback->
pushWarning( QObject::tr(
"Algorithm “%1” cannot be run in a background thread, switching to main thread for this step" ).arg( childAlg->displayName() ) );
438 context.pushToThread( qApp->thread() );
439 QMetaObject::invokeMethod( qApp, runOnMainThread, Qt::BlockingQueuedConnection );
444 results = childAlg->runPrepared( childParams, context, &modelFeedback );
453 Q_ASSERT_X( QThread::currentThread() == context.thread(),
"QgsProcessingModelAlgorithm::processAlgorithm",
"context was not transferred back to model thread" );
456 auto postProcessOnMainThread = [modelThread, &ppRes, &childAlg, &context, &modelFeedback]
458 Q_ASSERT_X( QThread::currentThread() == qApp->thread(),
"QgsProcessingModelAlgorithm::processAlgorithm",
"childAlg->postProcess() must be run on the main thread" );
459 ppRes = childAlg->postProcess( context, &modelFeedback );
460 context.pushToThread( modelThread );
464 if ( modelThread == qApp->thread() )
465 ppRes = childAlg->postProcess( context, &modelFeedback );
468 context.pushToThread( qApp->thread() );
469 QMetaObject::invokeMethod( qApp, postProcessOnMainThread, Qt::BlockingQueuedConnection );
472 Q_ASSERT_X( QThread::currentThread() == context.thread(),
"QgsProcessingModelAlgorithm::processAlgorithm",
"context was not transferred back to model thread" );
474 if ( !ppRes.isEmpty() )
477 childResults.insert( childId, results );
481 const QMap<QString, QgsProcessingModelOutput> outputs = child.modelOutputs();
482 for (
auto outputIt = outputs.constBegin(); outputIt != outputs.constEnd(); ++outputIt )
484 const int outputSortKey = mOutputOrder.indexOf( QStringLiteral(
"%1:%2" ).arg( childId, outputIt->childOutputName() ) );
485 switch ( mInternalVersion )
487 case QgsProcessingModelAlgorithm::InternalVersion::Version1:
488 finalResults.insert( childId +
':' + outputIt->name(), results.value( outputIt->childOutputName() ) );
490 case QgsProcessingModelAlgorithm::InternalVersion::Version2:
493 finalResults.insert( modelParam->name(), results.value( outputIt->childOutputName() ) );
498 if ( !results.value( outputIt->childOutputName() ).toString().isEmpty() )
502 if ( outputSortKey > 0 )
507 executed.insert( childId );
509 std::function< void(
const QString &,
const QString & )> pruneAlgorithmBranchRecursive;
510 pruneAlgorithmBranchRecursive = [&](
const QString & id,
const QString &branch = QString() )
512 const QSet<QString> toPrune = dependentChildAlgorithms(
id, branch );
513 for (
const QString &targetId : toPrune )
515 if ( executed.contains( targetId ) )
518 executed.insert( targetId );
519 pruneAlgorithmBranchRecursive( targetId, branch );
529 pruneAlgorithmBranchRecursive( childId, outputDef->name() );
537 for (
const QString &candidateId : std::as_const( toExecute ) )
539 if ( executed.contains( candidateId ) )
544 const QgsProcessingModelChildAlgorithm &candidate = mChildAlgorithms[ candidateId ];
545 const QMap<QString, QgsProcessingModelChildParameterSources> candidateParams = candidate.parameterSources();
546 QMap<QString, QgsProcessingModelChildParameterSources>::const_iterator paramIt = candidateParams.constBegin();
548 for ( ; paramIt != candidateParams.constEnd(); ++paramIt )
550 for (
const QgsProcessingModelChildParameterSource &source : paramIt.value() )
552 if ( source.source() == QgsProcessingModelChildParameterSource::ChildOutput && source.outputChildId() == childId )
555 if ( !results.contains( source.outputName() ) )
560 executed.insert( candidateId );
562 pruneAlgorithmBranchRecursive( candidateId, QString() );
573 childAlg.reset(
nullptr );
574 modelFeedback.setCurrentStep( executed.count() );
575 if ( feedback && !skipGenericLogging )
576 feedback->
pushInfo( QObject::tr(
"OK. Execution took %1 s (%n output(s)).",
nullptr, results.count() ).arg( childTime.elapsed() / 1000.0 ) );
583 feedback->
pushDebugInfo( QObject::tr(
"Model processed OK. Executed %n algorithm(s) total in %1 s.",
nullptr, executed.count() ).arg( totalTime.elapsed() / 1000.0 ) );
585 mResults = finalResults;
586 mResults.insert( QStringLiteral(
"CHILD_RESULTS" ), childResults );
587 mResults.insert( QStringLiteral(
"CHILD_INPUTS" ), childInputs );
591QString QgsProcessingModelAlgorithm::sourceFilePath()
const
596void QgsProcessingModelAlgorithm::setSourceFilePath(
const QString &sourceFile )
598 mSourceFile = sourceFile;
601bool QgsProcessingModelAlgorithm::modelNameMatchesFilePath()
const
603 if ( mSourceFile.isEmpty() )
606 const QFileInfo fi( mSourceFile );
607 return fi.completeBaseName().compare( mModelName, Qt::CaseInsensitive ) == 0;
612 QStringList fileDocString;
613 fileDocString << QStringLiteral(
"\"\"\"" );
614 fileDocString << QStringLiteral(
"Model exported as python." );
615 fileDocString << QStringLiteral(
"Name : %1" ).arg( displayName() );
616 fileDocString << QStringLiteral(
"Group : %1" ).arg( group() );
617 fileDocString << QStringLiteral(
"With QGIS : %1" ).arg(
Qgis::versionInt() );
618 fileDocString << QStringLiteral(
"\"\"\"" );
619 fileDocString << QString();
622 QString indent = QString(
' ' ).repeated( indentSize );
623 QString currentIndent;
625 QMap< QString, QString> friendlyChildNames;
626 QMap< QString, QString> friendlyOutputNames;
627 auto uniqueSafeName = [](
const QString & name,
bool capitalize,
const QMap< QString, QString > &friendlyNames )->QString
629 const QString base = safeName( name, capitalize );
630 QString candidate = base;
632 while ( friendlyNames.contains( candidate ) )
635 candidate = QStringLiteral(
"%1_%2" ).arg( base ).arg( i );
640 const QString algorithmClassName = safeName( name(),
true );
642 QSet< QString > toExecute;
643 for (
auto childIt = mChildAlgorithms.constBegin(); childIt != mChildAlgorithms.constEnd(); ++childIt )
645 if ( childIt->isActive() && childIt->algorithm() )
647 toExecute.insert( childIt->childId() );
648 friendlyChildNames.insert( childIt->childId(), uniqueSafeName( childIt->description().isEmpty() ? childIt->childId() : childIt->description(), !childIt->description().isEmpty(), friendlyChildNames ) );
651 const int totalSteps = toExecute.count();
653 QStringList importLines;
654 switch ( outputType )
659 const auto params = parameterDefinitions();
660 importLines.reserve( params.count() + 3 );
661 importLines << QStringLiteral(
"from qgis.core import QgsProcessing" );
662 importLines << QStringLiteral(
"from qgis.core import QgsProcessingAlgorithm" );
663 importLines << QStringLiteral(
"from qgis.core import QgsProcessingMultiStepFeedback" );
665 bool hasAdvancedParams =
false;
669 hasAdvancedParams =
true;
672 if ( !importString.isEmpty() && !importLines.contains( importString ) )
673 importLines << importString;
676 if ( hasAdvancedParams )
677 importLines << QStringLiteral(
"from qgis.core import QgsProcessingParameterDefinition" );
679 lines << QStringLiteral(
"import processing" );
680 lines << QString() << QString();
682 lines << QStringLiteral(
"class %1(QgsProcessingAlgorithm):" ).arg( algorithmClassName );
686 lines << indent + QStringLiteral(
"def initAlgorithm(self, config=None):" );
687 if ( params.empty() )
689 lines << indent + indent + QStringLiteral(
"pass" );
693 lines.reserve( lines.size() + params.size() );
696 std::unique_ptr< QgsProcessingParameterDefinition > defClone( def->clone() );
698 if ( defClone->isDestination() )
700 const QString uniqueChildName = defClone->metadata().value( QStringLiteral(
"_modelChildId" ) ).toString() +
':' + defClone->metadata().value( QStringLiteral(
"_modelChildOutputName" ) ).toString();
701 const QString friendlyName = !defClone->description().isEmpty() ? uniqueSafeName( defClone->description(),
true, friendlyOutputNames ) : defClone->name();
702 friendlyOutputNames.insert( uniqueChildName, friendlyName );
703 defClone->setName( friendlyName );
707 if ( !mParameterComponents.value( defClone->name() ).comment()->description().isEmpty() )
709 const QStringList parts = mParameterComponents.value( defClone->name() ).comment()->description().split( QStringLiteral(
"\n" ) );
710 for (
const QString &part : parts )
712 lines << indent + indent + QStringLiteral(
"# %1" ).arg( part );
719 lines << indent + indent + QStringLiteral(
"param = %1" ).arg( defClone->asPythonString() );
720 lines << indent + indent + QStringLiteral(
"param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)" );
721 lines << indent + indent + QStringLiteral(
"self.addParameter(param)" );
725 lines << indent + indent + QStringLiteral(
"self.addParameter(%1)" ).arg( defClone->asPythonString() );
731 lines << indent + QStringLiteral(
"def processAlgorithm(self, parameters, context, model_feedback):" );
732 currentIndent = indent + indent;
734 lines << currentIndent + QStringLiteral(
"# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the" );
735 lines << currentIndent + QStringLiteral(
"# overall progress through the model" );
736 lines << currentIndent + QStringLiteral(
"feedback = QgsProcessingMultiStepFeedback(%1, model_feedback)" ).arg( totalSteps );
744 QMap< QString, QgsProcessingModelParameter >::const_iterator paramIt = mParameterComponents.constBegin();
745 for ( ; paramIt != mParameterComponents.constEnd(); ++paramIt )
747 QString name = paramIt.value().parameterName();
748 if ( parameterDefinition( name ) )
751 params.insert( name, parameterDefinition( name )->valueAsPythonString( parameterDefinition( name )->defaultValue(), context ) );
755 if ( !params.isEmpty() )
757 lines << QStringLiteral(
"parameters = {" );
758 for (
auto it = params.constBegin(); it != params.constEnd(); ++it )
760 lines << QStringLiteral(
" '%1':%2," ).arg( it.key(), it.value() );
762 lines << QStringLiteral(
"}" )
766 lines << QStringLiteral(
"context = QgsProcessingContext()" )
767 << QStringLiteral(
"context.setProject(QgsProject.instance())" )
768 << QStringLiteral(
"feedback = QgsProcessingFeedback()" )
777 lines << currentIndent + QStringLiteral(
"results = {}" );
778 lines << currentIndent + QStringLiteral(
"outputs = {}" );
781 QSet< QString > executed;
782 bool executedAlg =
true;
784 while ( executedAlg && executed.count() < toExecute.count() )
787 const auto constToExecute = toExecute;
788 for (
const QString &childId : constToExecute )
790 if ( executed.contains( childId ) )
793 bool canExecute =
true;
794 const auto constDependsOnChildAlgorithms = dependsOnChildAlgorithms( childId );
795 for (
const QString &dependency : constDependsOnChildAlgorithms )
797 if ( !executed.contains( dependency ) )
809 const QgsProcessingModelChildAlgorithm &child = mChildAlgorithms[ childId ];
816 if ( def->isDestination() )
821 bool isFinalOutput =
false;
822 QMap<QString, QgsProcessingModelOutput> outputs = child.modelOutputs();
823 QMap<QString, QgsProcessingModelOutput>::const_iterator outputIt = outputs.constBegin();
824 for ( ; outputIt != outputs.constEnd(); ++outputIt )
826 if ( outputIt->childOutputName() == destParam->
name() )
828 QString paramName = child.childId() +
':' + outputIt.key();
829 paramName = friendlyOutputNames.value( paramName, paramName );
830 childParams.insert( destParam->
name(), QStringLiteral(
"parameters['%1']" ).arg( paramName ) );
831 isFinalOutput =
true;
836 if ( !isFinalOutput )
841 bool required =
true;
844 required = childOutputIsRequired( child.childId(), destParam->
name() );
850 childParams.insert( destParam->
name(), QStringLiteral(
"QgsProcessing.TEMPORARY_OUTPUT" ) );
856 lines << child.asPythonCode( outputType, childParams, currentIndent.size(), indentSize, friendlyChildNames, friendlyOutputNames );
858 if ( currentStep < totalSteps )
861 lines << currentIndent + QStringLiteral(
"feedback.setCurrentStep(%1)" ).arg( currentStep );
862 lines << currentIndent + QStringLiteral(
"if feedback.isCanceled():" );
863 lines << currentIndent + indent + QStringLiteral(
"return {}" );
866 executed.insert( childId );
870 switch ( outputType )
873 lines << currentIndent + QStringLiteral(
"return results" );
877 lines << indent + QStringLiteral(
"def name(self):" );
878 lines << indent + indent + QStringLiteral(
"return '%1'" ).arg( mModelName );
880 lines << indent + QStringLiteral(
"def displayName(self):" );
881 lines << indent + indent + QStringLiteral(
"return '%1'" ).arg( mModelName );
885 lines << indent + QStringLiteral(
"def group(self):" );
886 lines << indent + indent + QStringLiteral(
"return '%1'" ).arg( mModelGroup );
888 lines << indent + QStringLiteral(
"def groupId(self):" );
889 lines << indent + indent + QStringLiteral(
"return '%1'" ).arg( mModelGroupId );
893 if ( !shortHelpString().isEmpty() )
895 lines << indent + QStringLiteral(
"def shortHelpString(self):" );
896 lines << indent + indent + QStringLiteral(
"return \"\"\"%1\"\"\"" ).arg( shortHelpString() );
899 if ( !helpUrl().isEmpty() )
901 lines << indent + QStringLiteral(
"def helpUrl(self):" );
902 lines << indent + indent + QStringLiteral(
"return '%1'" ).arg( helpUrl() );
907 lines << indent + QStringLiteral(
"def createInstance(self):" );
908 lines << indent + indent + QStringLiteral(
"return %1()" ).arg( algorithmClassName );
911 static QMap< QString, QString > sAdditionalImports
913 { QStringLiteral(
"QgsCoordinateReferenceSystem" ), QStringLiteral(
"from qgis.core import QgsCoordinateReferenceSystem" ) },
914 { QStringLiteral(
"QgsExpression" ), QStringLiteral(
"from qgis.core import QgsExpression" ) },
915 { QStringLiteral(
"QgsRectangle" ), QStringLiteral(
"from qgis.core import QgsRectangle" ) },
916 { QStringLiteral(
"QgsReferencedRectangle" ), QStringLiteral(
"from qgis.core import QgsReferencedRectangle" ) },
917 { QStringLiteral(
"QgsPoint" ), QStringLiteral(
"from qgis.core import QgsPoint" ) },
918 { QStringLiteral(
"QgsReferencedPoint" ), QStringLiteral(
"from qgis.core import QgsReferencedPoint" ) },
919 { QStringLiteral(
"QgsProperty" ), QStringLiteral(
"from qgis.core import QgsProperty" ) },
920 { QStringLiteral(
"QgsRasterLayer" ), QStringLiteral(
"from qgis.core import QgsRasterLayer" ) },
921 { QStringLiteral(
"QgsMeshLayer" ), QStringLiteral(
"from qgis.core import QgsMeshLayer" ) },
922 { QStringLiteral(
"QgsVectorLayer" ), QStringLiteral(
"from qgis.core import QgsVectorLayer" ) },
923 { QStringLiteral(
"QgsMapLayer" ), QStringLiteral(
"from qgis.core import QgsMapLayer" ) },
924 { QStringLiteral(
"QgsProcessingFeatureSourceDefinition" ), QStringLiteral(
"from qgis.core import QgsProcessingFeatureSourceDefinition" ) },
925 { QStringLiteral(
"QgsPointXY" ), QStringLiteral(
"from qgis.core import QgsPointXY" ) },
926 { QStringLiteral(
"QgsReferencedPointXY" ), QStringLiteral(
"from qgis.core import QgsReferencedPointXY" ) },
927 { QStringLiteral(
"QgsGeometry" ), QStringLiteral(
"from qgis.core import QgsGeometry" ) },
928 { QStringLiteral(
"QgsProcessingOutputLayerDefinition" ), QStringLiteral(
"from qgis.core import QgsProcessingOutputLayerDefinition" ) },
929 { QStringLiteral(
"QColor" ), QStringLiteral(
"from qgis.PyQt.QtGui import QColor" ) },
930 { QStringLiteral(
"QDateTime" ), QStringLiteral(
"from qgis.PyQt.QtCore import QDateTime" ) },
931 { QStringLiteral(
"QDate" ), QStringLiteral(
"from qgis.PyQt.QtCore import QDate" ) },
932 { QStringLiteral(
"QTime" ), QStringLiteral(
"from qgis.PyQt.QtCore import QTime" ) },
935 for (
auto it = sAdditionalImports.constBegin(); it != sAdditionalImports.constEnd(); ++it )
937 if ( importLines.contains( it.value() ) )
944 for (
const QString &line : std::as_const( lines ) )
946 if ( line.contains( it.key() ) )
954 importLines << it.value();
958 lines = fileDocString + importLines + lines;
967QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> QgsProcessingModelAlgorithm::variablesForChildAlgorithm(
const QString &childId,
QgsProcessingContext &context,
const QVariantMap &modelParameters,
const QVariantMap &results )
const
969 QMap<QString, QgsProcessingModelAlgorithm::VariableDefinition> variables;
971 auto safeName = [](
const QString & name )->QString
974 const thread_local QRegularExpression safeNameRe( QStringLiteral(
"[\\s'\"\\(\\):\\.]" ) );
975 return s.replace( safeNameRe, QStringLiteral(
"_" ) );
1009 for (
const QgsProcessingModelChildParameterSource &source : std::as_const( sources ) )
1013 QString description;
1014 switch ( source.source() )
1016 case QgsProcessingModelChildParameterSource::ModelParameter:
1018 name = source.parameterName();
1019 value = modelParameters.value( source.parameterName() );
1020 description = parameterDefinition( source.parameterName() )->description();
1023 case QgsProcessingModelChildParameterSource::ChildOutput:
1025 const QgsProcessingModelChildAlgorithm &child = mChildAlgorithms.value( source.outputChildId() );
1026 name = QStringLiteral(
"%1_%2" ).arg( child.description().isEmpty() ?
1027 source.outputChildId() : child.description(), source.outputName() );
1030 description = QObject::tr(
"Output '%1' from algorithm '%2'" ).arg( alg->outputDefinition( source.outputName() )->description(),
1031 child.description() );
1033 value = results.value( source.outputChildId() ).toMap().value( source.outputName() );
1037 case QgsProcessingModelChildParameterSource::Expression:
1038 case QgsProcessingModelChildParameterSource::ExpressionText:
1039 case QgsProcessingModelChildParameterSource::StaticValue:
1040 case QgsProcessingModelChildParameterSource::ModelOutput:
1043 variables.insert( safeName( name ), VariableDefinition( value, source, description ) );
1047 sources = availableSourcesForChild( childId, QStringList()
1054 for (
const QgsProcessingModelChildParameterSource &source : std::as_const( sources ) )
1058 QString description;
1060 switch ( source.source() )
1062 case QgsProcessingModelChildParameterSource::ModelParameter:
1064 name = source.parameterName();
1065 value = modelParameters.value( source.parameterName() );
1066 description = parameterDefinition( source.parameterName() )->description();
1069 case QgsProcessingModelChildParameterSource::ChildOutput:
1071 const QgsProcessingModelChildAlgorithm &child = mChildAlgorithms.value( source.outputChildId() );
1072 name = QStringLiteral(
"%1_%2" ).arg( child.description().isEmpty() ?
1073 source.outputChildId() : child.description(), source.outputName() );
1074 value = results.value( source.outputChildId() ).toMap().value( source.outputName() );
1077 description = QObject::tr(
"Output '%1' from algorithm '%2'" ).arg( alg->outputDefinition( source.outputName() )->description(),
1078 child.description() );
1083 case QgsProcessingModelChildParameterSource::Expression:
1084 case QgsProcessingModelChildParameterSource::ExpressionText:
1085 case QgsProcessingModelChildParameterSource::StaticValue:
1086 case QgsProcessingModelChildParameterSource::ModelOutput:
1091 if ( value.userType() == QMetaType::type(
"QgsProcessingOutputLayerDefinition" ) )
1094 value = fromVar.
sink;
1095 if ( value.userType() == QMetaType::type(
"QgsProperty" ) )
1100 QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( value ) );
1104 variables.insert( safeName( name ), VariableDefinition( QVariant::fromValue(
QgsWeakMapLayerPointer( layer ) ), source, description ) );
1105 variables.insert( safeName( QStringLiteral(
"%1_minx" ).arg( name ) ), VariableDefinition( layer ? layer->
extent().
xMinimum() : QVariant(), source, QObject::tr(
"Minimum X of %1" ).arg( description ) ) );
1106 variables.insert( safeName( QStringLiteral(
"%1_miny" ).arg( name ) ), VariableDefinition( layer ? layer->
extent().
yMinimum() : QVariant(), source, QObject::tr(
"Minimum Y of %1" ).arg( description ) ) );
1107 variables.insert( safeName( QStringLiteral(
"%1_maxx" ).arg( name ) ), VariableDefinition( layer ? layer->
extent().
xMaximum() : QVariant(), source, QObject::tr(
"Maximum X of %1" ).arg( description ) ) );
1108 variables.insert( safeName( QStringLiteral(
"%1_maxy" ).arg( name ) ), VariableDefinition( layer ? layer->
extent().
yMaximum() : QVariant(), source, QObject::tr(
"Maximum Y of %1" ).arg( description ) ) );
1111 sources = availableSourcesForChild( childId, QStringList()
1113 for (
const QgsProcessingModelChildParameterSource &source : std::as_const( sources ) )
1117 QString description;
1119 switch ( source.source() )
1121 case QgsProcessingModelChildParameterSource::ModelParameter:
1123 name = source.parameterName();
1124 value = modelParameters.value( source.parameterName() );
1125 description = parameterDefinition( source.parameterName() )->description();
1128 case QgsProcessingModelChildParameterSource::ChildOutput:
1130 const QgsProcessingModelChildAlgorithm &child = mChildAlgorithms.value( source.outputChildId() );
1131 name = QStringLiteral(
"%1_%2" ).arg( child.description().isEmpty() ?
1132 source.outputChildId() : child.description(), source.outputName() );
1133 value = results.value( source.outputChildId() ).toMap().value( source.outputName() );
1136 description = QObject::tr(
"Output '%1' from algorithm '%2'" ).arg( alg->outputDefinition( source.outputName() )->description(),
1137 child.description() );
1142 case QgsProcessingModelChildParameterSource::Expression:
1143 case QgsProcessingModelChildParameterSource::ExpressionText:
1144 case QgsProcessingModelChildParameterSource::StaticValue:
1145 case QgsProcessingModelChildParameterSource::ModelOutput:
1151 if ( value.userType() == QMetaType::type(
"QgsProcessingFeatureSourceDefinition" ) )
1156 else if ( value.userType() == QMetaType::type(
"QgsProcessingOutputLayerDefinition" ) )
1159 value = fromVar.
sink;
1160 if ( value.userType() == QMetaType::type(
"QgsProperty" ) )
1165 if (
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( value ) ) )
1167 featureSource = layer;
1169 if ( !featureSource )
1175 variables.insert( safeName( name ), VariableDefinition( value, source, description ) );
1176 variables.insert( safeName( QStringLiteral(
"%1_minx" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->
sourceExtent().
xMinimum() : QVariant(), source, QObject::tr(
"Minimum X of %1" ).arg( description ) ) );
1177 variables.insert( safeName( QStringLiteral(
"%1_miny" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->
sourceExtent().
yMinimum() : QVariant(), source, QObject::tr(
"Minimum Y of %1" ).arg( description ) ) );
1178 variables.insert( safeName( QStringLiteral(
"%1_maxx" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->
sourceExtent().
xMaximum() : QVariant(), source, QObject::tr(
"Maximum X of %1" ).arg( description ) ) );
1179 variables.insert( safeName( QStringLiteral(
"%1_maxy" ).arg( name ) ), VariableDefinition( featureSource ? featureSource->
sourceExtent().
yMaximum() : QVariant(), source, QObject::tr(
"Maximum Y of %1" ).arg( description ) ) );
1185QgsExpressionContextScope *QgsProcessingModelAlgorithm::createExpressionContextScopeForChildAlgorithm(
const QString &childId,
QgsProcessingContext &context,
const QVariantMap &modelParameters,
const QVariantMap &results )
const
1187 std::unique_ptr< QgsExpressionContextScope > scope(
new QgsExpressionContextScope( QStringLiteral(
"algorithm_inputs" ) ) );
1188 QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition> variables = variablesForChildAlgorithm( childId, context, modelParameters, results );
1189 QMap< QString, QgsProcessingModelAlgorithm::VariableDefinition>::const_iterator varIt = variables.constBegin();
1190 for ( ; varIt != variables.constEnd(); ++varIt )
1194 return scope.release();
1197QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSourcesForChild(
const QString &childId,
const QStringList ¶meterTypes,
const QStringList &outputTypes,
const QList<int> &dataTypes )
const
1199 QgsProcessingModelChildParameterSources sources;
1202 QMap< QString, QgsProcessingModelParameter >::const_iterator paramIt = mParameterComponents.constBegin();
1203 for ( ; paramIt != mParameterComponents.constEnd(); ++paramIt )
1209 if ( parameterTypes.contains( def->
type() ) )
1211 if ( !dataTypes.isEmpty() )
1227 bool ok = sourceDef->
dataTypes().isEmpty();
1228 const auto constDataTypes = sourceDef->
dataTypes();
1229 for (
int type : constDataTypes )
1244 sources << QgsProcessingModelChildParameterSource::fromModelParameter( paramIt->parameterName() );
1248 QSet< QString > dependents;
1249 if ( !childId.isEmpty() )
1251 dependents = dependentChildAlgorithms( childId );
1252 dependents << childId;
1255 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
1256 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
1258 if ( dependents.contains( childIt->childId() ) )
1268 if ( outputTypes.contains( out->type() ) )
1270 if ( !dataTypes.isEmpty() )
1276 if ( !vectorOutputIsCompatibleType( dataTypes, vectorOut->
dataType() ) )
1283 sources << QgsProcessingModelChildParameterSource::fromChildOutput( childIt->childId(), out->name() );
1291QVariantMap QgsProcessingModelAlgorithm::helpContent()
const
1293 return mHelpContent;
1296void QgsProcessingModelAlgorithm::setHelpContent(
const QVariantMap &helpContent )
1298 mHelpContent = helpContent;
1301void QgsProcessingModelAlgorithm::setName(
const QString &name )
1306void QgsProcessingModelAlgorithm::setGroup(
const QString &group )
1308 mModelGroup = group;
1311bool QgsProcessingModelAlgorithm::validate( QStringList &issues )
const
1316 if ( mChildAlgorithms.empty() )
1319 issues << QObject::tr(
"Model does not contain any algorithms" );
1322 for (
auto it = mChildAlgorithms.constBegin(); it != mChildAlgorithms.constEnd(); ++it )
1324 QStringList childIssues;
1325 res = validateChildAlgorithm( it->childId(), childIssues ) && res;
1327 for (
const QString &issue : std::as_const( childIssues ) )
1329 issues << QStringLiteral(
"<b>%1</b>: %2" ).arg( it->description(), issue );
1335QMap<QString, QgsProcessingModelChildAlgorithm> QgsProcessingModelAlgorithm::childAlgorithms()
const
1337 return mChildAlgorithms;
1340void QgsProcessingModelAlgorithm::setParameterComponents(
const QMap<QString, QgsProcessingModelParameter> ¶meterComponents )
1342 mParameterComponents = parameterComponents;
1345void QgsProcessingModelAlgorithm::setParameterComponent(
const QgsProcessingModelParameter &component )
1347 mParameterComponents.insert( component.parameterName(), component );
1350QgsProcessingModelParameter &QgsProcessingModelAlgorithm::parameterComponent(
const QString &name )
1352 if ( !mParameterComponents.contains( name ) )
1354 QgsProcessingModelParameter &component = mParameterComponents[ name ];
1355 component.setParameterName( name );
1358 return mParameterComponents[ name ];
1361QList< QgsProcessingModelParameter > QgsProcessingModelAlgorithm::orderedParameters()
const
1363 QList< QgsProcessingModelParameter > res;
1364 QSet< QString > found;
1365 for (
const QString ¶meter : mParameterOrder )
1367 if ( mParameterComponents.contains( parameter ) )
1369 res << mParameterComponents.value( parameter );
1375 for (
auto it = mParameterComponents.constBegin(); it != mParameterComponents.constEnd(); ++it )
1377 if ( !found.contains( it.key() ) )
1385void QgsProcessingModelAlgorithm::setParameterOrder(
const QStringList &order )
1387 mParameterOrder = order;
1390QList<QgsProcessingModelOutput> QgsProcessingModelAlgorithm::orderedOutputs()
const
1392 QList< QgsProcessingModelOutput > res;
1393 QSet< QString > found;
1395 for (
const QString &output : mOutputOrder )
1397 bool foundOutput =
false;
1398 for (
auto it = mChildAlgorithms.constBegin(); it != mChildAlgorithms.constEnd(); ++it )
1400 const QMap<QString, QgsProcessingModelOutput> outputs = it.value().modelOutputs();
1401 for (
auto outputIt = outputs.constBegin(); outputIt != outputs.constEnd(); ++outputIt )
1403 if ( output == QStringLiteral(
"%1:%2" ).arg( outputIt->childId(), outputIt->childOutputName() ) )
1405 res << outputIt.value();
1407 found.insert( QStringLiteral(
"%1:%2" ).arg( outputIt->childId(), outputIt->childOutputName() ) );
1416 for (
auto it = mChildAlgorithms.constBegin(); it != mChildAlgorithms.constEnd(); ++it )
1418 const QMap<QString, QgsProcessingModelOutput> outputs = it.value().modelOutputs();
1419 for (
auto outputIt = outputs.constBegin(); outputIt != outputs.constEnd(); ++outputIt )
1421 if ( !found.contains( QStringLiteral(
"%1:%2" ).arg( outputIt->childId(), outputIt->childOutputName() ) ) )
1423 res << outputIt.value();
1431void QgsProcessingModelAlgorithm::setOutputOrder(
const QStringList &order )
1433 mOutputOrder = order;
1436QString QgsProcessingModelAlgorithm::outputGroup()
const
1438 return mOutputGroup;
1441void QgsProcessingModelAlgorithm::setOutputGroup(
const QString &group )
1443 mOutputGroup = group;
1446void QgsProcessingModelAlgorithm::updateDestinationParameters()
1449 QMutableListIterator<const QgsProcessingParameterDefinition *> it( mParameters );
1450 while ( it.hasNext() )
1460 qDeleteAll( mOutputs );
1464 QSet< QString > usedFriendlyNames;
1465 auto uniqueSafeName = [&usedFriendlyNames ](
const QString & name )->QString
1467 const QString base = safeName( name,
false );
1468 QString candidate = base;
1470 while ( usedFriendlyNames.contains( candidate ) )
1473 candidate = QStringLiteral(
"%1_%2" ).arg( base ).arg( i );
1475 usedFriendlyNames.insert( candidate );
1479 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
1480 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
1482 QMap<QString, QgsProcessingModelOutput> outputs = childIt->modelOutputs();
1483 QMap<QString, QgsProcessingModelOutput>::const_iterator outputIt = outputs.constBegin();
1484 for ( ; outputIt != outputs.constEnd(); ++outputIt )
1486 if ( !childIt->isActive() || !childIt->algorithm() )
1494 std::unique_ptr< QgsProcessingParameterDefinition > param( source->
clone() );
1498 if ( outputIt->isMandatory() )
1500 if ( mInternalVersion != InternalVersion::Version1 && !outputIt->description().isEmpty() )
1502 QString friendlyName = uniqueSafeName( outputIt->description() );
1503 param->setName( friendlyName );
1507 param->setName( outputIt->childId() +
':' + outputIt->name() );
1510 param->metadata().insert( QStringLiteral(
"_modelChildId" ), outputIt->childId() );
1511 param->metadata().insert( QStringLiteral(
"_modelChildOutputName" ), outputIt->name() );
1512 param->metadata().insert( QStringLiteral(
"_modelChildProvider" ), childIt->algorithm()->provider() ? childIt->algorithm()->provider()->id() : QString() );
1514 param->setDescription( outputIt->description() );
1515 param->setDefaultValue( outputIt->defaultValue() );
1518 if ( addParameter( param.release() ) && newDestParam )
1525 newDestParam->mOriginalProvider = provider;
1532void QgsProcessingModelAlgorithm::addGroupBox(
const QgsProcessingModelGroupBox &groupBox )
1534 mGroupBoxes.insert( groupBox.uuid(), groupBox );
1537QList<QgsProcessingModelGroupBox> QgsProcessingModelAlgorithm::groupBoxes()
const
1539 return mGroupBoxes.values();
1542void QgsProcessingModelAlgorithm::removeGroupBox(
const QString &uuid )
1544 mGroupBoxes.remove( uuid );
1547QVariant QgsProcessingModelAlgorithm::toVariant()
const
1550 map.insert( QStringLiteral(
"model_name" ), mModelName );
1551 map.insert( QStringLiteral(
"model_group" ), mModelGroup );
1552 map.insert( QStringLiteral(
"help" ), mHelpContent );
1553 map.insert( QStringLiteral(
"internal_version" ),
qgsEnumValueToKey( mInternalVersion ) );
1555 QVariantMap childMap;
1556 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
1557 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
1559 childMap.insert( childIt.key(), childIt.value().toVariant() );
1561 map.insert( QStringLiteral(
"children" ), childMap );
1563 QVariantMap paramMap;
1564 QMap< QString, QgsProcessingModelParameter >::const_iterator paramIt = mParameterComponents.constBegin();
1565 for ( ; paramIt != mParameterComponents.constEnd(); ++paramIt )
1567 paramMap.insert( paramIt.key(), paramIt.value().toVariant() );
1569 map.insert( QStringLiteral(
"parameters" ), paramMap );
1571 QVariantMap paramDefMap;
1576 map.insert( QStringLiteral(
"parameterDefinitions" ), paramDefMap );
1578 QVariantList groupBoxDefs;
1579 for (
auto it = mGroupBoxes.constBegin(); it != mGroupBoxes.constEnd(); ++it )
1581 groupBoxDefs.append( it.value().toVariant() );
1583 map.insert( QStringLiteral(
"groupBoxes" ), groupBoxDefs );
1585 map.insert( QStringLiteral(
"modelVariables" ), mVariables );
1587 map.insert( QStringLiteral(
"designerParameterValues" ), mDesignerParameterValues );
1589 map.insert( QStringLiteral(
"parameterOrder" ), mParameterOrder );
1590 map.insert( QStringLiteral(
"outputOrder" ), mOutputOrder );
1591 map.insert( QStringLiteral(
"outputGroup" ), mOutputGroup );
1596bool QgsProcessingModelAlgorithm::loadVariant(
const QVariant &model )
1598 QVariantMap map = model.toMap();
1600 mModelName = map.value( QStringLiteral(
"model_name" ) ).toString();
1601 mModelGroup = map.value( QStringLiteral(
"model_group" ) ).toString();
1602 mModelGroupId = map.value( QStringLiteral(
"model_group" ) ).toString();
1603 mHelpContent = map.value( QStringLiteral(
"help" ) ).toMap();
1605 mInternalVersion =
qgsEnumKeyToValue( map.value( QStringLiteral(
"internal_version" ) ).toString(), InternalVersion::Version1 );
1607 mVariables = map.value( QStringLiteral(
"modelVariables" ) ).toMap();
1608 mDesignerParameterValues = map.value( QStringLiteral(
"designerParameterValues" ) ).toMap();
1610 mParameterOrder = map.value( QStringLiteral(
"parameterOrder" ) ).toStringList();
1611 mOutputOrder = map.value( QStringLiteral(
"outputOrder" ) ).toStringList();
1612 mOutputGroup = map.value( QStringLiteral(
"outputGroup" ) ).toString();
1614 mChildAlgorithms.clear();
1615 QVariantMap childMap = map.value( QStringLiteral(
"children" ) ).toMap();
1616 QVariantMap::const_iterator childIt = childMap.constBegin();
1617 for ( ; childIt != childMap.constEnd(); ++childIt )
1619 QgsProcessingModelChildAlgorithm child;
1623 if ( !child.loadVariant( childIt.value() ) )
1626 mChildAlgorithms.insert( child.childId(), child );
1629 mParameterComponents.clear();
1630 QVariantMap paramMap = map.value( QStringLiteral(
"parameters" ) ).toMap();
1631 QVariantMap::const_iterator paramIt = paramMap.constBegin();
1632 for ( ; paramIt != paramMap.constEnd(); ++paramIt )
1634 QgsProcessingModelParameter param;
1635 if ( !param.loadVariant( paramIt.value().toMap() ) )
1638 mParameterComponents.insert( param.parameterName(), param );
1641 qDeleteAll( mParameters );
1642 mParameters.clear();
1643 QVariantMap paramDefMap = map.value( QStringLiteral(
"parameterDefinitions" ) ).toMap();
1645 auto addParam = [ = ](
const QVariant & value )
1653 if ( param->name() == QLatin1String(
"VERBOSE_LOG" ) )
1657 param->setHelp( mHelpContent.value( param->name() ).toString() );
1660 addParameter( param.release() );
1664 QVariantMap map = value.toMap();
1665 QString type = map.value( QStringLiteral(
"parameter_type" ) ).toString();
1666 QString name = map.value( QStringLiteral(
"name" ) ).toString();
1668 QgsMessageLog::logMessage( QCoreApplication::translate(
"Processing",
"Could not load parameter %1 of type %2." ).arg( name, type ), QCoreApplication::translate(
"Processing",
"Processing" ) );
1672 QSet< QString > loadedParams;
1674 for (
const QString &name : std::as_const( mParameterOrder ) )
1676 if ( paramDefMap.contains( name ) )
1678 addParam( paramDefMap.value( name ) );
1679 loadedParams << name;
1683 QVariantMap::const_iterator paramDefIt = paramDefMap.constBegin();
1684 for ( ; paramDefIt != paramDefMap.constEnd(); ++paramDefIt )
1686 if ( !loadedParams.contains( paramDefIt.key() ) )
1687 addParam( paramDefIt.value() );
1690 mGroupBoxes.clear();
1691 const QVariantList groupBoxList = map.value( QStringLiteral(
"groupBoxes" ) ).toList();
1692 for (
const QVariant &groupBoxDef : groupBoxList )
1694 QgsProcessingModelGroupBox groupBox;
1695 groupBox.loadVariant( groupBoxDef.toMap() );
1696 mGroupBoxes.insert( groupBox.uuid(), groupBox );
1699 updateDestinationParameters();
1704bool QgsProcessingModelAlgorithm::vectorOutputIsCompatibleType(
const QList<int> &acceptableDataTypes,
QgsProcessing::SourceType outputType )
1709 return ( acceptableDataTypes.empty()
1710 || acceptableDataTypes.contains( outputType )
1721void QgsProcessingModelAlgorithm::reattachAlgorithms()
const
1723 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
1724 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
1726 if ( !childIt->algorithm() )
1727 childIt->reattach();
1731bool QgsProcessingModelAlgorithm::toFile(
const QString &path )
const
1733 QDomDocument doc = QDomDocument( QStringLiteral(
"model" ) );
1735 doc.appendChild( elem );
1738 if ( file.open( QFile::WriteOnly | QFile::Truncate ) )
1740 QTextStream stream( &file );
1741 doc.save( stream, 2 );
1748bool QgsProcessingModelAlgorithm::fromFile(
const QString &path )
1753 if ( file.open( QFile::ReadOnly ) )
1755 if ( !doc.setContent( &file ) )
1766 return loadVariant( props );
1769void QgsProcessingModelAlgorithm::setChildAlgorithms(
const QMap<QString, QgsProcessingModelChildAlgorithm> &childAlgorithms )
1771 mChildAlgorithms = childAlgorithms;
1772 updateDestinationParameters();
1775void QgsProcessingModelAlgorithm::setChildAlgorithm(
const QgsProcessingModelChildAlgorithm &
algorithm )
1778 updateDestinationParameters();
1781QString QgsProcessingModelAlgorithm::addChildAlgorithm( QgsProcessingModelChildAlgorithm &
algorithm )
1783 if (
algorithm.childId().isEmpty() || mChildAlgorithms.contains(
algorithm.childId() ) )
1787 updateDestinationParameters();
1791QgsProcessingModelChildAlgorithm &QgsProcessingModelAlgorithm::childAlgorithm(
const QString &childId )
1793 return mChildAlgorithms[ childId ];
1796bool QgsProcessingModelAlgorithm::removeChildAlgorithm(
const QString &
id )
1798 if ( !dependentChildAlgorithms(
id ).isEmpty() )
1801 mChildAlgorithms.remove(
id );
1802 updateDestinationParameters();
1806void QgsProcessingModelAlgorithm::deactivateChildAlgorithm(
const QString &
id )
1808 const auto constDependentChildAlgorithms = dependentChildAlgorithms(
id );
1809 for (
const QString &child : constDependentChildAlgorithms )
1811 childAlgorithm( child ).setActive(
false );
1813 childAlgorithm(
id ).setActive(
false );
1814 updateDestinationParameters();
1817bool QgsProcessingModelAlgorithm::activateChildAlgorithm(
const QString &
id )
1819 const auto constDependsOnChildAlgorithms = dependsOnChildAlgorithms(
id );
1820 for (
const QString &child : constDependsOnChildAlgorithms )
1822 if ( !childAlgorithm( child ).isActive() )
1825 childAlgorithm(
id ).setActive(
true );
1826 updateDestinationParameters();
1832 if ( addParameter( definition ) )
1833 mParameterComponents.insert( definition->
name(), component );
1838 removeParameter( definition->
name() );
1839 addParameter( definition );
1842void QgsProcessingModelAlgorithm::removeModelParameter(
const QString &name )
1844 removeParameter( name );
1845 mParameterComponents.remove( name );
1848void QgsProcessingModelAlgorithm::changeParameterName(
const QString &oldName,
const QString &newName )
1853 auto replaceExpressionVariable = [oldName, newName, &expressionContext](
const QString & expressionString ) -> std::tuple< bool, QString >
1856 expression.prepare( &expressionContext );
1857 QSet<QString> variables = expression.referencedVariables();
1858 if ( variables.contains( oldName ) )
1860 QString newExpression = expressionString;
1861 newExpression.replace( QStringLiteral(
"@%1" ).arg( oldName ), QStringLiteral(
"@%2" ).arg( newName ) );
1862 return {
true, newExpression };
1864 return {
false, QString() };
1867 QMap< QString, QgsProcessingModelChildAlgorithm >::iterator childIt = mChildAlgorithms.begin();
1868 for ( ; childIt != mChildAlgorithms.end(); ++childIt )
1870 bool changed =
false;
1871 QMap<QString, QgsProcessingModelChildParameterSources> childParams = childIt->parameterSources();
1872 QMap<QString, QgsProcessingModelChildParameterSources>::iterator paramIt = childParams.begin();
1873 for ( ; paramIt != childParams.end(); ++paramIt )
1875 QList< QgsProcessingModelChildParameterSource > &value = paramIt.value();
1876 for (
auto valueIt = value.begin(); valueIt != value.end(); ++valueIt )
1878 switch ( valueIt->source() )
1880 case QgsProcessingModelChildParameterSource::ModelParameter:
1882 if ( valueIt->parameterName() == oldName )
1884 valueIt->setParameterName( newName );
1890 case QgsProcessingModelChildParameterSource::Expression:
1892 bool updatedExpression =
false;
1893 QString newExpression;
1894 std::tie( updatedExpression, newExpression ) = replaceExpressionVariable( valueIt->expression() );
1895 if ( updatedExpression )
1897 valueIt->setExpression( newExpression );
1903 case QgsProcessingModelChildParameterSource::StaticValue:
1905 if ( valueIt->staticValue().userType() == QMetaType::type(
"QgsProperty" ) )
1910 bool updatedExpression =
false;
1911 QString newExpression;
1912 std::tie( updatedExpression, newExpression ) = replaceExpressionVariable( property.expressionString() );
1913 if ( updatedExpression )
1915 property.setExpressionString( newExpression );
1916 valueIt->setStaticValue( property );
1924 case QgsProcessingModelChildParameterSource::ChildOutput:
1925 case QgsProcessingModelChildParameterSource::ExpressionText:
1926 case QgsProcessingModelChildParameterSource::ModelOutput:
1932 childIt->setParameterSources( childParams );
1936bool QgsProcessingModelAlgorithm::childAlgorithmsDependOnParameter(
const QString &name )
const
1938 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
1939 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
1942 QMap<QString, QgsProcessingModelChildParameterSources> childParams = childIt->parameterSources();
1943 QMap<QString, QgsProcessingModelChildParameterSources>::const_iterator paramIt = childParams.constBegin();
1944 for ( ; paramIt != childParams.constEnd(); ++paramIt )
1946 const auto constValue = paramIt.value();
1947 for (
const QgsProcessingModelChildParameterSource &source : constValue )
1949 if ( source.source() == QgsProcessingModelChildParameterSource::ModelParameter
1950 && source.parameterName() == name )
1960bool QgsProcessingModelAlgorithm::otherParametersDependOnParameter(
const QString &name )
const
1962 const auto constMParameters = mParameters;
1965 if ( def->
name() == name )
1974QMap<QString, QgsProcessingModelParameter> QgsProcessingModelAlgorithm::parameterComponents()
const
1976 return mParameterComponents;
1979void QgsProcessingModelAlgorithm::dependentChildAlgorithmsRecursive(
const QString &childId, QSet<QString> &depends,
const QString &branch )
const
1981 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
1982 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
1984 if ( depends.contains( childIt->childId() ) )
1988 const QList< QgsProcessingModelChildDependency > constDependencies = childIt->dependencies();
1989 bool hasDependency =
false;
1990 for (
const QgsProcessingModelChildDependency &dep : constDependencies )
1992 if ( dep.childId == childId && ( branch.isEmpty() || dep.conditionalBranch == branch ) )
1994 hasDependency =
true;
1999 if ( hasDependency )
2001 depends.insert( childIt->childId() );
2002 dependentChildAlgorithmsRecursive( childIt->childId(), depends, branch );
2007 QMap<QString, QgsProcessingModelChildParameterSources> childParams = childIt->parameterSources();
2008 QMap<QString, QgsProcessingModelChildParameterSources>::const_iterator paramIt = childParams.constBegin();
2009 for ( ; paramIt != childParams.constEnd(); ++paramIt )
2011 const auto constValue = paramIt.value();
2012 for (
const QgsProcessingModelChildParameterSource &source : constValue )
2014 if ( source.source() == QgsProcessingModelChildParameterSource::ChildOutput
2015 && source.outputChildId() == childId )
2017 depends.insert( childIt->childId() );
2018 dependentChildAlgorithmsRecursive( childIt->childId(), depends, branch );
2026QSet<QString> QgsProcessingModelAlgorithm::dependentChildAlgorithms(
const QString &childId,
const QString &conditionalBranch )
const
2028 QSet< QString > algs;
2032 algs.insert( childId );
2034 dependentChildAlgorithmsRecursive( childId, algs, conditionalBranch );
2037 algs.remove( childId );
2043void QgsProcessingModelAlgorithm::dependsOnChildAlgorithmsRecursive(
const QString &childId, QSet< QString > &depends )
const
2045 const QgsProcessingModelChildAlgorithm &alg = mChildAlgorithms.value( childId );
2048 const QList< QgsProcessingModelChildDependency > constDependencies = alg.dependencies();
2049 for (
const QgsProcessingModelChildDependency &val : constDependencies )
2051 if ( !depends.contains( val.childId ) )
2053 depends.insert( val.childId );
2054 dependsOnChildAlgorithmsRecursive( val.childId, depends );
2059 QMap<QString, QgsProcessingModelChildParameterSources> childParams = alg.parameterSources();
2060 QMap<QString, QgsProcessingModelChildParameterSources>::const_iterator paramIt = childParams.constBegin();
2061 for ( ; paramIt != childParams.constEnd(); ++paramIt )
2063 const auto constValue = paramIt.value();
2064 for (
const QgsProcessingModelChildParameterSource &source : constValue )
2066 if ( source.source() == QgsProcessingModelChildParameterSource::ChildOutput && !depends.contains( source.outputChildId() ) )
2068 depends.insert( source.outputChildId() );
2069 dependsOnChildAlgorithmsRecursive( source.outputChildId(), depends );
2075QSet< QString > QgsProcessingModelAlgorithm::dependsOnChildAlgorithms(
const QString &childId )
const
2077 QSet< QString > algs;
2081 algs.insert( childId );
2083 dependsOnChildAlgorithmsRecursive( childId, algs );
2086 algs.remove( childId );
2091QList<QgsProcessingModelChildDependency> QgsProcessingModelAlgorithm::availableDependenciesForChildAlgorithm(
const QString &childId )
const
2093 QSet< QString > dependent;
2094 if ( !childId.isEmpty() )
2096 dependent.unite( dependentChildAlgorithms( childId ) );
2097 dependent.insert( childId );
2100 QList<QgsProcessingModelChildDependency> res;
2101 for (
auto it = mChildAlgorithms.constBegin(); it != mChildAlgorithms.constEnd(); ++it )
2103 if ( !dependent.contains( it->childId() ) )
2106 bool hasBranches =
false;
2107 if ( it->algorithm() )
2115 QgsProcessingModelChildDependency alg;
2116 alg.childId = it->childId();
2117 alg.conditionalBranch = def->
name();
2125 QgsProcessingModelChildDependency alg;
2126 alg.childId = it->childId();
2134bool QgsProcessingModelAlgorithm::validateChildAlgorithm(
const QString &childId, QStringList &issues )
const
2137 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constFind( childId );
2138 if ( childIt != mChildAlgorithms.constEnd() )
2140 if ( !childIt->algorithm() )
2142 issues << QObject::tr(
"Algorithm is not available: <i>%1</i>" ).arg( childIt->algorithmId() );
2151 if ( childIt->parameterSources().contains( def->
name() ) )
2154 const QList< QgsProcessingModelChildParameterSource > sources = childIt->parameterSources().value( def->
name() );
2155 for (
const QgsProcessingModelChildParameterSource &source : sources )
2157 switch ( source.source() )
2159 case QgsProcessingModelChildParameterSource::StaticValue:
2163 issues << QObject::tr(
"Value for <i>%1</i> is not acceptable for this parameter" ).arg( def->
name() );
2167 case QgsProcessingModelChildParameterSource::ModelParameter:
2168 if ( !parameterComponents().contains( source.parameterName() ) )
2171 issues << QObject::tr(
"Model input <i>%1</i> used for parameter <i>%2</i> does not exist" ).arg( source.parameterName(), def->
name() );
2175 case QgsProcessingModelChildParameterSource::ChildOutput:
2176 if ( !childAlgorithms().contains( source.outputChildId() ) )
2179 issues << QObject::tr(
"Child algorithm <i>%1</i> used for parameter <i>%2</i> does not exist" ).arg( source.outputChildId(), def->
name() );
2183 case QgsProcessingModelChildParameterSource::Expression:
2184 case QgsProcessingModelChildParameterSource::ExpressionText:
2185 case QgsProcessingModelChildParameterSource::ModelOutput:
2201 issues << QObject::tr(
"Parameter <i>%1</i> is mandatory" ).arg( def->
name() );
2210 issues << QObject::tr(
"Invalid child ID: <i>%1</i>" ).arg( childId );
2215bool QgsProcessingModelAlgorithm::canExecute( QString *errorMessage )
const
2217 reattachAlgorithms();
2218 QMap< QString, QgsProcessingModelChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
2219 for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
2221 if ( !childIt->algorithm() )
2225 *errorMessage = QObject::tr(
"The model you are trying to run contains an algorithm that is not available: <i>%1</i>" ).arg( childIt->algorithmId() );
2233QString QgsProcessingModelAlgorithm::asPythonCommand(
const QVariantMap ¶meters,
QgsProcessingContext &context )
const
2235 if ( mSourceFile.isEmpty() )
2250 QgsProcessingModelAlgorithm *alg =
new QgsProcessingModelAlgorithm();
2251 alg->loadVariant( toVariant() );
2252 alg->setProvider( provider() );
2253 alg->setSourceFilePath( sourceFilePath() );
2257QString QgsProcessingModelAlgorithm::safeName(
const QString &name,
bool capitalize )
2259 QString n = name.toLower().trimmed();
2260 const thread_local QRegularExpression rx( QStringLiteral(
"[^\\sa-z_A-Z0-9]" ) );
2261 n.replace( rx, QString() );
2262 const thread_local QRegularExpression rx2( QStringLiteral(
"^\\d*" ) );
2263 n.replace( rx2, QString() );
2265 n = n.replace(
' ',
'_' );
2269QVariantMap QgsProcessingModelAlgorithm::variables()
const
2274void QgsProcessingModelAlgorithm::setVariables(
const QVariantMap &variables )
2276 mVariables = variables;
2279QVariantMap QgsProcessingModelAlgorithm::designerParameterValues()
const
2281 return mDesignerParameterValues;
@ UpperCamelCase
Convert the string to upper camel case. Note that this method does not unaccent characters.
static int versionInt()
Version number used for comparing versions using the "Check QGIS Version" function.
static QgsProcessingRegistry * processingRegistry()
Returns the application's processing registry, used for managing processing providers,...
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QgsExpressionContextScope * processingModelAlgorithmScope(const QgsProcessingModelAlgorithm *model, const QVariantMap ¶meters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing model algorithm,...
static QgsExpressionContextScope * processingAlgorithmScope(const QgsProcessingAlgorithm *algorithm, const QVariantMap ¶meters, QgsProcessingContext &context)
Creates a new scope which contains variables and functions relating to a processing algorithm,...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
An interface for objects which provide features via a getFeatures method.
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Base class for all map layer types.
virtual QgsRectangle extent() const
Returns the extent of the layer.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Abstract base class for processing algorithms.
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
@ FlagNoThreading
Algorithm is not thread safe and cannot be run in a background thread, e.g. for algorithms which mani...
@ FlagCustomException
Algorithm raises custom exception notices, don't use the standard ones.
@ FlagSkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
@ FlagPruneModelBranchesBasedOnAlgorithmResults
Algorithm results will cause remaining model branches to be pruned based on the results of running th...
virtual QgsExpressionContext createExpressionContext(const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeatureSource *source=nullptr) const
Creates an expression context relating to the algorithm.
const QgsProcessingParameterDefinition * parameterDefinition(const QString &name) const
Returns a matching parameter by name.
virtual QString asPythonCommand(const QVariantMap ¶meters, QgsProcessingContext &context) const
Returns a Python command string which can be executed to run the algorithm using the specified parame...
QgsProcessingProvider * provider() const
Returns the provider to which this algorithm belongs.
Details for layers to load into projects.
int layerSortKey
Optional sorting key for sorting output layers when loading them into a project.
QString groupName
Optional name for a layer tree group under which to place the layer when loading it into a project.
Contains information about the context in which a processing algorithm is executed.
@ Verbose
Verbose logging.
QgsExpressionContext & expressionContext()
Returns the expression context.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
LogLevel logLevel() const
Returns the logging level for algorithms to use when pushing feedback messages to users.
Base class for all parameter definitions which represent file or layer destinations,...
virtual QString generateTemporaryDestination(const QgsProcessingContext *context=nullptr) const
Generates a temporary destination value for this parameter.
void setSupportsNonFileBasedOutput(bool supportsNonFileBasedOutput)
Sets whether the destination parameter supports non filed-based outputs, such as memory layers or dir...
Custom exception class for processing related exceptions.
Encapsulates settings relating to a feature source input to a processing algorithm.
QgsProperty source
Source definition.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Base class for providing feedback from a processing algorithm.
virtual void pushCommandInfo(const QString &info)
Pushes an informational message containing a command from the algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void pushDebugInfo(const QString &info)
Pushes an informational message containing debugging helpers from the algorithm.
virtual void setProgressText(const QString &text)
Sets a progress report text string.
Processing feedback object for multi-step operations.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
Base class for the definition of processing outputs.
Encapsulates settings relating to a feature sink or output raster layer for a processing algorithm.
QgsProperty sink
Sink/layer definition.
QString destinationName
Name to use for sink if it's to be loaded into a destination project.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
static QString typeName()
Returns the type name for the output class.
A vector layer output for processing algorithms.
static QString typeName()
Returns the type name for the output class.
QgsProcessing::SourceType dataType() const
Returns the layer type for the output layer.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
Base class for the definition of processing parameters.
QgsProcessingAlgorithm * algorithm() const
Returns a pointer to the algorithm which owns this parameter.
QVariantMap metadata() const
Returns the parameter's freeform metadata.
virtual bool isDestination() const
Returns true if this parameter represents a file or layer destination, e.g.
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
@ FlagOptional
Parameter is optional.
@ FlagHidden
Parameter is hidden and should not be shown to users.
virtual QgsProcessingParameterDefinition * clone() const =0
Creates a clone of the parameter definition.
virtual QString type() const =0
Unique parameter type name.
Flags flags() const
Returns any flags associated with the parameter.
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
QString name() const
Returns the name of the parameter.
virtual QStringList dependsOnOtherParameters() const
Returns a list of other parameter names on which this parameter is dependent (e.g.
virtual bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const
Checks whether the specified input value is acceptable for the parameter.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
A vector layer or feature source field parameter for processing algorithms.
static QString typeName()
Returns the type name for the parameter class.
DataType dataType() const
Returns the acceptable data type for the field.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
Can be inherited by parameters which require limits to their acceptable data types.
QList< int > dataTypes() const
Returns the geometry types for sources acceptable by the parameter.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
static QString typeName()
Returns the type name for the parameter class.
virtual QString pythonImportString() const
Returns a valid Python import string for importing the corresponding parameter type,...
static QString typeName()
Returns the type name for the parameter class.
static QgsProcessingParameterDefinition * parameterFromVariantMap(const QVariantMap &map)
Creates a new QgsProcessingParameterDefinition using the configuration from a supplied variant map.
Abstract base class for processing providers.
const QgsProcessingAlgorithm * algorithm(const QString &name) const
Returns the matching algorithm by name, or nullptr if no matching algorithm is contained by this prov...
QgsProcessingParameterType * parameterType(const QString &id) const
Returns the parameter type registered for id.
static QString formatHelpMapAsHtml(const QVariantMap &map, const QgsProcessingAlgorithm *algorithm)
Returns a HTML formatted version of the help text encoded in a variant map for a specified algorithm.
@ Vector
Vector layer type.
static QVariantMap removePointerValuesFromMap(const QVariantMap &map)
Removes any raw pointer values from an input map, replacing them with appropriate string values where...
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
PythonOutputType
Available Python output types.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
SourceType
Data source types enum.
@ TypeVectorLine
Vector line layers.
@ TypeMapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
@ TypeVectorPolygon
Vector polygon layers.
@ TypeVector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
@ TypeVectorPoint
Vector point layers.
@ TypeVectorAnyGeometry
Any vector layer with geometry.
A store for object properties.
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
QVariant staticValue() const
Returns the current static value for the property.
double yMaximum() const SIP_HOLDGIL
Returns the y maximum value (top side of rectangle).
double xMaximum() const SIP_HOLDGIL
Returns the x maximum value (right side of rectangle).
double xMinimum() const SIP_HOLDGIL
Returns the x minimum value (left side of rectangle).
double yMinimum() const SIP_HOLDGIL
Returns the y minimum value (bottom side of rectangle).
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Represents a vector layer which manages a vector based data sets.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
QString qgsSetJoin(const QSet< T > &set, const QString &separator)
Joins all the set values into a single string with each element separated by the given separator.
QMap< QString, QString > QgsStringMap
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Single variable definition for use within a QgsExpressionContextScope.