Skip to content

Commit c0293dc

Browse files
authoredJun 21, 2017
Merge pull request #4762 from nyalldawson/processing_model_c++
Resurrect ability to run processing models
2 parents b01cae0 + 0db4cf3 commit c0293dc

23 files changed

+908
-187
lines changed
 

‎python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ Copies are protected to avoid slicing
282282
%End
283283
public:
284284

285-
ModelOutput( const QString &description = QString() );
285+
ModelOutput( const QString &name = QString(), const QString &description = QString() );
286286
%Docstring
287-
Constructor for ModelOutput with the specified ``description``.
287+
Constructor for ModelOutput with the specified ``name`` and ``description``.
288288
%End
289289

290290
QString childId() const;
@@ -502,6 +502,10 @@ Copies are protected to avoid slicing
502502
%Docstring
503503
Returns the final model output with matching ``name``. If no output
504504
exists with the name, a new one will be created and returned.
505+
506+
If child model outputs are altered by this method, QgsProcessingModelAlgorithm.updateDestinationParameters()
507+
must be called on the parent model.
508+
505509
.. seealso:: modelOutputs()
506510
.. seealso:: setModelOutputs()
507511
:rtype: QgsProcessingModelAlgorithm.ModelOutput
@@ -512,6 +516,10 @@ Copies are protected to avoid slicing
512516
Sets the map of final model ``outputs`` which are generated by this child algorithm.
513517
The keys are the output names from this child algorithm. Only outputs which are
514518
part of the final outputs from the model should be included in this map.
519+
520+
If child model outputs are altered by this method, QgsProcessingModelAlgorithm.updateDestinationParameters()
521+
must be called on the parent model.
522+
515523
.. seealso:: modelOutputs()
516524
%End
517525

@@ -549,8 +557,6 @@ Copies are protected to avoid slicing
549557

550558
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
551559

552-
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
553-
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
554560

555561
void setName( const QString &name );
556562
%Docstring
@@ -728,6 +734,13 @@ Copies are protected to avoid slicing
728734
:rtype: QgsProcessingModelAlgorithm.ModelParameter
729735
%End
730736

737+
void updateDestinationParameters();
738+
%Docstring
739+
Updates the model's parameter definitions to include all relevant destination
740+
parameters as required by child algorithm ModelOutputs.
741+
Must be called whenever child algorithm ModelOutputs are altered.
742+
%End
743+
731744
bool toFile( const QString &path ) const;
732745
%Docstring
733746
Writes the model to a file, at the specified ``path``.
@@ -742,6 +755,11 @@ Copies are protected to avoid slicing
742755
:rtype: bool
743756
%End
744757

758+
protected:
759+
760+
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
761+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
762+
745763
};
746764

747765

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212

1313

14-
1514
class QgsProcessingFeatureSourceDefinition
1615
{
1716
%Docstring
@@ -1308,8 +1307,72 @@ class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition
13081307

13091308
};
13101309

1310+
class QgsProcessingDestinationParameter : QgsProcessingParameterDefinition
1311+
{
1312+
%Docstring
1313+
Base class for all parameter definitions which represent file or layer destinations, e.g. parameters
1314+
which are used for the destination for layers output by an algorithm.
1315+
.. versionadded:: 3.0
1316+
%End
13111317

1312-
class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
1318+
%TypeHeaderCode
1319+
#include "qgsprocessingparameters.h"
1320+
%End
1321+
public:
1322+
1323+
QgsProcessingDestinationParameter( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
1324+
bool optional = false );
1325+
%Docstring
1326+
Constructor for QgsProcessingDestinationParameter.
1327+
%End
1328+
1329+
virtual bool isDestination() const;
1330+
virtual QVariantMap toVariantMap() const;
1331+
1332+
virtual bool fromVariantMap( const QVariantMap &map );
1333+
1334+
1335+
virtual QgsProcessingOutputDefinition *toOutputDefinition() const = 0 /Factory/;
1336+
%Docstring
1337+
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination
1338+
parameter.
1339+
:rtype: QgsProcessingOutputDefinition
1340+
%End
1341+
1342+
bool supportsNonFileBasedOutputs() const;
1343+
%Docstring
1344+
Returns true if the destination parameter supports non filed-based outputs,
1345+
such as memory layers or direct database outputs.
1346+
.. seealso:: setSupportsNonFileBasedOutputs()
1347+
:rtype: bool
1348+
%End
1349+
1350+
void setSupportsNonFileBasedOutputs( bool supportsNonFileBasedOutputs );
1351+
%Docstring
1352+
Sets whether the destination parameter supports non filed-based outputs,
1353+
such as memory layers or direct database outputs.
1354+
.. seealso:: supportsNonFileBasedOutputs()
1355+
%End
1356+
1357+
virtual QString defaultFileExtension() const = 0;
1358+
%Docstring
1359+
Returns the default file extension for destination file paths
1360+
associated with this parameter.
1361+
:rtype: str
1362+
%End
1363+
1364+
virtual QString generateTemporaryDestination() const;
1365+
%Docstring
1366+
Generates a temporary destination value for this parameter. The returned
1367+
value will be a file path or QGIS data provider URI suitable for
1368+
temporary storage of created layers and files.
1369+
:rtype: str
1370+
%End
1371+
1372+
};
1373+
1374+
1375+
class QgsProcessingParameterFeatureSink : QgsProcessingDestinationParameter
13131376
{
13141377
%Docstring
13151378
A feature sink output for processing algorithms.
@@ -1330,11 +1393,14 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
13301393
%End
13311394

13321395
virtual QString type() const;
1333-
virtual bool isDestination() const;
13341396
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
13351397

13361398
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
13371399

1400+
virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;
1401+
1402+
virtual QString defaultFileExtension() const;
1403+
13381404

13391405
QgsProcessingParameterDefinition::LayerType dataType() const;
13401406
%Docstring
@@ -1360,11 +1426,13 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
13601426

13611427
virtual bool fromVariantMap( const QVariantMap &map );
13621428

1429+
virtual QString generateTemporaryDestination() const;
1430+
13631431

13641432
};
13651433

