QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 bool addParameter( std::unique_ptr<QgsProcessingParameterDefinition> parameterDefinition, bool createOutput = true ) SIP_SKIP;
535
540 void removeParameter( const QString &name ) SIP_HOLDGIL;
541
555 bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER ) SIP_HOLDGIL;
556
561 bool addOutput( std::unique_ptr<QgsProcessingOutputDefinition> outputDefinition ) SIP_SKIP;
562
593 virtual bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
594
624 virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
625
653 virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
654
658 QString parameterAsString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
659
663 QString parameterAsExpression( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
664
668 double parameterAsDouble( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
669
673 int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
674
679 QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
680
684 int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
685
689 QList<int> parameterAsEnums( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
690
695 QString parameterAsEnumString( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
696
701 QStringList parameterAsEnumStrings( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
702
706 bool parameterAsBool( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
707
712 bool parameterAsBoolean( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
713
734 QgsFeatureSink *parameterAsSink( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
735 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;
736
745 QgsProcessingFeatureSource *parameterAsSource( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
746
762 QString parameterAsCompatibleSourceLayerPath( const QVariantMap &parameters, const QString &name,
763 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr ) const;
764
794 QString parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap &parameters, const QString &name,
795 QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr, QString *layerName SIP_OUT = nullptr ) const;
796
804 QgsMapLayer *parameterAsLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
805
813 QgsRasterLayer *parameterAsRasterLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
814
824 QgsMeshLayer *parameterAsMeshLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
825
829 QString parameterAsOutputLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
830
834 QString parameterAsFileOutput( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
835
843 QgsVectorLayer *parameterAsVectorLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
844
848 QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
849
858 QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
860
871 QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
873
879 QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
880
889 QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
891
897 QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
898
907 QgsGeometry parameterAsGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
909
915 QgsCoordinateReferenceSystem parameterAsGeometryCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
916
920 QString parameterAsFile( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
921
926 QVariantList parameterAsMatrix( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
927
932 QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
933
939 QStringList parameterAsFileList( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
940
944 QList<double> parameterAsRange( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
945
951 Q_DECL_DEPRECATED QStringList parameterAsFields( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const SIP_DEPRECATED;
952
958 QStringList parameterAsStrings( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
959
968 QgsPrintLayout *parameterAsLayout( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
969
978 QgsLayoutItem *parameterAsLayoutItem( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
979
985 QColor parameterAsColor( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
986
992 QString parameterAsConnectionName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
993
999 QString parameterAsSchema( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1000
1006 QString parameterAsDatabaseTableName( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1007
1013 QDateTime parameterAsDateTime( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1014
1025 QgsPointCloudLayer *parameterAsPointCloudLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
1026
1040 QgsAnnotationLayer *parameterAsAnnotationLayer( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
1041
1055 static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
1056
1070 static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
1071
1085 static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
1086
1100 static QString invalidPointCloudError( const QVariantMap &parameters, const QString &name );
1101
1113 static QString writeFeatureError( QgsFeatureSink *sink, const QVariantMap &parameters, const QString &name );
1114
1115 private:
1116
1117 QgsProcessingProvider *mProvider = nullptr;
1120 bool mHasPrepared = false;
1121 bool mHasExecuted = false;
1122 bool mHasPostProcessed = false;
1123 std::unique_ptr< QgsProcessingContext > mLocalContext;
1124
1125 bool createAutoOutputForParameter( const QgsProcessingParameterDefinition *parameter );
1126
1127
1129 friend class TestQgsProcessing;
1130 friend class QgsProcessingModelAlgorithm;
1132 friend class DummyRaiseExceptionAlgorithm;
1133
1134#ifdef SIP_RUN
1136#endif
1137
1138};
1139
1140
1164{
1165 public:
1166
1168
1169 Qgis::ProcessingAlgorithmFlags flags() const override SIP_HOLDGIL;
1170
1194 virtual QgsFeatureList processFeature( const QgsFeature &feature, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
1195
1204 bool supportInPlaceEdit( const QgsMapLayer *layer ) const override;
1205
1213 virtual QString inputParameterName() const SIP_HOLDGIL;
1214
1222 virtual QString inputParameterDescription() const SIP_HOLDGIL;
1223
1224 protected:
1225
1226 void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
1227
1233 virtual QString outputName() const = 0 SIP_HOLDGIL;
1234
1240 virtual QList<int> inputLayerTypes() const SIP_HOLDGIL;
1241
1246 virtual Qgis::ProcessingSourceType outputLayerType() const SIP_HOLDGIL;
1247
1251 virtual Qgis::ProcessingFeatureSourceFlags sourceFlags() const SIP_HOLDGIL;
1252
1258 virtual QgsFeatureSink::SinkFlags sinkFlags() const SIP_HOLDGIL;
1259
1267 virtual Qgis::WkbType outputWkbType( Qgis::WkbType inputWkbType ) const SIP_HOLDGIL;
1268
1279 virtual QgsFields outputFields( const QgsFields &inputFields ) const SIP_HOLDGIL;
1280
1290
1296 virtual void initParameters( const QVariantMap &configuration = QVariantMap() );
1297
1302 QgsCoordinateReferenceSystem sourceCrs() const SIP_HOLDGIL;
1303
1304
1305 QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override SIP_THROW( QgsProcessingException );
1306
1311 virtual QgsFeatureRequest request() const;
1312
1318 void prepareSource( const QVariantMap &parameters, QgsProcessingContext &context );
1319
1320 QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
1321 const QVariantMap &parameters,
1322 QgsProcessingContext &context,
1323 const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const override;
1324
1325 private:
1326
1327 std::unique_ptr< QgsProcessingFeatureSource > mSource;
1328
1329};
1330
1331// clazy:excludeall=qstring-allocations
1332
1333#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:3424
@ 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:3392
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_SKIP
Definition qgis_sip.h:126
#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.