QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsprocessingalgorithm.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingalgorithm.h
3  ------------------------
4  begin : December 2016
5  copyright : (C) 2016 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSPROCESSINGALGORITHM_H
19 #define QGSPROCESSINGALGORITHM_H
20 
21 #include "qgis_core.h"
22 #include "qgis.h"
24 #include "qgsprocessingoutputs.h"
25 #include "qgsprocessingcontext.h"
26 #include "qgsfeaturesource.h"
27 #include "qgsprocessingutils.h"
28 #include <QString>
29 #include <QVariant>
30 #include <QIcon>
31 
34 class QgsFeatureSink;
35 class QgsProcessingModelAlgorithm;
37 
38 #ifdef SIP_RUN
39 % ModuleHeaderCode
40 #include <qgsprocessingmodelalgorithm.h>
41 % End
42 #endif
43 
50 class CORE_EXPORT QgsProcessingAlgorithm
51 {
52 
53 #ifdef SIP_RUN
55  if ( dynamic_cast< QgsProcessingModelAlgorithm * >( sipCpp ) != NULL )
56  sipType = sipType_QgsProcessingModelAlgorithm;
57  else if ( dynamic_cast< QgsProcessingFeatureBasedAlgorithm * >( sipCpp ) != NULL )
58  sipType = sipType_QgsProcessingFeatureBasedAlgorithm;
59  else
60  sipType = sipType_QgsProcessingAlgorithm;
61  SIP_END
62 #endif
63 
64  public:
65 
67  enum Flag
68  {
69  FlagHideFromToolbox = 1 << 1,
70  FlagHideFromModeler = 1 << 2,
71  FlagSupportsBatch = 1 << 3,
72  FlagCanCancel = 1 << 4,
73  FlagRequiresMatchingCrs = 1 << 5,
74  FlagNoThreading = 1 << 6,
75  FlagDisplayNameIsLiteral = 1 << 7,
76  FlagSupportsInPlaceEdits = 1 << 8,
77  FlagKnownIssues = 1 << 9,
78  FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler,
79  };
80  Q_DECLARE_FLAGS( Flags, Flag )
81 
82 
89  QgsProcessingAlgorithm() = default;
90 
91  virtual ~QgsProcessingAlgorithm();
92 
94  QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other ) = delete;
96  QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
97 
98  /*
99  * IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
100  * the case.
101  * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
102  *
103  * "
104  * /Factory/ is used when the instance returned is guaranteed to be new to Python.
105  * In this case it isn't because it has already been seen when being returned by createInstance()
106  * (However for a different sub-class implemented in C++ then it would be the first time it was seen
107  * by Python so the /Factory/ on create() would be correct.)
108  *
109  * You might try using /TransferBack/ on create() instead - that might be the best compromise.
110  * "
111  */
112 
130  QgsProcessingAlgorithm *create( const QVariantMap &configuration = QVariantMap() ) const SIP_THROW( QgsProcessingException ) SIP_TRANSFERBACK;
131 
141  virtual QString name() const = 0;
142 
149  QString id() const;
150 
161  virtual QString displayName() const = 0;
162 
168  virtual QString shortDescription() const;
169 
174  virtual QStringList tags() const { return QStringList(); }
175 
182  virtual QString shortHelpString() const;
183 
191  Q_DECL_DEPRECATED virtual QString helpString() const SIP_DEPRECATED;
192 
198  virtual QString helpUrl() const;
199 
204  virtual QIcon icon() const;
205 
210  virtual QString svgIconPath() const;
211 
218  virtual QString group() const { return QString(); }
219 
227  virtual QString groupId() const { return QString(); }
228 
233  virtual Flags flags() const;
234 
241  virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
242 
250  virtual bool checkParameterValues( const QVariantMap &parameters,
251  QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
252 
260  virtual QVariantMap preprocessParameters( const QVariantMap &parameters );
261 
265  QgsProcessingProvider *provider() const;
266 
274 
280  const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
281 
286  int countVisibleParameters() const;
287 
293  QgsProcessingParameterDefinitions destinationParameterDefinitions() const;
294 
301 
307  const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const;
308 
312  bool hasHtmlOutputs() const;
313 
331  QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap() ) const;
332 
343  bool prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
344 
356  QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
357 
366  QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback );
367 
374  virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
375 
382  QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
383  QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
384 
391  virtual bool validateInputCrs( const QVariantMap &parameters,
392  QgsProcessingContext &context ) const;
393 
401  virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
402 
406  void setProvider( QgsProcessingProvider *provider );
407 
408  protected:
409 
415  virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
416 
435  virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
436 
454  bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true );
455 
460  void removeParameter( const QString &name );
461 
475  bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER );
476 
507  virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
508 
539  virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
540 
568  virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
569 
573  QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
574 
578  QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
579 
583  double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
584 
588  int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
589 
594  QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
595 
599  int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
600 
604  QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
605 
609  bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
610 
625  QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
626  const QgsFields &fields, QgsWkbTypes::Type geometryType = QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), QgsFeatureSink::SinkFlags sinkFlags = nullptr ) const SIP_FACTORY;
627 
636  QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
637 
650  QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
651  QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr );
652 
660  QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
661 
669  QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
670 
674  QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
675 
679  QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
680 
688  QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
689 
693  QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
694 
703  QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
705 
716  QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
718 
724  QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
725 
734  QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
736 
742  QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
743 
747  QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
748 
753  QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
754 
758  QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
759 
763  QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
764 
768  QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
769 
782  static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
783 
796  static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
797 
810  static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
811 
819  virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
820 
821  private:
822 
823  QgsProcessingProvider *mProvider = nullptr;
826  bool mHasPrepared = false;
827  bool mHasExecuted = false;
828  bool mHasPostProcessed = false;
829  std::unique_ptr< QgsProcessingContext > mLocalContext;
830 
831  bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
832 
833 
834  friend class QgsProcessingProvider;
835  friend class TestQgsProcessing;
836  friend class QgsProcessingModelAlgorithm;
838 
839 #ifdef SIP_RUN
841 #endif
842 
843 };
844 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
845 
846 
847 
848 
872 {
873  public:
874 
879 
880  QgsProcessingAlgorithm::Flags flags() const override;
881 
905  virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
906 
907  protected:
908 
909  void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
910 
916  virtual QString outputName() const = 0;
917 
923  virtual QList<int> inputLayerTypes() const;
924 
929  virtual QgsProcessing::SourceType outputLayerType() const;
930 
934  virtual QgsProcessingFeatureSource::Flag sourceFlags() const;
935 
941  virtual QgsFeatureSink::SinkFlags sinkFlags() const;
942 
950  virtual QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const;
951 
962  virtual QgsFields outputFields( const QgsFields &inputFields ) const;
963 
973 
979  virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
980 
985  QgsCoordinateReferenceSystem sourceCrs() const;
986 
987 
988  QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
989 
994  virtual QgsFeatureRequest request() const;
995 
1004  bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1005 
1011  void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1012 
1013  private:
1014 
1015  std::unique_ptr< QgsProcessingFeatureSource > mSource;
1016 
1017 };
1018 
1019 // clazy:excludeall=qstring-allocations
1020 
1021 #endif // QGSPROCESSINGALGORITHM_H
1022 
1023 
#define SIP_VIRTUALERRORHANDLER(name)
Definition: qgis_sip.h:170
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
Base class for all map layer types.
Definition: qgsmaplayer.h:63
Base class for providing feedback from a processing algorithm.
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns an ordered list of parameter definitions utilized by the algorithm.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
A class to represent a 2D point.
Definition: qgspointxy.h:43
An interface for objects which accept features via addFeature(s) methods.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource, modifying results according to the settings in a QgsProcessingContext.
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
Abstract base class for processing providers.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:41
Abstract base class for processing algorithms.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
#define SIP_TRANSFER
Definition: qgis_sip.h:36
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
#define SIP_END
Definition: qgis_sip.h:182
#define SIP_FACTORY
Definition: qgis_sip.h:69
#define SIP_DEPRECATED
Definition: qgis_sip.h:99
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Base class for the definition of processing outputs.
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
A configuration widget for processing algorithms allows providing additional configuration options di...
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox...
#define SIP_OUT
Definition: qgis_sip.h:51
This class represents a coordinate reference system (CRS).
Base class for the definition of processing parameters.
Flag
Flags indicating how and when an algorithm operates and should be exposed to users.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:165
virtual QString group() const
Returns the name of the group this algorithm belongs to.
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
const QgsCoordinateReferenceSystem & outputCrs
#define SIP_THROW(name)
Definition: qgis_sip.h:177
An abstract QgsProcessingAlgorithm base class for processing algorithms which operate "feature-by-fea...
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.