QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  FlagCustomException = 1 << 10,
80  FlagPruneModelBranchesBasedOnAlgorithmResults = 1 << 11,
81  FlagSkipGenericModelLogging = 1 << 12,
82  FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler,
83  };
84  Q_DECLARE_FLAGS( Flags, Flag )
85 
86 
93  QgsProcessingAlgorithm() = default;
94 
95  virtual ~QgsProcessingAlgorithm();
96 
98  QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other ) = delete;
100  QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
101 
102  /*
103  * IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
104  * the case.
105  * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
106  *
107  * "
108  * /Factory/ is used when the instance returned is guaranteed to be new to Python.
109  * In this case it isn't because it has already been seen when being returned by createInstance()
110  * (However for a different sub-class implemented in C++ then it would be the first time it was seen
111  * by Python so the /Factory/ on create() would be correct.)
112  *
113  * You might try using /TransferBack/ on create() instead - that might be the best compromise.
114  * "
115  */
116 
134  QgsProcessingAlgorithm *create( const QVariantMap &configuration = QVariantMap() ) const SIP_THROW( QgsProcessingException ) SIP_TRANSFERBACK;
135 
145  virtual QString name() const = 0;
146 
153  QString id() const;
154 
165  virtual QString displayName() const = 0;
166 
172  virtual QString shortDescription() const;
173 
178  virtual QStringList tags() const { return QStringList(); }
179 
186  virtual QString shortHelpString() const;
187 
195  Q_DECL_DEPRECATED virtual QString helpString() const SIP_DEPRECATED;
196 
202  virtual QString helpUrl() const;
203 
208  virtual QIcon icon() const;
209 
214  virtual QString svgIconPath() const;
215 
222  virtual QString group() const { return QString(); }
223 
231  virtual QString groupId() const { return QString(); }
232 
237  virtual Flags flags() const;
238 
245  virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
246 
254  virtual bool checkParameterValues( const QVariantMap &parameters,
255  QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
256 
264  virtual QVariantMap preprocessParameters( const QVariantMap &parameters );
265 
269  QgsProcessingProvider *provider() const;
270 
278 
284  const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
285 
290  int countVisibleParameters() const;
291 
297  QgsProcessingParameterDefinitions destinationParameterDefinitions() const;
298 
305 
311  const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const;
312 
316  bool hasHtmlOutputs() const;
317 
323  {
326  };
327 
334  {
337 
340 
343 
346  };
347 
363  virtual QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
364  const QVariantMap &parameters,
365  QgsProcessingContext &context,
366  const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const;
367 
385  QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap() ) const;
386 
397  bool prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
398 
410  QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
411 
420  QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback );
421 
428  virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
429 
436  virtual QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
437  QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
438 
445  virtual bool validateInputCrs( const QVariantMap &parameters,
446  QgsProcessingContext &context ) const;
447 
455  virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
456 
460  void setProvider( QgsProcessingProvider *provider );
461 
462  protected:
463 
469  virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
470 
489  virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
490 
508  bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true );
509 
514  void removeParameter( const QString &name );
515 
529  bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER );
530 
561  virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
562 
592  virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
593 
621  virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
622 
626  QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
627 
631  QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
632 
636  double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
637 
641  int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
642 
647  QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
648 
652  int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
653 
657  QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
658 
662  bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
663 
668  bool parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
669 
686  QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
687  const QgsFields &fields, QgsWkbTypes::Type geometryType = QgsWkbTypes::NoGeometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags() ) const SIP_THROW( QgsProcessingException ) SIP_FACTORY;
688 
697  QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
698 
714  QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
715  QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr );
716 
746  QString parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap &parameters, const QString &name,
747  QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr, QString *layerName SIP_OUT = nullptr );
748 
756  QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
757 
765  QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
766 
776  QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
777 
778 
782  QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
783 
787  QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
788 
796  QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
797 
801  QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
802 
811  QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
813 
824  QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
826 
832  QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
833 
842  QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
844 
850  QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
851 
855  QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
856 
861  QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
862 
866  QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
867 
873  QStringList parameterAsFileList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
874 
878  QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
879 
883  QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
884 
893  QgsPrintLayout *parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
894 
903  QgsLayoutItem *parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
904 
910  QColor parameterAsColor( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
911 
917  QString parameterAsConnectionName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
918 
924  QString parameterAsSchema( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
925 
931  QString parameterAsDatabaseTableName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
932 
938  QDateTime parameterAsDateTime( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
939 
940 
953  static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
954 
967  static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
968 
981  static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
982 
990  virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
991 
992  private:
993 
994  QgsProcessingProvider *mProvider = nullptr;
997  bool mHasPrepared = false;
998  bool mHasExecuted = false;
999  bool mHasPostProcessed = false;
1000  std::unique_ptr< QgsProcessingContext > mLocalContext;
1001 
1002  bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
1003 
1004 
1006  friend class TestQgsProcessing;
1007  friend class QgsProcessingModelAlgorithm;
1009 
1010 #ifdef SIP_RUN
1012 #endif
1013 
1014 };
1015 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
1016 
1017 
1018 
1019 
1043 {
1044  public:
1045 
1050 
1051  QgsProcessingAlgorithm::Flags flags() const override;
1052 
1076  virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
1077 
1078  protected:
1079 
1080  void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
1081 
1089  virtual QString inputParameterName() const;
1090 
1098  virtual QString inputParameterDescription() const;
1099 
1105  virtual QString outputName() const = 0;
1106 
1112  virtual QList<int> inputLayerTypes() const;
1113 
1118  virtual QgsProcessing::SourceType outputLayerType() const;
1119 
1123  virtual QgsProcessingFeatureSource::Flag sourceFlags() const;
1124 
1130  virtual QgsFeatureSink::SinkFlags sinkFlags() const;
1131 
1139  virtual QgsWkbTypes::Type outputWkbType( QgsWkbTypes::Type inputWkbType ) const;
1140 
1151  virtual QgsFields outputFields( const QgsFields &inputFields ) const;
1152 
1161  virtual QgsCoordinateReferenceSystem outputCrs( const QgsCoordinateReferenceSystem &inputCrs ) const;
1162 
1168  virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
1169 
1174  QgsCoordinateReferenceSystem sourceCrs() const;
1175 
1176 
1177  QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
1178 
1183  virtual QgsFeatureRequest request() const;
1184 
1193  bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1194 
1200  void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1201 
1202  QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
1203  const QVariantMap &parameters,
1204  QgsProcessingContext &context,
1205  const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const override;
1206 
1207  private:
1208 
1209  std::unique_ptr< QgsProcessingFeatureSource > mSource;
1210 
1211 };
1212 
1213 // clazy:excludeall=qstring-allocations
1214 
1215 #endif // QGSPROCESSINGALGORITHM_H
1216 
1217 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
outputCrs
const QgsCoordinateReferenceSystem & outputCrs
Definition: qgswfsgetfeature.cpp:115
QgsProcessingAlgorithm::VectorProperties::fields
QgsFields fields
Fields.
Definition: qgsprocessingalgorithm.h:336
QgsProcessingAlgorithm::VectorProperties::crs
QgsCoordinateReferenceSystem crs
Coordinate Reference System.
Definition: qgsprocessingalgorithm.h:342
QgsProcessingFeedback
Definition: qgsprocessingfeedback.h:37
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFields
Definition: qgsfields.h:44
QgsProcessingProvider
Definition: qgsprocessingprovider.h:34
QgsProcessingAlgorithm::NotAvailable
@ NotAvailable
Properties are not available.
Definition: qgsprocessingalgorithm.h:324
qgis.h
QgsProcessingAlgorithm::groupId
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
Definition: qgsprocessingalgorithm.h:231
QgsProcessingParameterDefinition
Definition: qgsprocessingparameters.h:330
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsProcessingAlgorithm::PropertyAvailability
PropertyAvailability
Property availability, used for QgsProcessingAlgorithm::VectorProperties in order to determine if pro...
Definition: qgsprocessingalgorithm.h:322
QgsRectangle
Definition: qgsrectangle.h:41
QgsProcessingAlgorithm::Available
@ Available
Properties are available.
Definition: qgsprocessingalgorithm.h:325
QgsProcessingParameterDefinitions
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Definition: qgsprocessingparameters.h:732
QgsProcessingFeatureBasedAlgorithm
Definition: qgsprocessingalgorithm.h:1042
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsProcessingAlgorithm::outputDefinitions
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
Definition: qgsprocessingalgorithm.h:304
QgsProcessingOutputDefinition
Definition: qgsprocessingoutputs.h:41
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
QgsPrintLayout
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Definition: qgsprintlayout.h:30
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsProcessingToolboxProxyModel
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,...
Definition: qgsprocessingtoolboxmodel.h:417
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:70
QgsProcessingAlgorithm::Flag
Flag
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition: qgsprocessingalgorithm.h:68
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
QgsMeshLayer
Definition: qgsmeshlayer.h:94
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
qgsfeaturesource.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsPointXY
Definition: qgspointxy.h:43
qgsprocessingoutputs.h
qgsprocessingutils.h
qgsprocessingparameters.h
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
QgsGeometry
Definition: qgsgeometry.h:122
QgsProcessingAlgorithm::VectorProperties
Properties of a vector source or sink used in an algorithm.
Definition: qgsprocessingalgorithm.h:333
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsProcessingFeatureSource
Definition: qgsprocessingutils.h:468
qgsprocessingcontext.h
QgsProcessingAlgorithm::parameterDefinitions
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns an ordered list of parameter definitions utilized by the algorithm.
Definition: qgsprocessingalgorithm.h:277
QgsFeature
Definition: qgsfeature.h:55
QgsWkbTypes
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
QgsProcessingOutputDefinitions
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
Definition: qgsprocessingoutputs.h:143
QgsProcessingAlgorithmConfigurationWidget
A configuration widget for processing algorithms allows providing additional configuration options di...
Definition: qgsprocessingalgorithmconfigurationwidget.h:40
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
QgsProcessing::SourceType
SourceType
Data source types enum.
Definition: qgsprocessing.h:44
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink
Definition: qgsfeaturesink.h:33
SIP_VIRTUALERRORHANDLER
#define SIP_VIRTUALERRORHANDLER(name)
Definition: qgis_sip.h:177