QGIS API Documentation 3.37.0-Master (df6056bf1be)
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 "qgsprocessingutils.h"
27#include <QString>
28#include <QVariant>
29#include <QIcon>
30
33class QgsFeatureSink;
34class QgsProcessingModelAlgorithm;
36class QgsMeshLayer;
38
39#ifdef SIP_RUN
40% ModuleHeaderCode
42% End
43#endif
44
50class 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;
62#endif
63
64 public:
65
74
76
81
82 /*
83 * IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
84 * the case.
85 * As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
86 *
87 * "
88 * /Factory/ is used when the instance returned is guaranteed to be new to Python.
89 * In this case it isn't because it has already been seen when being returned by createInstance()
90 * (However for a different sub-class implemented in C++ then it would be the first time it was seen
91 * by Python so the /Factory/ on create() would be correct.)
92 *
93 * You might try using /TransferBack/ on create() instead - that might be the best compromise.
94 * "
95 */
96
114 QgsProcessingAlgorithm *create( const QVariantMap &configuration = QVariantMap() ) const SIP_THROW( QgsProcessingException ) SIP_TRANSFERBACK;
115
125 virtual QString name() const = 0 SIP_HOLDGIL;
126
133 QString id() const SIP_HOLDGIL;
134
145 virtual QString displayName() const = 0 SIP_HOLDGIL;
146
152 virtual QString shortDescription() const SIP_HOLDGIL;
153
158 virtual QStringList tags() const SIP_HOLDGIL { return QStringList(); }
159
166 virtual QString shortHelpString() const SIP_HOLDGIL;
167
175 Q_DECL_DEPRECATED virtual QString helpString() const SIP_HOLDGIL SIP_DEPRECATED;
176
182 virtual QString helpUrl() const SIP_HOLDGIL;
183
188 virtual QIcon icon() const SIP_HOLDGIL;
189
194 virtual QString svgIconPath() const SIP_HOLDGIL;
195
202 virtual QString group() const SIP_HOLDGIL { return QString(); }
203
211 virtual QString groupId() const SIP_HOLDGIL { return QString(); }
212
217 virtual Qgis::ProcessingAlgorithmFlags flags() const SIP_HOLDGIL;
218
225 virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
226
234 virtual bool checkParameterValues( const QVariantMap &parameters,
235 QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
236
244 virtual QVariantMap preprocessParameters( const QVariantMap &parameters );
245
249 QgsProcessingProvider *provider() const SIP_HOLDGIL;
250
257 QgsProcessingParameterDefinitions parameterDefinitions() const SIP_HOLDGIL { return mParameters; }
258
264 const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const SIP_HOLDGIL;
265
270 int countVisibleParameters() const SIP_HOLDGIL;
271
277 QgsProcessingParameterDefinitions destinationParameterDefinitions() const SIP_HOLDGIL;
278
284 QgsProcessingOutputDefinitions outputDefinitions() const SIP_HOLDGIL { return mOutputs; }
285
291 const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const SIP_HOLDGIL;
292
296 bool hasHtmlOutputs() const SIP_HOLDGIL;
297
298
318
334 virtual QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
335 const QVariantMap &parameters,
336 QgsProcessingContext &context,
337 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const;
338
359 QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap(),
360 bool catchExceptions = true ) const SIP_THROW( QgsProcessingException );
361
372 bool prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
373
385 QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
386
399 QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool runResult = true );
400
407 virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
408
415 virtual QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
416 QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
417
424 virtual bool validateInputCrs( const QVariantMap &parameters,
425 QgsProcessingContext &context ) const;
426
434 virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
435
450 virtual QString asQgisProcessCommand( const QVariantMap &parameters, QgsProcessingContext &context, bool &ok SIP_OUT ) const;
451
457 virtual QVariantMap asMap( const QVariantMap &parameters, QgsProcessingContext &context ) const;
458
462 void setProvider( QgsProcessingProvider *provider ) SIP_HOLDGIL;
463
464 protected:
465
471 virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
472
491 virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
492
510 bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true ) SIP_HOLDGIL;
511
516 void removeParameter( const QString &name ) SIP_HOLDGIL;
517
531 bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER ) SIP_HOLDGIL;
532
563 virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
564
594 virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
595
623 virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
624
628 QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
629
633 QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
634
638 double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
639
643 int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
644
649 QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
650
654 int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
655
659 QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
660
665 QString parameterAsEnumString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
666
671 QStringList parameterAsEnumStrings( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
672
676 bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
677
682 bool parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
683
704 QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
705 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;
706
715 QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
716
732 QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
733 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr ) const;
734
764 QString parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap &parameters, const QString &name,
765 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr, QString *layerName SIP_OUT = nullptr ) const;
766
774 QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
775
783 QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
784
794 QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
795
799 QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
800
804 QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
805
813 QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
814
818 QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
819
828 QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
830
841 QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
843
849 QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
850
859 QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
861
867 QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
868
877 QgsGeometry parameterAsGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
879
885 QgsCoordinateReferenceSystem parameterAsGeometryCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
886
890 QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
891
896 QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
897
902 QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
903
909 QStringList parameterAsFileList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
910
914 QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
915
921 Q_DECL_DEPRECATED QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_DEPRECATED;
922
928 QStringList parameterAsStrings( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
929
938 QgsPrintLayout *parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
939
948 QgsLayoutItem *parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
949
955 QColor parameterAsColor( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
956
962 QString parameterAsConnectionName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
963
969 QString parameterAsSchema( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
970
976 QString parameterAsDatabaseTableName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
977
983 QDateTime parameterAsDateTime( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
984
995 QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
996
1010 QgsAnnotationLayer *parameterAsAnnotationLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1011
1025 static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
1026
1040 static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
1041
1055 static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
1056
1070 static QString invalidPointCloudError( const QVariantMap &parameters, const QString &name );
1071
1083 static QString writeFeatureError( QgsFeatureSink *sink, const QVariantMap &parameters, const QString &name );
1084
1092 virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
1093
1094 private:
1095
1096 QgsProcessingProvider *mProvider = nullptr;
1099 bool mHasPrepared = false;
1100 bool mHasExecuted = false;
1101 bool mHasPostProcessed = false;
1102 std::unique_ptr< QgsProcessingContext > mLocalContext;
1103
1104 bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
1105
1106
1108 friend class TestQgsProcessing;
1109 friend class QgsProcessingModelAlgorithm;
1111 friend class DummyRaiseExceptionAlgorithm;
1112
1113#ifdef SIP_RUN
1115#endif
1116
1117};
1118
1119
1143{
1144 public:
1145
1150
1151 Qgis::ProcessingAlgorithmFlags flags() const override SIP_HOLDGIL;
1152
1176 virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
1177
1178 protected:
1179
1180 void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
1181
1189 virtual QString inputParameterName() const SIP_HOLDGIL;
1190
1198 virtual QString inputParameterDescription() const SIP_HOLDGIL;
1199
1205 virtual QString outputName() const = 0 SIP_HOLDGIL;
1206
1212 virtual QList<int> inputLayerTypes() const SIP_HOLDGIL;
1213
1218 virtual Qgis::ProcessingSourceType outputLayerType() const SIP_HOLDGIL;
1219
1223 virtual Qgis::ProcessingFeatureSourceFlags sourceFlags() const SIP_HOLDGIL;
1224
1230 virtual QgsFeatureSink::SinkFlags sinkFlags() const SIP_HOLDGIL;
1231
1239 virtual Qgis::WkbType outputWkbType( Qgis::WkbType inputWkbType ) const SIP_HOLDGIL;
1240
1251 virtual QgsFields outputFields( const QgsFields &inputFields ) const SIP_HOLDGIL;
1252
1262
1268 virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
1269
1274 QgsCoordinateReferenceSystem sourceCrs() const SIP_HOLDGIL;
1275
1276
1277 QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
1278
1283 virtual QgsFeatureRequest request() const;
1284
1293 bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1294
1300 void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1301
1302 QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
1303 const QVariantMap &parameters,
1304 QgsProcessingContext &context,
1305 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const override;
1306
1307 private:
1308
1309 std::unique_ptr< QgsProcessingFeatureSource > mSource;
1310
1311};
1312
1313// clazy:excludeall=qstring-allocations
1314
1315#endif // QGSPROCESSINGALGORITHM_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
ProcessingPropertyAvailability
Property availability, used for QgsProcessingAlgorithm::VectorProperties in order to determine if pro...
Definition qgis.h:3083
@ NotAvailable
Properties are not available.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3071
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:201
@ 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:58
Container of fields for a vector layer.
Definition qgsfields.h:46
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:75
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:60
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.
virtual QString groupId() const
Returns the unique ID of the group this algorithm belongs to.
QgsProcessingAlgorithm(const QgsProcessingAlgorithm &other)=delete
Algorithms cannot be copied - create() should be used instead.
QgsProcessingAlgorithm & operator=(const QgsProcessingAlgorithm &other)=delete
Algorithms cannot be copied- create() should be used instead.
QgsProcessingAlgorithm()=default
Constructor for QgsProcessingAlgorithm.
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...
QgsProcessingFeatureBasedAlgorithm()=default
Constructor for QgsProcessingFeatureBasedAlgorithm.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
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.
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:191
#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_HOLDGIL
Definition qgis_sip.h:171
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_VIRTUALERRORHANDLER(name)
Definition qgis_sip.h:196
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
#define SIP_END
Definition qgis_sip.h:208
QList< QgsFeature > QgsFeatureList
QList< const QgsProcessingOutputDefinition * > QgsProcessingOutputDefinitions
List of processing parameters.
QList< const QgsProcessingParameterDefinition * > QgsProcessingParameterDefinitions
List of processing parameters.
const QgsCoordinateReferenceSystem & outputCrs
const QgsCoordinateReferenceSystem & crs
Properties of a vector source or sink used in an algorithm.
QgsCoordinateReferenceSystem crs
Coordinate Reference System.