Skip to content
Permalink
Browse files

Merge pull request #4762 from nyalldawson/processing_model_c++

Resurrect ability to run processing models
  • Loading branch information
nyalldawson committed Jun 21, 2017
2 parents b01cae0 + 0db4cf3 commit c0293dce8b97cb69ef2f3913d634be4f36053af7
@@ -282,9 +282,9 @@ Copies are protected to avoid slicing
%End
public:

ModelOutput( const QString &description = QString() );
ModelOutput( const QString &name = QString(), const QString &description = QString() );
%Docstring
Constructor for ModelOutput with the specified ``description``.
Constructor for ModelOutput with the specified ``name`` and ``description``.
%End

QString childId() const;
@@ -502,6 +502,10 @@ Copies are protected to avoid slicing
%Docstring
Returns the final model output with matching ``name``. If no output
exists with the name, a new one will be created and returned.

If child model outputs are altered by this method, QgsProcessingModelAlgorithm.updateDestinationParameters()
must be called on the parent model.

.. seealso:: modelOutputs()
.. seealso:: setModelOutputs()
:rtype: QgsProcessingModelAlgorithm.ModelOutput
@@ -512,6 +516,10 @@ Copies are protected to avoid slicing
Sets the map of final model ``outputs`` which are generated by this child algorithm.
The keys are the output names from this child algorithm. Only outputs which are
part of the final outputs from the model should be included in this map.

If child model outputs are altered by this method, QgsProcessingModelAlgorithm.updateDestinationParameters()
must be called on the parent model.

.. seealso:: modelOutputs()
%End

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

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

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;

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

void updateDestinationParameters();
%Docstring
Updates the model's parameter definitions to include all relevant destination
parameters as required by child algorithm ModelOutputs.
Must be called whenever child algorithm ModelOutputs are altered.
%End

bool toFile( const QString &path ) const;
%Docstring
Writes the model to a file, at the specified ``path``.
@@ -742,6 +755,11 @@ Copies are protected to avoid slicing
:rtype: bool
%End

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;

};


@@ -11,7 +11,6 @@




class QgsProcessingFeatureSourceDefinition
{
%Docstring
@@ -1308,8 +1307,72 @@ class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition

};

class QgsProcessingDestinationParameter : QgsProcessingParameterDefinition
{
%Docstring
Base class for all parameter definitions which represent file or layer destinations, e.g. parameters
which are used for the destination for layers output by an algorithm.
.. versionadded:: 3.0
%End

class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingDestinationParameter( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingDestinationParameter.
%End

virtual bool isDestination() const;
virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


virtual QgsProcessingOutputDefinition *toOutputDefinition() const = 0 /Factory/;
%Docstring
Returns a new QgsProcessingOutputDefinition corresponding to the definition of the destination
parameter.
:rtype: QgsProcessingOutputDefinition
%End

bool supportsNonFileBasedOutputs() const;
%Docstring
Returns true if the destination parameter supports non filed-based outputs,
such as memory layers or direct database outputs.
.. seealso:: setSupportsNonFileBasedOutputs()
:rtype: bool
%End

void setSupportsNonFileBasedOutputs( bool supportsNonFileBasedOutputs );
%Docstring
Sets whether the destination parameter supports non filed-based outputs,
such as memory layers or direct database outputs.
.. seealso:: supportsNonFileBasedOutputs()
%End

virtual QString defaultFileExtension() const = 0;
%Docstring
Returns the default file extension for destination file paths
associated with this parameter.
:rtype: str
%End

virtual QString generateTemporaryDestination() const;
%Docstring
Generates a temporary destination value for this parameter. The returned
value will be a file path or QGIS data provider URI suitable for
temporary storage of created layers and files.
:rtype: str
%End

};


class QgsProcessingParameterFeatureSink : QgsProcessingDestinationParameter
{
%Docstring
A feature sink output for processing algorithms.
@@ -1330,11 +1393,14 @@ class QgsProcessingParameterFeatureSink : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

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

virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;

virtual QString defaultFileExtension() const;


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

virtual bool fromVariantMap( const QVariantMap &map );

virtual QString generateTemporaryDestination() const;


};


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

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

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

virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;

virtual QString defaultFileExtension() const;


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

};

class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition
class QgsProcessingParameterRasterOutput : QgsProcessingDestinationParameter
{
%Docstring
A raster layer output parameter.
@@ -1437,14 +1508,17 @@ class QgsProcessingParameterRasterOutput : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

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

virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;

virtual QString defaultFileExtension() const;

};