13661434

1367-
class QgsProcessingParameterVectorOutput : QgsProcessingParameterDefinition
1435+
class QgsProcessingParameterVectorOutput : QgsProcessingDestinationParameter
13681436
{
13691437
%Docstring
13701438
A vector layer output parameter. Consider using the more flexible QgsProcessingParameterFeatureSink wherever
@@ -1384,11 +1452,14 @@ class QgsProcessingParameterVectorOutput : QgsProcessingParameterDefinition
13841452
%End
13851453

13861454
virtual QString type() const;
1387-
virtual bool isDestination() const;
13881455
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
13891456

13901457
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
13911458

1459+
virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;
1460+
1461+
virtual QString defaultFileExtension() const;
1462+
13921463

13931464
QgsProcessingParameterDefinition::LayerType dataType() const;
13941465
%Docstring
@@ -1417,7 +1488,7 @@ class QgsProcessingParameterVectorOutput : QgsProcessingParameterDefinition
14171488

14181489
};
14191490

1420-
class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition
1491+
class QgsProcessingParameterRasterOutput : QgsProcessingDestinationParameter
14211492
{
14221493
%Docstring
14231494
A raster layer output parameter.
@@ -1437,14 +1508,17 @@ class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition
14371508
%End
14381509

14391510
virtual QString type() const;
1440-
virtual bool isDestination() const;
14411511
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
14421512

14431513
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
14441514

1515+
virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;
1516+
1517+
virtual QString defaultFileExtension() const;
1518+
14451519
};
14461520

1447-
class QgsProcessingParameterFileOutput : QgsProcessingParameterDefinition
1521+
class QgsProcessingParameterFileOutput : QgsProcessingDestinationParameter
14481522
{
14491523
%Docstring
14501524
A generic file based output parameter.
@@ -1465,11 +1539,14 @@ class QgsProcessingParameterFileOutput : QgsProcessingParameterDefinition
14651539
%End
14661540

14671541
virtual QString type() const;
1468-
virtual bool isDestination() const;
14691542
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
14701543

14711544
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
14721545

1546+
virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;
1547+
1548+
virtual QString defaultFileExtension() const;
1549+
14731550

14741551
QString fileFilter() const;
14751552
%Docstring
@@ -1491,7 +1568,7 @@ class QgsProcessingParameterFileOutput : QgsProcessingParameterDefinition
14911568

14921569
};
14931570

1494-
class QgsProcessingParameterFolderOutput : QgsProcessingParameterDefinition
1571+
class QgsProcessingParameterFolderOutput : QgsProcessingDestinationParameter
14951572
{
14961573
%Docstring
14971574
A folder output parameter.
@@ -1511,9 +1588,12 @@ class QgsProcessingParameterFolderOutput : QgsProcessingParameterDefinition
15111588
%End
15121589

15131590
virtual QString type() const;
1514-
virtual bool isDestination() const;
15151591
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
15161592

1593+
virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;
1594+
1595+
virtual QString defaultFileExtension() const;
1596+
15171597

15181598
};
15191599

‎python/core/processing/qgsprocessingutils.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ class QgsProcessingUtils
135135
:rtype: QVariant
136136
%End
137137

138+
static QString tempFolder();
139+
%Docstring
140+
Returns a session specific processing temporary folder for use in processing algorithms.
141+
.. seealso:: generateTempFileName()
142+
:rtype: str
143+
%End
144+
145+
static QString generateTempFilename( const QString &basename );
146+
%Docstring
147+
Returns a temporary filename for a given file, putting it into
148+
a temporary folder (creating that folder in the process),
149+
but not changing the ``basename``.
150+
.. seealso:: tempFolder()
151+
:rtype: str
152+
%End
153+
138154
};
139155

