QGIS API Documentation 3.41.0-Master (3440c17df1d)
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
191 virtual Qgis::ProcessingAlgorithmDocumentationFlags documentationFlags() const SIP_HOLDGIL;
192
197 virtual QIcon icon() const SIP_HOLDGIL;
198
203 virtual QString svgIconPath() const SIP_HOLDGIL;
204
211 virtual QString group() const SIP_HOLDGIL { return QString(); }
212
220 virtual QString groupId() const SIP_HOLDGIL { return QString(); }
221
226 virtual Qgis::ProcessingAlgorithmFlags flags() const SIP_HOLDGIL;
227
234 virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
235
243 virtual bool checkParameterValues( const QVariantMap &parameters,
244 QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
245
253 virtual QVariantMap preprocessParameters( const QVariantMap &parameters );
254
258 QgsProcessingProvider *provider() const SIP_HOLDGIL;
259
266 QgsProcessingParameterDefinitions parameterDefinitions() const SIP_HOLDGIL { return mParameters; }
267
273 const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const SIP_HOLDGIL;
274
279 int countVisibleParameters() const SIP_HOLDGIL;
280
286 QgsProcessingParameterDefinitions destinationParameterDefinitions() const SIP_HOLDGIL;
287
293 QgsProcessingOutputDefinitions outputDefinitions() const SIP_HOLDGIL { return mOutputs; }
294
300 const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const SIP_HOLDGIL;
301
305 bool hasHtmlOutputs() const SIP_HOLDGIL;
306
307
327
343 virtual QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
344 const QVariantMap &parameters,
345 QgsProcessingContext &context,
346 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const;
347
368 QVariantMap run( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap(),
369 bool catchExceptions = true ) const SIP_THROW( QgsProcessingException );
370
381 bool prepare( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
382
394 QVariantMap runPrepared( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
395
408 QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool runResult = true );
409
416 virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
417
424 virtual QgsExpressionContext createExpressionContext( const QVariantMap &parameters,
425 QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
426
433 virtual bool validateInputCrs( const QVariantMap &parameters,
434 QgsProcessingContext &context ) const;
435
443 virtual QString asPythonCommand( const QVariantMap &parameters, QgsProcessingContext &context ) const;
444
459 virtual QString asQgisProcessCommand( const QVariantMap &parameters, QgsProcessingContext &context, bool &ok SIP_OUT ) const;
460
466 virtual QVariantMap asMap( const QVariantMap &parameters, QgsProcessingContext &context ) const;
467
471 void setProvider( QgsProcessingProvider *provider ) SIP_HOLDGIL;
472
480 virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
481
482 protected:
483
489 virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
490
509 virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
510
528 bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true ) SIP_HOLDGIL;
529
534 void removeParameter( const QString &name ) SIP_HOLDGIL;
535
549 bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER ) SIP_HOLDGIL;
550
581 virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
582
612 virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
613
641 virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
642
646 QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
647
651 QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
652
656 double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
657
661 int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
662
667 QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
668
672 int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
673
677 QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
678
683 QString parameterAsEnumString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
684
689 QStringList parameterAsEnumStrings( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
690
694 bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
695
700 bool parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
701
722 QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
723 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;
724
733 QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
734
750 QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
751 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr ) const;
752
782 QString parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap &parameters, const QString &name,
783 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr, QString *layerName SIP_OUT = nullptr ) const;
784
792 QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
793
801 QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
802
812 QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
813
817 QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
818
822 QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
823
831 QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
832
836 QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
837
846 QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
848
859 QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
861
867 QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
868
877 QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
879
885 QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
886
895 QgsGeometry parameterAsGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
897
903 QgsCoordinateReferenceSystem parameterAsGeometryCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
904
908 QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
909
914 QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
915
920 QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
921
927 QStringList parameterAsFileList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
928
932 QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
933
939 Q_DECL_DEPRECATED QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_DEPRECATED;
940
946 QStringList parameterAsStrings( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
947
956 QgsPrintLayout *parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
957
966 QgsLayoutItem *parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
967
973 QColor parameterAsColor( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
974
980 QString parameterAsConnectionName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
981
987 QString parameterAsSchema( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
988
994 QString parameterAsDatabaseTableName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
995
1001 QDateTime parameterAsDateTime( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1002
1013 QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
1014
1028 QgsAnnotationLayer *parameterAsAnnotationLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1029
1043 static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
1044
1058 static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
1059
1073 static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
1074
1088 static QString invalidPointCloudError( const QVariantMap &parameters, const QString &name );
1089
1101 static QString writeFeatureError( QgsFeatureSink *sink, const QVariantMap &parameters, const QString &name );
1102
1103 private:
1104
1105 QgsProcessingProvider *mProvider = nullptr;
1108 bool mHasPrepared = false;
1109 bool mHasExecuted = false;
1110 bool mHasPostProcessed = false;
1111 std::unique_ptr< QgsProcessingContext > mLocalContext;
1112
1113 bool createAutoOutputForParameter( QgsProcessingParameterDefinition *parameter );
1114
1115
1117 friend class TestQgsProcessing;
1118 friend class QgsProcessingModelAlgorithm;
1120 friend class DummyRaiseExceptionAlgorithm;
1121
1122#ifdef SIP_RUN
1124#endif
1125
1126};
1127
1128
1152{
1153 public:
1154
1156
1157 Qgis::ProcessingAlgorithmFlags flags() const override SIP_HOLDGIL;
1158
1182 virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
1183
1192 bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1193
1201 virtual QString inputParameterName() const SIP_HOLDGIL;
1202
1210 virtual QString inputParameterDescription() const SIP_HOLDGIL;
1211
1212 protected:
1213
1214 void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
1215
1221 virtual QString outputName() const = 0 SIP_HOLDGIL;
1222
1228 virtual QList<int> inputLayerTypes() const SIP_HOLDGIL;
1229
1234 virtual Qgis::ProcessingSourceType outputLayerType() const SIP_HOLDGIL;
1235
1239 virtual Qgis::ProcessingFeatureSourceFlags sourceFlags() const SIP_HOLDGIL;
1240
1246 virtual QgsFeatureSink::SinkFlags sinkFlags() const SIP_HOLDGIL;
1247
1255 virtual Qgis::WkbType outputWkbType( Qgis::WkbType inputWkbType ) const SIP_HOLDGIL;
1256
1267 virtual QgsFields outputFields( const QgsFields &inputFields ) const SIP_HOLDGIL;
1268
1278
1284 virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
1285
1290 QgsCoordinateReferenceSystem sourceCrs() const SIP_HOLDGIL;
1291
1292
1293 QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
1294
1299 virtual QgsFeatureRequest request() const;
1300
1306 void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1307
1308 QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
1309 const QVariantMap &parameters,
1310 QgsProcessingContext &context,
1311 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const override;
1312
1313 private:
1314
1315 std::unique_ptr< QgsProcessingFeatureSource > mSource;
1316
1317};
1318
1319// clazy:excludeall=qstring-allocations
1320
1321#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:3379
@ 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:3347
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ 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:76
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...
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.