class QgsProcessingParameterFileOutput : QgsProcessingParameterDefinition
class QgsProcessingParameterFileOutput : QgsProcessingDestinationParameter
{
%Docstring
A generic file based output parameter.
@@ -1465,11 +1539,14 @@ class QgsProcessingParameterFileOutput : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

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

virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;

virtual QString defaultFileExtension() const;


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

};

class QgsProcessingParameterFolderOutput : QgsProcessingParameterDefinition
class QgsProcessingParameterFolderOutput : QgsProcessingDestinationParameter
{
%Docstring
A folder output parameter.
@@ -1511,9 +1588,12 @@ class QgsProcessingParameterFolderOutput : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QgsProcessingOutputDefinition *toOutputDefinition() const /Factory/;

virtual QString defaultFileExtension() const;


};

@@ -135,6 +135,22 @@ class QgsProcessingUtils
:rtype: QVariant
%End

static QString tempFolder();
%Docstring
Returns a session specific processing temporary folder for use in processing algorithms.
.. seealso:: generateTempFileName()
:rtype: str
%End

static QString generateTempFilename( const QString &basename );
%Docstring
Returns a temporary filename for a given file, putting it into
a temporary folder (creating that folder in the process),
but not changing the ``basename``.
.. seealso:: tempFolder()
:rtype: str
%End

};


@@ -31,7 +31,8 @@
import os
import sys

from qgis.core import QgsApplication
from qgis.core import (QgsApplication,
QgsProcessingUtils)
from qgis.gui import QgsOptionsWidgetFactory
from qgis.PyQt.QtCore import Qt, QCoreApplication, QDir
from qgis.PyQt.QtWidgets import QMenu, QAction
@@ -44,7 +45,7 @@
from processing.gui.ResultsDock import ResultsDock
from processing.gui.AlgorithmLocatorFilter import AlgorithmLocatorFilter
from processing.modeler.ModelerDialog import ModelerDialog
from processing.tools.system import tempFolder, tempHelpFolder
from processing.tools.system import tempHelpFolder
from processing.gui.menus import removeMenus, initializeMenus, createMenus
from processing.core.ProcessingResults import resultsList

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

# delete temporary output files
folder = tempFolder()
folder = QgsProcessingUtils.tempFolder()
if QDir(folder).exists():
shutil.rmtree(folder, True)

@@ -29,13 +29,13 @@

from qgis.PyQt.QtGui import QIcon

from qgis.core import QgsProcessingUtils
from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.core.outputs import OutputRaster
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterSelection
from processing.algs.gdal.GdalUtils import GdalUtils
from processing.tools.system import tempFolder
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
@@ -85,7 +85,7 @@ def getConsoleCommands(self, parameters):
arguments.append('-allow_projection_difference')
# Always write input files to a text file in case there are many of them and the
# length of the command will be longer then allowed in command prompt
listFile = os.path.join(tempFolder(), 'buildvrtInputFiles.txt')
listFile = os.path.join(QgsProcessingUtils.tempFolder(), 'buildvrtInputFiles.txt')
with open(listFile, 'w') as f:
f.write(self.getParameterValue(buildvrt.INPUT).replace(';', '\n'))
arguments.append('-input_file_list')
@@ -37,7 +37,7 @@
QgsMessageLog)
from qgis.PyQt.QtCore import QCoreApplication
from processing.core.ProcessingConfig import ProcessingConfig
from processing.tools.system import userFolder, isWindows, isMac, tempFolder, mkdir
from processing.tools.system import userFolder, isWindows, isMac, mkdir
from processing.tests.TestData import points


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

@staticmethod
def grassDataFolder():
tempfolder = os.path.join(tempFolder(), 'grassdata')
tempfolder = os.path.join(QgsProcessingUtils.tempFolder(), 'grassdata')
mkdir(tempfolder)
return tempfolder

@@ -54,7 +54,7 @@
OutputRaster)
from processing.tools import dataobjects
from processing.algs.help import shortHelp
from processing.tools.system import getTempFilename, getTempFilenameInTempFolder
from processing.tools.system import getTempFilename
from processing.algs.saga.SagaNameDecorator import decoratedAlgorithmName, decoratedGroupName
from . import SagaUtils

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

0 comments on commit c0293dc

Please sign in to comment.
You can’t perform that action at this time.