QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
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"
26#include "qgsfeaturesource.h"
27#include "qgsprocessingutils.h"
28#include <QString>
29#include <QVariant>
30#include <QIcon>
31
34class QgsFeatureSink;
35class QgsProcessingModelAlgorithm;
37class QgsMeshLayer;
39
40#ifdef SIP_RUN
41% ModuleHeaderCode
43% End
44#endif
45
52class CORE_EXPORT QgsProcessingAlgorithm
53{
54
55#ifdef SIP_RUN
57 if ( dynamic_cast< QgsProcessingModelAlgorithm * >( sipCpp ) != NULL )
58 sipType = sipType_QgsProcessingModelAlgorithm;
59 else if ( dynamic_cast< QgsProcessingFeatureBasedAlgorithm * >( sipCpp ) != NULL )
60 sipType = sipType_QgsProcessingFeatureBasedAlgorithm;
61 else
62 sipType = sipType_QgsProcessingAlgorithm;
64#endif
65
66 public:
67
69 enum Flag
70 {
71 FlagHideFromToolbox = 1 << 1,
72 FlagHideFromModeler = 1 << 2,
73 FlagSupportsBatch = 1 << 3,
74 FlagCanCancel = 1 << 4,
75 FlagRequiresMatchingCrs = 1 << 5,
76 FlagNoThreading = 1 << 6,
77 FlagDisplayNameIsLiteral = 1 << 7,
78 FlagSupportsInPlaceEdits = 1 << 8,
79 FlagKnownIssues = 1 << 9,
80 FlagCustomException = 1 << 10,
81 FlagPruneModelBranchesBasedOnAlgorithmResults = 1 << 11,
82 FlagSkipGenericModelLogging = 1 << 12,
83 FlagNotAvailableInStandaloneTool = 1 << 13,
84 FlagRequiresProject = 1 << 14,
85 FlagDeprecated = FlagHideFromToolbox | FlagHideFromModeler,
86 };
87 Q_DECLARE_FLAGS( Flags, Flag )
88
89
97
98 virtual ~QgsProcessingAlgorithm();
99
103 QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
104
105 /*
106 * IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
107 * the case.
108 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
109 *
110 * "
111 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
112 * In this case it isn't because it has already been seen when being returned by createInstance()
113 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
114 * by Python so the /Factory/ on create() would be correct.)
115 *
116 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
117 * "
118 */
119
137 QgsProcessingAlgorithm *create( const QVariantMap &configuration = QVariantMap() ) const SIP_THROW( QgsProcessingException ) SIP_TRANSFERBACK;
138
148 virtual QString name() const = 0;
149
156 QString id() const;
157
168 virtual QString displayName() const = 0;
169
175 virtual QString shortDescription() const;
176
181 virtual QStringList tags() const { return QStringList(); }
182
189 virtual QString shortHelpString() const;
190
198 Q_DECL_DEPRECATED virtual QString helpString() const SIP_DEPRECATED;
199
205 virtual QString helpUrl() const;
206
211 virtual QIcon icon() const;
212
217 virtual QString svgIconPath() const;
218
225 virtual QString group() const { return QString(); }
226
234 virtual QString groupId() const { return QString(); }
235
240 virtual Flags flags() const;
241
248 virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
249
257 virtual bool checkParameterValues( const QVariantMap &parameters,
258 QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
259
267 virtual QVariantMap preprocessParameters( const QVariantMap &parameters );
268
272 QgsProcessingProvider *provider() const;
273
281
287 const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
288
293 int countVisibleParameters() const;
294
300 QgsProcessingParameterDefinitions destinationParameterDefinitions() const;
301
308
314 const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const;
315
319 bool hasHtmlOutputs() const;
320
330
350
366 virtual QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
367 const QVariantMap &parameters,
368 QgsProcessingContext &context,
369 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const;
370
391 QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap(),
392 bool catchExceptions = true ) const SIP_THROW( QgsProcessingException );
393
404 bool prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
405
417 QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
418
427 QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback );
428
435 virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
436
443 virtual QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
444 QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
445
452 virtual bool validateInputCrs( const QVariantMap &parameters,
453 QgsProcessingContext &context ) const;
454
462 virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
463
478 virtual QString asQgisProcessCommand( const QVariantMap &parameters, QgsProcessingContext &context, bool &ok SIP_OUT ) const;
479
485 virtual QVariantMap asMap( const QVariantMap &parameters, QgsProcessingContext &context ) const;
486
490 void setProvider( QgsProcessingProvider *provider );
491
492 protected:
493
499 virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
500
519 virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
520
538 bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true );
539
544 void removeParameter( const QString &name );
545
559 bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER );
560
591 virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
592
622 virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
623
651 virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
652
656 QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
657
661 QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
662
666 double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
667
671 int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
672
677 QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
678
682 int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
683
687 QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
688
693 QString parameterAsEnumString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
694
699 QStringList parameterAsEnumStrings( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
700
704 bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
705
710 bool parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
711
732 QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
733 const QgsFields &fields, Qgis::WkbType geometryType = Qgis::WkbType::NoGeometry, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(), QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags(), const QVariantMap &createOptions = QVariantMap(), const QStringList &datasourceOptions = QStringList(), const QStringList &layerOptions = QStringList() ) const SIP_THROW( QgsProcessingException ) SIP_FACTORY;
734
743 QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
744
760 QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
761 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr ) const;
762
792 QString parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap &parameters, const QString &name,
793 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr, QString *layerName SIP_OUT = nullptr ) const;
794
802 QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
803
811 QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
812
822 QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
823
827 QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
828
832 QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
833
841 QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
842
846 QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
847
856 QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
858
869 QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
871
877 QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
878
887 QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
889
895 QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
896
905 QgsGeometry parameterAsGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
907
913 QgsCoordinateReferenceSystem parameterAsGeometryCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
914
918 QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
919
924 QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
925
930 QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
931
937 QStringList parameterAsFileList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
938
942 QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
943
949 Q_DECL_DEPRECATED QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_DEPRECATED;
950
956 QStringList parameterAsStrings( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
957
966 QgsPrintLayout *parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
967
976 QgsLayoutItem *parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
977
983 QColor parameterAsColor( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
984
990 QString parameterAsConnectionName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
991
997 QString parameterAsSchema( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
998
1004 QString parameterAsDatabaseTableName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1005
1011 QDateTime parameterAsDateTime( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1012
1023 QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
1024
1038 QgsAnnotationLayer *parameterAsAnnotationLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1039
1053 static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
1054
1068 static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
1069
1083 static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
1084
1098 static QString invalidPointCloudError( const QVariantMap &parameters, const QString &name );
1099
1111 static QString writeFeatureError( QgsFeatureSink *sink, const QVariantMap &parameters, const QString &name );
1112
1120 virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
1121
1122 private:
1123
1124 QgsProcessingProvider *mProvider = nullptr;
1127 bool mHasPrepared = false;
1128 bool mHasExecuted = false;
1129 bool mHasPostProcessed = false;
1130 std::unique_ptr< QgsProcessingContext > mLocalContext;
1131
1132 bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
1133
1134
1136 friend class TestQgsProcessing;
1137 friend class QgsProcessingModelAlgorithm;
1139
1140#ifdef SIP_RUN
1142#endif
1143
1144};
1145Q_DECLARE_OPERATORS_FOR_FLAGS( QgsProcessingAlgorithm::Flags )
1146
1147
1148
1149
1173{
1174 public:
1175
1180
1181 QgsProcessingAlgorithm::Flags flags() const override;
1182
1206 virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
1207
1208 protected:
1209
1210 void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
1211
1219 virtual QString inputParameterName() const;
1220
1228 virtual QString inputParameterDescription() const;
1229
1235 virtual QString outputName() const = 0;
1236
1242 virtual QList<int> inputLayerTypes() const;
1243
1248 virtual QgsProcessing::SourceType outputLayerType() const;
1249
1253 virtual QgsProcessingFeatureSource::Flag sourceFlags() const;
1254
1260 virtual QgsFeatureSink::SinkFlags sinkFlags() const;
1261
1269 virtual Qgis::WkbType outputWkbType( Qgis::WkbType inputWkbType ) const;
1270
1281 virtual QgsFields outputFields( const QgsFields &inputFields ) const;
1282
1292
1298 virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
1299
1304 QgsCoordinateReferenceSystem sourceCrs() const;
1305
1306
1307 QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
1308
1313 virtual QgsFeatureRequest request() const;
1314
1323 bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1324
1330 void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1331
1332 QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
1333 const QVariantMap &parameters,
1334 QgsProcessingContext &context,
1335 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const override;
1336
1337 private:
1338
1339 std::unique_ptr< QgsProcessingFeatureSource > mSource;
1340
1341};
1342
1343// clazy:excludeall=qstring-allocations
1344
1345#endif // QGSPROCESSINGALGORITHM_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:182
@ Unknown
Unknown.
Represents a map layer containing a set of georeferenced annotations, e.g.
This class represents a coordinate reference system (CRS).
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
This class wraps a request for features to a vector layer (or directly its vector data provider).
An interface for objects which accept features via addFeature(s) methods.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
A geometry is the spatial representation of a feature.
Base class for graphical items within a QgsLayout.
Base class for all map layer types.
Definition qgsmaplayer.h:74
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Represents a map layer supporting display of point clouds.
A class to represent a 2D point.
Definition qgspointxy.h:59
Print layout, a QgsLayout subclass for static or atlas-based layouts.
A configuration widget for processing algorithms allows providing additional configuration options di...
Abstract base class for processing algorithms.
QgsProcessingOutputDefinitions outputDefinitions() const
Returns an ordered list of output definitions utilized by the algorithm.
PropertyAvailability
Property availability, used for QgsProcessingAlgorithm::VectorProperties in order to determine if pro...
@ NotAvailable
Properties are not available.
@ Available
Properties are available.
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
QgsProcessingParameterDefinitions parameterDefinitions() const
Returns an ordered list of parameter definitions utilized by the algorithm.
Flag
Flags indicating how and when an algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
An abstract QgsProcessingAlgorithm base class for processing algorithms which operate "feature-by-fea...
virtual QgsFeatureList processFeature(const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Processes an individual input feature from the source.
QgsProcessingFeatureBasedAlgorithm()=default
Constructor for QgsProcessingFeatureBasedAlgorithm.
virtual QString outputName() const =0
Returns the translated, user visible name for any layers created by this algorithm.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Base class for providing feedback from a processing algorithm.
Base class for the definition of processing outputs.
Base class for the definition of processing parameters.
Abstract base class for processing providers.
A sort/filter proxy model for providers and algorithms shown within the Processing toolbox,...
Contains enumerations and other constants for use in processing algorithms and parameters.
SourceType
Data source types enum.
Represents a raster layer.
A rectangle specified with double values.
Represents a vector layer which manages a vector based data sets.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:186
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_VIRTUALERRORHANDLER(name)
Definition qgis_sip.h:191
#define SIP_THROW(name,...)
Definition qgis_sip.h:198
#define SIP_END
Definition qgis_sip.h:203
QList< QgsFeature > QgsFeatureList
Definition qgsfeature.h:920
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
const QgsCoordinateReferenceSystem & outputCrs
const QgsCoordinateReferenceSystem & crs
Properties of a vector source or sink used in an algorithm.
QgsCoordinateReferenceSystem crs
Coordinate Reference System.