mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-27 00:07:16 -05:00
Start on executing models
This commit is contained in:
parent
47f2cc895c
commit
1df9f6b6f0
@ -557,8 +557,6 @@ Copies are protected to avoid slicing
|
|||||||
|
|
||||||
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
|
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
|
||||||
|
|
||||||
virtual QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
|
||||||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
|
|
||||||
|
|
||||||
void setName( const QString &name );
|
void setName( const QString &name );
|
||||||
%Docstring
|
%Docstring
|
||||||
@ -757,6 +755,11 @@ Copies are protected to avoid slicing
|
|||||||
:rtype: bool
|
:rtype: bool
|
||||||
%End
|
%End
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
||||||
|
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -242,14 +242,14 @@ class ModelerAlgorithm(QgsProcessingModelAlgorithm):
|
|||||||
|
|
||||||
def processAlgorithm(self, parameters, context, feedback):
|
def processAlgorithm(self, parameters, context, feedback):
|
||||||
executed = []
|
executed = []
|
||||||
toExecute = [alg for alg in list(self.algs.values()) if alg.isActive()]
|
toExecute = [alg for alg in list(self.childAlgorithms().values()) if alg.isActive()]
|
||||||
while len(executed) < len(toExecute):
|
while len(executed) < len(toExecute):
|
||||||
for alg in toExecute:
|
for alg in toExecute:
|
||||||
if alg.childId() not in executed:
|
if alg.childId() not in executed:
|
||||||
canExecute = True
|
canExecute = True
|
||||||
required = self.dependsOnChildAlgorithms(alg.childId())
|
required = self.dependsOnChildAlgorithms(alg.childId())
|
||||||
for requiredAlg in required:
|
for requiredAlg in required:
|
||||||
if requiredAlg != alg.childId() and requiredAlg not in executed:
|
if requiredAlg not in executed:
|
||||||
canExecute = False
|
canExecute = False
|
||||||
break
|
break
|
||||||
if canExecute:
|
if canExecute:
|
||||||
|
|||||||
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include "qgsprocessingmodelalgorithm.h"
|
#include "qgsprocessingmodelalgorithm.h"
|
||||||
#include "qgsprocessingregistry.h"
|
#include "qgsprocessingregistry.h"
|
||||||
|
#include "qgsprocessingfeedback.h"
|
||||||
#include "qgsxmlutils.h"
|
#include "qgsxmlutils.h"
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
@ -285,13 +286,77 @@ QString QgsProcessingModelAlgorithm::svgIconPath() const
|
|||||||
|
|
||||||
QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
|
QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
|
||||||
{
|
{
|
||||||
Q_UNUSED( parameters );
|
QSet< QString > toExecute;
|
||||||
Q_UNUSED( context );
|
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
|
||||||
Q_UNUSED( feedback );
|
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
|
||||||
return QVariantMap();
|
{
|
||||||
}
|
if ( childIt->isActive() )
|
||||||
|
toExecute.insert( childIt->childId() );
|
||||||
|
}
|
||||||
|
|
||||||
void QgsProcessingModelAlgorithm::setName( const QString &name )
|
QMap< QString, QVariantMap > resultsMap;
|
||||||
|
QSet< QString > executed;
|
||||||
|
while ( executed.count() < toExecute.count() )
|
||||||
|
{
|
||||||
|
Q_FOREACH ( const QString &childId, toExecute )
|
||||||
|
{
|
||||||
|
if ( executed.contains( childId ) )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
bool canExecute = true;
|
||||||
|
Q_FOREACH ( const QString &dependency, dependsOnChildAlgorithms( childId ) )
|
||||||
|
{
|
||||||
|
if ( !executed.contains( dependency ) )
|
||||||
|
{
|
||||||
|
canExecute = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( !canExecute )
|
||||||
|
continue;
|
||||||
|
|
||||||
|
feedback->pushDebugInfo( QObject::tr( "Prepare algorithm: %1" ).arg( childId ) );
|
||||||
|
|
||||||
|
const ChildAlgorithm &child = mChildAlgorithms[ childId ];
|
||||||
|
|
||||||
|
// self.prepareAlgorithm( alg )
|
||||||
|
|
||||||
|
feedback->setProgressText( QObject::tr( "Running %1 [%2/%3]" ).arg( child.description() ).arg( executed.count() + 1 ).arg( toExecute.count() ) );
|
||||||
|
//feedback->pushDebugInfo( "Parameters: " + ', '.join( [str( p ).strip() +
|
||||||
|
// '=' + str( p.value ) for p in alg.algorithm.parameters] ) )
|
||||||
|
//t0 = time.time()
|
||||||
|
QVariantMap results = child.algorithm()->run( parameters, context, feedback );
|
||||||
|
resultsMap.insert( childId, results );
|
||||||
|
|
||||||
|
//dt = time.time() - t0
|
||||||
|
#if 0
|
||||||
|
|
||||||
|
# copy algorithm output value(s) back to model in case the algorithm modified those
|
||||||
|
for out in alg.algorithm().outputs :
|
||||||
|
if not out.flags() & QgsProcessingParameterDefinition.FlagHidden:
|
||||||
|
if out.name() in alg.modelOutputs():
|
||||||
|
modelOut = self.getOutputFromName( self.getSafeNameForOutput( alg.childId(), out.name() ) )
|
||||||
|
if modelOut:
|
||||||
|
modelOut.value = out.value
|
||||||
|
#endif
|
||||||
|
|
||||||
|
executed.insert( childId );
|
||||||
|
//feedback->pushDebugInfo( QObject::tr( "OK. Execution took %1 ms (%2 outputs)." ).arg( dt, len( alg.algorithm.modelOutputs() ) ) )
|
||||||
|
#if 0
|
||||||
|
except GeoAlgorithmExecutionException as e:
|
||||||
|
feedback.pushDebugInfo( self.tr( 'Failed', 'ModelerAlgorithm' ) )
|
||||||
|
raise GeoAlgorithmExecutionException(
|
||||||
|
self.tr( 'Error executing algorithm {0}\n{1}', 'ModelerAlgorithm' ).format( alg.description, e.msg ) )
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
feedback->pushDebugInfo( QObject::tr( "Model processed ok. Executed %1 algorithms total" ).arg( executed.count() ) );
|
||||||
|
|
||||||
|
return QVariantMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsProcessingModelAlgorithm::setName( const QString &name )
|
||||||
{
|
{
|
||||||
mModelName = name;
|
mModelName = name;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -559,8 +559,6 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
|
|||||||
QString svgIconPath() const override;
|
QString svgIconPath() const override;
|
||||||
|
|
||||||
bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
|
bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
|
||||||
QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
|
||||||
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the model \a name.
|
* Sets the model \a name.
|
||||||
@ -747,6 +745,11 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
|
|||||||
*/
|
*/
|
||||||
bool fromFile( const QString &path );
|
bool fromFile( const QString &path );
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
||||||
|
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
QString mModelName;
|
QString mModelName;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user