140156

‎python/plugins/processing/ProcessingPlugin.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import os
3232
import sys
3333

34-
from qgis.core import QgsApplication
34+
from qgis.core import (QgsApplication,
35+
QgsProcessingUtils)
3536
from qgis.gui import QgsOptionsWidgetFactory
3637
from qgis.PyQt.QtCore import Qt, QCoreApplication, QDir
3738
from qgis.PyQt.QtWidgets import QMenu, QAction
@@ -44,7 +45,7 @@
4445
from processing.gui.ResultsDock import ResultsDock
4546
from processing.gui.AlgorithmLocatorFilter import AlgorithmLocatorFilter
4647
from processing.modeler.ModelerDialog import ModelerDialog
47-
from processing.tools.system import tempFolder, tempHelpFolder
48+
from processing.tools.system import tempHelpFolder
4849
from processing.gui.menus import removeMenus, initializeMenus, createMenus
4950
from processing.core.ProcessingResults import resultsList
5051

@@ -142,7 +143,7 @@ def unload(self):
142143
self.menu.deleteLater()
143144

144145
# delete temporary output files
145-
folder = tempFolder()
146+
folder = QgsProcessingUtils.tempFolder()
146147
if QDir(folder).exists():
147148
shutil.rmtree(folder, True)
148149

‎python/plugins/processing/algs/gdal/buildvrt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929

3030
from qgis.PyQt.QtGui import QIcon
3131

32+
from qgis.core import QgsProcessingUtils
3233
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
3334
from processing.core.outputs import OutputRaster
3435
from processing.core.parameters import ParameterBoolean
3536
from processing.core.parameters import ParameterMultipleInput
3637
from processing.core.parameters import ParameterSelection
3738
from processing.algs.gdal.GdalUtils import GdalUtils
38-
from processing.tools.system import tempFolder
3939
from processing.tools import dataobjects
4040

4141
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@@ -85,7 +85,7 @@ def getConsoleCommands(self, parameters):
8585
arguments.append('-allow_projection_difference')
8686
# Always write input files to a text file in case there are many of them and the
8787
# length of the command will be longer then allowed in command prompt
88-
listFile = os.path.join(tempFolder(), 'buildvrtInputFiles.txt')
88+
listFile = os.path.join(QgsProcessingUtils.tempFolder(), 'buildvrtInputFiles.txt')
8989
with open(listFile, 'w') as f:
9090
f.write(self.getParameterValue(buildvrt.INPUT).replace(';', '\n'))
9191
arguments.append('-input_file_list')

‎python/plugins/processing/algs/grass7/Grass7Utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
QgsMessageLog)
3838
from qgis.PyQt.QtCore import QCoreApplication
3939
from processing.core.ProcessingConfig import ProcessingConfig
40-
from processing.tools.system import userFolder, isWindows, isMac, tempFolder, mkdir
40+
from processing.tools.system import userFolder, isWindows, isMac, mkdir
4141
from processing.tests.TestData import points
4242

4343

@@ -205,7 +205,7 @@ def grassMapsetFolder():
205205

206206
@staticmethod
207207
def grassDataFolder():
208-
tempfolder = os.path.join(tempFolder(), 'grassdata')
208+
tempfolder = os.path.join(QgsProcessingUtils.tempFolder(), 'grassdata')
209209
mkdir(tempfolder)
210210
return tempfolder
211211

‎python/plugins/processing/algs/saga/SagaAlgorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
OutputRaster)
5555
from processing.tools import dataobjects
5656
from processing.algs.help import shortHelp
57-
from processing.tools.system import getTempFilename, getTempFilenameInTempFolder
57+
from processing.tools.system import getTempFilename
5858
from processing.algs.saga.SagaNameDecorator import decoratedAlgorithmName, decoratedGroupName
5959
from . import SagaUtils
6060

@@ -342,7 +342,7 @@ def exportRasterLayer(self, source):
342342
filename = ''.join(c for c in filename if c in validChars)
343343
if len(filename) == 0:
344344
filename = 'layer'
345-
destFilename = getTempFilenameInTempFolder(filename + '.sgrd')
345+
destFilename = QgsProcessingUtils.generateTempFilename(filename + '.sgrd')
346346
self.exportedLayers[source] = destFilename
347347
sessionExportedLayers[source] = destFilename
348348
return 'io_gdal 0 -TRANSFORM 1 -RESAMPLING 3 -GRIDS "' + destFilename + '" -FILES "' + source + '"'

0 commit comments

Comments
 (0)
Please sign in to comment.