QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 class QgsMeshLayer;
38 
39 #ifdef SIP_RUN
40 % ModuleHeaderCode
41 #include <qgsprocessingmodelalgorithm.h>
42 % End
43 #endif
44 
51 class CORE_EXPORT QgsProcessingAlgorithm
52 {
53 
54 #ifdef SIP_RUN
56  if ( dynamic_cast< QgsProcessingModelAlgorithm * >( sipCpp ) != NULL )
57  sipType = sipType_QgsProcessingModelAlgorithm;
58  else if ( dynamic_cast< QgsProcessingFeatureBasedAlgorithm * >( sipCpp ) != NULL )
59  sipType = sipType_QgsProcessingFeatureBasedAlgorithm;
60  else
61  sipType = sipType_QgsProcessingAlgorithm;
62  SIP_END
63 #endif
64 
65  public:
66 
68  enum Flag
69  {
70  FlagHideFromToolbox = 1 << 1,
71  FlagHideFromModeler = 1 << 2,
72  FlagSupportsBatch = 1 << 3,
73  FlagCanCancel = 1 << 4,
74  FlagRequiresMatchingCrs = 1 << 5,
75  FlagNoThreading = 1 << 6,
76  FlagDisplayNameIsLiteral = 1 << 7,
77  FlagSupportsInPlaceEdits = 1 << 8,
78  FlagKnownIssues = 1 << 9,
79  FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler,
80  };
81  Q_DECLARE_FLAGS( Flags, Flag )
82 
83 
90  QgsProcessingAlgorithm() = default;
91 
92  virtual ~QgsProcessingAlgorithm();
93 
95  QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other ) = delete;
97  QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
98 
99  /*
100  * IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
101  * the case.
102  * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
103  *
104  * "
105  * /Factory/ is used when the instance returned is guaranteed to be new to Python.
106  * In this case it isn't because it has already been seen when being returned by createInstance()
107  * (However for a different sub-class implemented in C++ then it would be the first time it was seen
108  * by Python so the /Factory/ on create() would be correct.)
109  *
110  * You might try using /TransferBack/ on create() instead - that might be the best compromise.
111  * "
112  */
113 
131  QgsProcessingAlgorithm *create( const QVariantMap &configuration = QVariantMap() ) const SIP_THROW( QgsProcessingException ) SIP_TRANSFERBACK;
132 
142  virtual QString name() const = 0;
143 
150  QString id() const;
151 
162  virtual QString displayName() const = 0;
163 
169  virtual QString shortDescription() const;
170 
175  virtual QStringList tags() const { return QStringList(); }
176 
183  virtual QString shortHelpString() const;
184 
192  Q_DECL_DEPRECATED virtual QString helpString() const SIP_DEPRECATED;
193 
199  virtual QString helpUrl() const;
200 
205  virtual QIcon icon() const;
206 
211  virtual QString svgIconPath() const;
212 
219  virtual QString group() const { return QString(); }
220 
228  virtual QString groupId() const { return QString(); }
229 
234  virtual Flags flags() const;
235 
242  virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
243 
251  virtual bool checkParameterValues( const QVariantMap &parameters,
252  QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
253 
261  virtual QVariantMap preprocessParameters( const QVariantMap &parameters );
262 
266  QgsProcessingProvider *provider() const;
267 
275 
281  const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
282 
287  int countVisibleParameters() const;
288 
294  QgsProcessingParameterDefinitions destinationParameterDefinitions() const;
295 
302 
308  const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const;
309 
313  bool hasHtmlOutputs() const;
314 
332  QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap() ) const;
333 
344  bool prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
345 
357  QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
358 
367  QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback );
368 
375  virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
376 
383  virtual QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
384  QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
385 
392  virtual bool validateInputCrs( const QVariantMap &parameters,
393  QgsProcessingContext &context ) const;
394 
402  virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
403 
407  void setProvider( QgsProcessingProvider *provider );
408 
409  protected:
410 
416  virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
417 
436  virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
437 
455  bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true );
456 
461  void removeParameter( const QString &name );
462 
476  bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER );
477 
508  virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
509 
540  virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
541 
569  virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
570 
574  QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
575 
579  QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
580 
584  double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
585 
589  int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
590 
595  QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
596 
600  int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
601 
605  QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
606 
610  bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
611 
616  bool parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
617 
632  QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
633  const QgsFields &fields, QgsWkbTypes::Type geometryType = QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), QgsFeatureSink::SinkFlags sinkFlags = nullptr ) const SIP_FACTORY;
634 
643  QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
644 
657  QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
658  QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr );
659 
667  QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
668 
676  QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
677 
687  QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
688 
689 
693  QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
694 
698  QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
699 
707  QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
708 
712  QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
713 
722  QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
724 
735  QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
737 
743  QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
744 
753  QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
755 
761  QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
762 
766  QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
767 
772  QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
773 
777  QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
778 
782  QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
783 
787  QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
788 
797  QgsPrintLayout *parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
798 
807  QgsLayoutItem *parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
808 
821  static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
822 
835  static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
836 
849  static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
850 
858  virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
859 
860  private:
861 
862  QgsProcessingProvider *mProvider = nullptr;
865  bool mHasPrepared = false;
866  bool mHasExecuted = false;
867  bool mHasPostProcessed = false;
868  std::unique_ptr< QgsProcessingContext > mLocalContext;
869 
870  bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
871 
872 
873  friend class QgsProcessingProvider;
874  friend class TestQgsProcessing;
875  friend class QgsProcessingModelAlgorithm;
877 
878 #ifdef SIP_RUN
880 #endif
881 
882 };
883 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
884 
885 
886 
887 
911 {
912  public:
913 
918 
919  QgsProcessingAlgorithm::Flags flags() const override;
920 
944  virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
945 
946  protected:
947 
948  void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
949 
955  virtual QString outputName() const = 0;
956 
962  virtual QList<int> inputLayerTypes() const;
963 
968  virtual QgsProcessing::SourceType outputLayerType() const;
969 
973  virtual QgsProcessingFeatureSource::Flag sourceFlags() const;
974 
980  virtual QgsFeatureSink::SinkFlags sinkFlags() const;
981 
989  virtual QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const;
990 
1001  virtual QgsFields outputFields( const QgsFields &inputFields ) const;
1002 
1011  virtual QgsCoordinateReferenceSystem outputCrs( const QgsCoordinateReferenceSystem &inputCrs ) const;
1012 
1018  virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
1019 
1024  QgsCoordinateReferenceSystem sourceCrs() const;
1025 
1026 
1027  QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
1028 
1033  virtual QgsFeatureRequest request() const;
1034 
1043  bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1044 
1050  void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1051 
1052  private:
1053 
1054  std::unique_ptr< QgsProcessingFeatureSource > mSource;
1055 
1056 };
1057 
1058 // clazy:excludeall=qstring-allocations
1059 
1060 #endif // QGSPROCESSINGALGORITHM_H
1061 
1062 
#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:41
Base class for all map layer types.
Definition: qgsmaplayer.h:78
Base class for providing feedback from a processing algorithm.
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns an ordered list of parameter definitions utilized by the algorithm.
Base class for graphical items within a QgsLayout.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
virtual QString group() const
Returns the name of the group this algorithm belongs to.
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:111
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.
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
Base class for the definition of processing outputs.
QList< const QgsProcessingOutputDefinition *> QgsProcessingOutputDefinitions
List of processing parameters.
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
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
const QgsCoordinateReferenceSystem & outputCrs
#define SIP_THROW(name)
Definition: qgis_sip.h:177
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:90
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
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.