-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsprocessingalgorithm.h
1321 lines (1168 loc) · 60.5 KB
/
qgsprocessingalgorithm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsprocessingalgorithm.h
------------------------
begin : December 2016
copyright : (C) 2016 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSPROCESSINGALGORITHM_H
#define QGSPROCESSINGALGORITHM_H
#include "qgis_core.h"
#include "qgis.h"
#include "qgsprocessingparameters.h"
#include "qgsprocessingoutputs.h"
#include "qgsprocessingcontext.h"
#include "qgsprocessingutils.h"
#include <QString>
#include <QVariant>
#include <QIcon>
class QgsProcessingProvider;
class QgsProcessingFeedback;
class QgsFeatureSink;
class QgsProcessingModelAlgorithm;
class QgsProcessingAlgorithmConfigurationWidget;
class QgsMeshLayer;
class QgsPointCloudLayer;
#ifdef SIP_RUN
% ModuleHeaderCode
#include <qgsprocessingmodelalgorithm.h>
% End
#endif
/**
* \class QgsProcessingAlgorithm
* \ingroup core
* \brief Abstract base class for processing algorithms.
*/
class CORE_EXPORT QgsProcessingAlgorithm
{
#ifdef SIP_RUN
SIP_CONVERT_TO_SUBCLASS_CODE
if ( dynamic_cast< QgsProcessingModelAlgorithm * >( sipCpp ) != NULL )
sipType = sipType_QgsProcessingModelAlgorithm;
else if ( dynamic_cast< QgsProcessingFeatureBasedAlgorithm * >( sipCpp ) != NULL )
sipType = sipType_QgsProcessingFeatureBasedAlgorithm;
else
sipType = sipType_QgsProcessingAlgorithm;
SIP_END
#endif
public:
/**
* Constructor for QgsProcessingAlgorithm.
*
* initAlgorithm() should be called after creating an algorithm to ensure it can correctly configure
* its parameterDefinitions() and outputDefinitions(). Alternatively, calling create() will return
* a pre-initialized copy of the algorithm.
*/
QgsProcessingAlgorithm() = default;
virtual ~QgsProcessingAlgorithm();
//! Algorithms cannot be copied - create() should be used instead
QgsProcessingAlgorithm( const QgsProcessingAlgorithm &other ) = delete;
//! Algorithms cannot be copied- create() should be used instead
QgsProcessingAlgorithm &operator=( const QgsProcessingAlgorithm &other ) = delete;
/*
* IMPORTANT: While it seems like /Factory/ would be the correct annotation here, that's not
* the case.
* As per Phil Thomson's advice on https://www.riverbankcomputing.com/pipermail/pyqt/2017-July/039450.html:
*
* "
* /Factory/ is used when the instance returned is guaranteed to be new to Python.
* In this case it isn't because it has already been seen when being returned by createInstance()
* (However for a different sub-class implemented in C++ then it would be the first time it was seen
* by Python so the /Factory/ on create() would be correct.)
*
* You might try using /TransferBack/ on create() instead - that might be the best compromise.
* "
*/
/**
* Creates a copy of the algorithm, ready for execution.
*
* This method returns a new, preinitialized copy of the algorithm, ready for
* executing.
*
* The \a configuration argument allows passing of a map of configuration settings
* to the algorithm, allowing it to dynamically adjust its initialized parameters
* and outputs according to this configuration. This is generally used only for
* algorithms in a model, allowing them to adjust their behavior at run time
* according to some user configuration.
*
* Raises a QgsProcessingException if a new algorithm instance could not be created,
* e.g. if there is an issue with the subclass' createInstance() method.
*
* \see initAlgorithm()
*/
QgsProcessingAlgorithm *create( const QVariantMap &configuration = QVariantMap() ) const SIP_THROW( QgsProcessingException ) SIP_TRANSFERBACK;
/**
* Returns the algorithm name, used for identifying the algorithm. This string
* should be fixed for the algorithm, and must not be localised. The name should
* be unique within each provider. Names should contain lowercase alphanumeric characters
* only and no spaces or other formatting characters.
* \see displayName()
* \see group()
* \see tags()
*/
virtual QString name() const = 0 SIP_HOLDGIL;
/**
* Returns the unique ID for the algorithm, which is a combination of the algorithm
* provider's ID and the algorithms unique name (e.g. "qgis:mergelayers" ).
* \see name()
* \see provider()
*/
QString id() const SIP_HOLDGIL;
/**
* Returns the translated algorithm name, which should be used for any user-visible display
* of the algorithm name.
*
* Algorithm display names should be short, e.g. ideally no more than 3 or 4 words.
* The name should use sentence case (e.g. "Raster layer statistics", not "Raster Layer Statistics").
*
* \see name()
* \see shortDescription()
*/
virtual QString displayName() const = 0 SIP_HOLDGIL;
/**
* Returns an optional translated short description of the algorithm. This should be
* at most a single sentence, e.g. "Converts 2D features to 3D by sampling a DEM raster."
* \since QGIS 3.2
*/
virtual QString shortDescription() const SIP_HOLDGIL;
/**
* Returns a list of tags which relate to the algorithm, and are used to assist users in searching
* for suitable algorithms. These tags should be localised.
*/
virtual QStringList tags() const SIP_HOLDGIL { return QStringList(); }
/**
* Returns a localised short helper string for the algorithm. This string should provide a basic description
* about what the algorithm does and the parameters and outputs associated with it.
* \see helpString()
* \see helpUrl()
*/
virtual QString shortHelpString() const SIP_HOLDGIL;
/**
* Returns a localised help string for the algorithm. Algorithm subclasses should implement either
* helpString() or helpUrl().
* \see helpUrl()
* \see shortHelpString()
* \deprecated QGIS 3.40. Unused, will be removed in QGIS 4.0.
*/
Q_DECL_DEPRECATED virtual QString helpString() const SIP_HOLDGIL SIP_DEPRECATED;
/**
* Returns a url pointing to the algorithm's help page.
* \see helpString()
* \see shortHelpString()
*/
virtual QString helpUrl() const SIP_HOLDGIL;
/**
* Returns the flags describing algorithm behavior for documentation purposes.
*
* The default is to return no flags.
*
* \since QGIS 3.40
*/
virtual Qgis::ProcessingAlgorithmDocumentationFlags documentationFlags() const SIP_HOLDGIL;
/**
* Returns an icon for the algorithm.
* \see svgIconPath()
*/
virtual QIcon icon() const SIP_HOLDGIL;
/**
* Returns a path to an SVG version of the algorithm's icon.
* \see icon()
*/
virtual QString svgIconPath() const SIP_HOLDGIL;
/**
* Returns the name of the group this algorithm belongs to. This string
* should be localised.
* \see groupId()
* \see tags()
*/
virtual QString group() const SIP_HOLDGIL { return QString(); }
/**
* Returns the unique ID of the group this algorithm belongs to. This string
* should be fixed for the algorithm, and must not be localised. The group id
* should be unique within each provider. Group id should contain lowercase
* alphanumeric characters only and no spaces or other formatting characters.
* \see group()
*/
virtual QString groupId() const SIP_HOLDGIL { return QString(); }
/**
* Returns the flags indicating how and when the algorithm operates and should be exposed to users.
* Default flags are FlagSupportsBatch and FlagCanCancel.
*/
virtual Qgis::ProcessingAlgorithmFlags flags() const SIP_HOLDGIL;
/**
* Returns TRUE if the algorithm can execute. Algorithm subclasses can return FALSE
* here to indicate that they are not able to execute, e.g. as a result of unmet
* external dependencies. If specified, the \a errorMessage argument will be filled
* with a localised error message describing why the algorithm cannot execute.
*/
virtual bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const;
/**
* Checks the supplied \a parameter values to verify that they satisfy the requirements
* of this algorithm in the supplied \a context. The \a message parameter will be
* filled with explanatory text if validation fails.
* Overridden implementations should also check this base class implementation.
* \returns TRUE if parameters are acceptable for the algorithm.
*/
virtual bool checkParameterValues( const QVariantMap ¶meters,
QgsProcessingContext &context, QString *message SIP_OUT = nullptr ) const;
/**
* Pre-processes a set of \a parameters, allowing the algorithm to clean their
* values.
*
* This method is automatically called after users enter parameters, e.g. via the algorithm
* dialog. This method should NOT be called manually by algorithms.
*/
virtual QVariantMap preprocessParameters( const QVariantMap ¶meters );
/**
* Returns the provider to which this algorithm belongs.
*/
QgsProcessingProvider *provider() const SIP_HOLDGIL;
/**
* Returns an ordered list of parameter definitions utilized by the algorithm.
* \see addParameter()
* \see parameterDefinition()
* \see destinationParameterDefinitions()
*/
QgsProcessingParameterDefinitions parameterDefinitions() const SIP_HOLDGIL { return mParameters; }
/**
* Returns a matching parameter by \a name. Matching is done in a case-insensitive
* manner, but exact case matches will be preferred.
* \see parameterDefinitions()
*/
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const SIP_HOLDGIL;
/**
* Returns the number of visible (non-hidden) parameters defined by this
* algorithm.
*/
int countVisibleParameters() const SIP_HOLDGIL;
/**
* Returns a list of destination parameters definitions utilized by the algorithm.
* \see QgsProcessingParameterDefinition::isDestination()
* \see parameterDefinitions()
*/
QgsProcessingParameterDefinitions destinationParameterDefinitions() const SIP_HOLDGIL;
/**
* Returns an ordered list of output definitions utilized by the algorithm.
* \see addOutput()
* \see outputDefinition()
*/
QgsProcessingOutputDefinitions outputDefinitions() const SIP_HOLDGIL { return mOutputs; }
/**
* Returns a matching output by \a name. Matching is done in a case-insensitive
* manner.
* \see outputDefinitions()
*/
const QgsProcessingOutputDefinition *outputDefinition( const QString &name ) const SIP_HOLDGIL;
/**
* Returns TRUE if this algorithm generates HTML outputs.
*/
bool hasHtmlOutputs() const SIP_HOLDGIL;
/**
* Properties of a vector source or sink used in an algorithm.
*
* \since QGIS 3.14
*/
struct VectorProperties
{
//! Fields
QgsFields fields;
//! Geometry (WKB) type
Qgis::WkbType wkbType = Qgis::WkbType::Unknown;
//! Coordinate Reference System
QgsCoordinateReferenceSystem crs;
//! Availability of the properties. By default properties are not available.
Qgis::ProcessingPropertyAvailability availability = Qgis::ProcessingPropertyAvailability::NotAvailable;
};
/**
* Returns the vector properties which will be used for the \a sink with matching name.
*
* The \a parameters argument specifies the values of all parameters which would be used to generate
* the sink. These can be used alongside the provided \a context in order to pre-evaluate inputs
* when required in order to determine the sink's properties.
*
* The \a sourceProperties map will contain the vector properties of the various sources used
* as inputs to the algorithm. These will only be available in certain circumstances (e.g. when the
* algorithm is used within a model), so implementations will need to be adaptable to circumstances
* when either \a sourceParameters is empty or \a parameters is empty, and use whatever information
* is passed in order to make a best guess determination of the output properties.
*
* \since QGIS 3.14
*/
virtual QgsProcessingAlgorithm::VectorProperties sinkProperties( const QString &sink,
const QVariantMap ¶meters,
QgsProcessingContext &context,
const QMap< QString, QgsProcessingAlgorithm::VectorProperties > &sourceProperties ) const;
/**
* Executes the algorithm using the specified \a parameters. This method internally
* creates a copy of the algorithm before running it, so it is safe to call
* on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider.
*
* The \a context argument specifies the context in which the algorithm is being run.
*
* Algorithm progress should be reported using the supplied \a feedback object.
*
* If specified, \a ok will be set to TRUE if algorithm was successfully run.
*
* If \a catchExceptions is set to FALSE, then QgsProcessingException raised during
* the algorithm run will not be automatically caught and will be raised instead.
*
* \returns A map of algorithm outputs. These may be output layer references, or calculated
* values such as statistical calculations.
*
* \note this method can only be called from the main thread. Use prepare(), runPrepared() and postProcess()
* if you need to run algorithms from a background thread, or use the QgsProcessingAlgRunnerTask class.
*/
QVariantMap run( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool *ok SIP_OUT = nullptr, const QVariantMap &configuration = QVariantMap(),
bool catchExceptions = true ) const SIP_THROW( QgsProcessingException );
/**
* Prepares the algorithm for execution. This must be run in the main thread, and allows the algorithm
* to pre-evaluate input parameters in a thread-safe manner. This must be called before
* calling runPrepared() (which is safe to do in any thread).
* \see runPrepared()
* \see postProcess()
* \note This method modifies the algorithm instance, so it is not safe to call
* on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy
* of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.
*/
bool prepare( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
/**
* Runs the algorithm, which has been prepared by an earlier call to prepare().
* This method is safe to call from any thread. Returns TRUE if the algorithm was successfully executed.
* After runPrepared() has finished, the postProcess() method should be called from the main thread
* to allow the algorithm to perform any required cleanup tasks and return its final result.
* \see prepare()
* \see postProcess()
* \note This method modifies the algorithm instance, so it is not safe to call
* on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy
* of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.
*/
QVariantMap runPrepared( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException );
/**
* Should be called in the main thread following the completion of runPrepared(). This method
* allows the algorithm to perform any required cleanup tasks. The returned variant map
* includes the results evaluated by the algorithm.
* \note This method modifies the algorithm instance, so it is not safe to call
* on algorithms directly retrieved from QgsProcessingRegistry and QgsProcessingProvider. Instead, a copy
* of the algorithm should be created with clone() and prepare()/runPrepared() called on the copy.
*
* Since QGIS 3.38, postProcess() will always be called even for unsuccessful run executions, to allow
* the algorithm to gracefully clean up. The \a runResult argument is used to indicate whether the run
* was successful. The algorithm's postProcessAlgorithm() method will only be called when \a runResult is TRUE.
*/
QVariantMap postProcess( QgsProcessingContext &context, QgsProcessingFeedback *feedback, bool runResult = true );
/**
* If an algorithm subclass implements a custom parameters widget, a copy of this widget
* should be constructed and returned by this method.
* The base class implementation returns NULLPTR, which indicates that an autogenerated
* parameters widget should be used.
*/
virtual QWidget *createCustomParametersWidget( QWidget *parent = nullptr ) const SIP_FACTORY;
/**
* Creates an expression context relating to the algorithm. This can be called by algorithms
* to create a new expression context ready for evaluating expressions within the algorithm.
* Optionally, a \a source can be specified which will be used to populate the context if it
* implements the QgsExpressionContextGenerator interface.
*/
virtual QgsExpressionContext createExpressionContext( const QVariantMap ¶meters,
QgsProcessingContext &context, QgsProcessingFeatureSource *source = nullptr ) const;
/**
* Checks whether the coordinate reference systems for the specified set of \a parameters
* are valid for the algorithm. For instance, the base implementation performs
* checks to ensure that all input CRS are equal
* Returns TRUE if \a parameters have passed the CRS check.
*/
virtual bool validateInputCrs( const QVariantMap ¶meters,
QgsProcessingContext &context ) const;
/**
* Returns a Python command string which can be executed to run the algorithm
* using the specified \a parameters.
*
* Algorithms which cannot be run from a Python command should return an empty
* string.
*/
virtual QString asPythonCommand( const QVariantMap ¶meters, QgsProcessingContext &context ) const;
/**
* Returns a command string which will execute the algorithm using the specified \a parameters
* via the command line qgis_process tool.
*
* Note that some combinations of parameter types and values cannot be represented as a qgis_process string.
*
* \param parameters algorithm parameters
* \param context processing context
* \param ok will be set to TRUE if the command was successfully generated
*
* \returns equivalent qgis_process command
*
* \since QGIS 3.24
*/
virtual QString asQgisProcessCommand( const QVariantMap ¶meters, QgsProcessingContext &context, bool &ok SIP_OUT ) const;
/**
* Returns a JSON serializable variant map containing the specified \a parameters and \a context settings.
*
* \since QGIS 3.24
*/
virtual QVariantMap asMap( const QVariantMap ¶meters, QgsProcessingContext &context ) const;
/**
* Associates this algorithm with its provider. No transfer of ownership is involved.
*/
void setProvider( QgsProcessingProvider *provider ) SIP_HOLDGIL;
/**
* Checks whether this algorithm supports in-place editing on the given \a layer
* Default implementation returns FALSE.
*
* \return TRUE if the algorithm supports in-place editing
* \since QGIS 3.4
*/
virtual bool supportInPlaceEdit( const QgsMapLayer *layer ) const;
protected:
/**
* Creates a new instance of the algorithm class.
*
* This method should return a 'pristine' instance of the algorithm class.
*/
virtual QgsProcessingAlgorithm *createInstance() const = 0 SIP_FACTORY SIP_VIRTUALERRORHANDLER( processing_exception_handler );
/**
* Initializes the algorithm using the specified \a configuration.
*
* This should be called directly after creating algorithms and before retrieving
* any parameterDefinitions() or outputDefinitions().
*
* Subclasses should use their implementations to add all required input parameter and output
* definitions (which can be dynamically adjusted according to \a configuration).
*
* Dynamic configuration can be used by algorithms which alter their behavior
* when used inside processing models. For instance, a "feature router" type
* algorithm which sends input features to one of any number of outputs sinks
* based on some preconfigured filter parameters can use the init method to
* create these outputs based on the specified \a configuration.
*
* \see addParameter()
* \see addOutput()
*/
virtual void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) = 0;
/**
* Adds a parameter \a definition to the algorithm. Ownership of the definition is transferred to the algorithm.
* Returns TRUE if parameter could be successfully added, or FALSE if the parameter could not be added (e.g.
* as a result of a duplicate name).
*
* This should usually be called from a subclass' initAlgorithm() implementation.
*
* If the \a createOutput argument is TRUE, then a corresponding output definition will also be created
* (and added to the algorithm) where appropriate. E.g. when adding a QgsProcessingParameterVectorDestination
* and \a createOutput is TRUE, then a QgsProcessingOutputVectorLayer output will be created and
* added to the algorithm. There is no need to call addOutput() to manually add a corresponding output
* for this vector. If \a createOutput is FALSE then this automatic output creation will not
* occur.
*
* \see initAlgorithm()
* \see addOutput()
*/
bool addParameter( QgsProcessingParameterDefinition *parameterDefinition SIP_TRANSFER, bool createOutput = true ) SIP_HOLDGIL;
/**
* Removes the parameter with matching \a name from the algorithm, and deletes any existing
* definition.
*/
void removeParameter( const QString &name ) SIP_HOLDGIL;
/**
* Adds an output \a definition to the algorithm. Ownership of the definition is transferred to the algorithm.
* Returns TRUE if the output could be successfully added, or FALSE if the output could not be added (e.g.
* as a result of a duplicate name).
*
* This should usually be called from a subclass' initAlgorithm() implementation.
*
* Note that in some cases output creation can be automatically performed when calling addParameter().
* See the notes in addParameter() for a description of when this occurs.
*
* \see addParameter()
* \see initAlgorithm()
*/
bool addOutput( QgsProcessingOutputDefinition *outputDefinition SIP_TRANSFER ) SIP_HOLDGIL;
/**
* Prepares the algorithm to run using the specified \a parameters. Algorithms should implement
* their logic for evaluating parameter values here. The evaluated parameter results should
* be stored in member variables ready for a call to processAlgorithm().
*
* The \a context argument specifies the context in which the algorithm is being run.
*
* prepareAlgorithm should be used to handle any thread-sensitive preparation which is required
* by the algorithm. It will always be called from the same thread that \a context has thread
* affinity with. While this will generally be the main thread, it is not guaranteed. For instance,
* algorithms which are run as a step in a larger model or as a subcomponent of a script-based algorithm
* will call prepareAlgorithm from the same thread as that model/script it being executed in.
*
* Note that the processAlgorithm step uses a temporary context with affinity for the thread in
* which the algorithm is executed, making it safe for processAlgorithm implementations to load
* sources and sinks without issue. Implementing prepareAlgorithm is only required if special
* thread safe handling is required by the algorithm.
*
* Algorithm preparation progress should be reported using the supplied \a feedback object. Additionally,
* well-behaved algorithms should periodically check \a feedback to determine whether the
* algorithm should be canceled and exited early.
*
* If the preparation was successful algorithms must return TRUE. If a FALSE value is returned
* this indicates that the preparation could not be completed, and the algorithm execution
* will be canceled.
*
* \returns TRUE if preparation was successful.
* \see processAlgorithm()
* \see postProcessAlgorithm()
*/
virtual bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
/**
* Runs the algorithm using the specified \a parameters. Algorithms should implement
* their custom processing logic here.
*
* The \a context argument gives a temporary context with thread affinity matching the thread
* in which the algorithm is being run. This is a cut-back copy of the context passed to
* the prepareAlgorithm() and postProcessAlgorithm() steps, but it is generally safe
* for most algorithms to utilize this context for loading layers and creating sinks.
* Any loaded layers or sinks created within this temporary context will be transferred
* back to the main execution context upon successful completion of the processAlgorithm()
* step.
*
* Algorithm progress should be reported using the supplied \a feedback object. Additionally,
* well-behaved algorithms should periodically check \a feedback to determine whether the
* algorithm should be canceled and exited early.
*
* This method will not be called if the prepareAlgorithm() step failed (returned FALSE).
*
* Implementations of processAlgorithm can throw the QgsProcessingException exception
* to indicate that a fatal error occurred within the execution.
*
* \returns A map of algorithm outputs. These may be output layer references, or calculated
* values such as statistical calculations. Unless the algorithm subclass overrides
* the postProcessAlgorithm() step this returned map will be used as the output for the
* algorithm.
*
* \see prepareAlgorithm()
* \see postProcessAlgorithm()
*/
virtual QVariantMap processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) = 0 SIP_VIRTUALERRORHANDLER( processing_exception_handler );
/**
* Allows the algorithm to perform any required cleanup tasks. The returned variant map
* includes the results evaluated by the algorithm. These may be output layer references, or calculated
* values such as statistical calculations.
*
* The \a context argument specifies the context in which the algorithm was run.
*
* Postprocess progress should be reported using the supplied \a feedback object. Additionally,
* well-behaved algorithms should periodically check \a feedback to determine whether the
* post processing should be canceled and exited early.
*
* postProcessAlgorithm should be used to handle any thread-sensitive cleanup which is required
* by the algorithm. It will always be called from the same thread that \a context has thread
* affinity with. While this will generally be the main thread, it is not guaranteed. For instance,
* algorithms which are run as a step in a larger model or as a subcomponent of a script-based algorithm
* will call postProcessAlgorithm from the same thread as that model/script it being executed in.
*
* postProcessAlgorithm will not be called if the prepareAlgorithm() step failed (returned FALSE),
* or if an exception was raised by the processAlgorithm() step.
*
* \returns A map of algorithm outputs. These may be output layer references, or calculated
* values such as statistical calculations. Implementations which return a non-empty
* map will override any results returned by processAlgorithm().
*
* \see prepareAlgorithm()
* \see processAlgorithm()
*/
virtual QVariantMap postProcessAlgorithm( QgsProcessingContext &context, QgsProcessingFeedback *feedback ) SIP_THROW( QgsProcessingException ) SIP_VIRTUALERRORHANDLER( processing_exception_handler );
/**
* Evaluates the parameter with matching \a name to a static string value.
*/
QString parameterAsString( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to an expression.
*/
QString parameterAsExpression( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static double value.
*/
double parameterAsDouble( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static integer value.
*/
int parameterAsInt( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a list of integer values.
* \since QGIS 3.4
*/
QList<int> parameterAsInts( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a enum value.
*/
int parameterAsEnum( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to list of enum values.
*/
QList<int> parameterAsEnums( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static enum string.
* \since QGIS 3.18
*/
QString parameterAsEnumString( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to list of static enum strings.
* \since QGIS 3.18
*/
QStringList parameterAsEnumStrings( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static boolean value.
*/
bool parameterAsBool( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a static boolean value.
* \since QGIS 3.8
*/
bool parameterAsBoolean( const QVariantMap ¶meters, const QString &name, const QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a feature sink.
*
* Sinks will either be taken from \a context's active project, or created from external
* providers and stored temporarily in the \a context.
*
* The \a fields, \a geometryType and \a crs parameters dictate the properties
* of the resulting feature sink.
*
* The \a destinationIdentifier argument will be set to a string which can be used to retrieve the layer corresponding
* to the sink, e.g. via calling QgsProcessingUtils::mapLayerFromString().
*
* The \a createOptions argument is used to pass on creation options such as layer name.
*
* The \a datasourceOptions and \a layerOptions arguments is used to pass on GDAL-specific format driver options.
*
* This function creates a new object and the caller takes responsibility for deleting the returned object.
*
* \throws QgsProcessingException
*/
QgsFeatureSink *parameterAsSink( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context, QString &destinationIdentifier SIP_OUT,
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;
/**
* Evaluates the parameter with matching \a name to a feature source.
*
* Sources will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context.
*
* This function creates a new object and the caller takes responsibility for deleting the returned object.
*/
QgsProcessingFeatureSource *parameterAsSource( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const SIP_FACTORY;
/**
* Evaluates the parameter with matching \a name to a source vector layer file path of compatible format.
*
* If the parameter is evaluated to an existing layer, and that layer is not of the format listed in the
* \a compatibleFormats argument, then the layer will first be exported to a compatible format
* in a temporary location. The function will then return the path to that temporary file.
*
* \a compatibleFormats should consist entirely of lowercase file extensions, e.g. 'shp'.
*
* The \a preferredFormat argument is used to specify to desired file extension to use when a temporary
* layer export is required.
*
* When an algorithm is capable of handling multi-layer input files (such as Geopackage), it is preferable
* to use parameterAsCompatibleSourceLayerPathAndLayerName() which may avoid conversion in more situations.
*/
QString parameterAsCompatibleSourceLayerPath( const QVariantMap ¶meters, const QString &name,
QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr ) const;
/**
* Evaluates the parameter with matching \a name to a source vector layer file path and layer name of compatible format.
*
* If the parameter is evaluated to an existing layer, and that layer is not of the format listed in the
* \a compatibleFormats argument, then the layer will first be exported to a compatible format
* in a temporary location. The function will then return the path to that temporary file.
*
* \a compatibleFormats should consist entirely of lowercase file extensions, e.g. 'shp'.
*
* The \a preferredFormat argument is used to specify to desired file extension to use when a temporary
* layer export is required. This defaults to shapefiles, because shapefiles are the future (don't believe the geopackage hype!).
*
* This method should be preferred over parameterAsCompatibleSourceLayerPath() when an algorithm is able
* to correctly handle files with multiple layers. Unlike parameterAsCompatibleSourceLayerPath(), it will not force
* a conversion in this case and will return the target layer name in the \a layerName argument.
*
* \param parameters input parameter value map
* \param name name of target parameter
* \param context processing context
* \param compatibleFormats a list of lowercase file extensions compatible with the algorithm
* \param preferredFormat preferred format extension to use if conversion if required
* \param feedback feedback object
* \param layerName will be set to the target layer name for multi-layer sources (e.g. Geopackage)
*
* \returns path to source layer, or nearly converted compatible layer
*
* \see parameterAsCompatibleSourceLayerPath()
* \since QGIS 3.10
*/
QString parameterAsCompatibleSourceLayerPathAndLayerName( const QVariantMap ¶meters, const QString &name,
QgsProcessingContext &context, const QStringList &compatibleFormats, const QString &preferredFormat = QString( "shp" ), QgsProcessingFeedback *feedback = nullptr, QString *layerName SIP_OUT = nullptr ) const;
/**
* Evaluates the parameter with matching \a name to a map layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
QgsMapLayer *parameterAsLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a raster layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
QgsRasterLayer *parameterAsRasterLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a mesh layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*
* \since QGIS 3.6
*/
QgsMeshLayer *parameterAsMeshLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a output layer destination.
*/
QString parameterAsOutputLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a file based output destination.
*/
QString parameterAsFileOutput( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a vector layer.
*
* Layers will either be taken from \a context's active project, or loaded from external
* sources and stored temporarily in the \a context. In either case, callers do not
* need to handle deletion of the returned layer.
*/
QgsVectorLayer *parameterAsVectorLayer( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a coordinate reference system.
*/
QgsCoordinateReferenceSystem parameterAsCrs( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a rectangular extent.
*
* If \a crs is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically
* reprojected so that it is in the specified \a crs. In this case the extent of the reproject rectangle will be returned.
*
* \see parameterAsExtentGeometry()
*/
QgsRectangle parameterAsExtent( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
/**
* Evaluates the parameter with matching \a name to a rectangular extent, and returns a geometry covering this extent.
*
* If \a crs is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically
* reprojected so that it is in the specified \a crs. Unlike parameterAsExtent(), the reprojected rectangle returned by this function
* will no longer be a rectangle itself (i.e. this method returns the geometry of the actual reprojected rectangle, while parameterAsExtent() returns
* just the extent of the reprojected rectangle).
*
* \see parameterAsExtent()
*/
QgsGeometry parameterAsExtentGeometry( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
/**
* Returns the coordinate reference system associated with an extent parameter value.
*
* \see parameterAsExtent()
*/
QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a point.
*
* If \a crs is set then the point will be automatically
* reprojected so that it is in the specified \a crs.
*
* \see parameterAsPointCrs()
*/
QgsPointXY parameterAsPoint( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
/**
* Returns the coordinate reference system associated with an point parameter value.
*
* \see parameterAsPoint()
*/
QgsCoordinateReferenceSystem parameterAsPointCrs( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a geometry.
*
* If \a crs is set then the geometry will be automatically
* reprojected so that it is in the specified \a crs.
*
* \see parameterAsGeometryCrs()
*/
QgsGeometry parameterAsGeometry( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context,
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
/**
* Returns the coordinate reference system associated with a geometry parameter value.
*
* \see parameterAsGeometry()
*/
QgsCoordinateReferenceSystem parameterAsGeometryCrs( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a file/folder name.
*/
QString parameterAsFile( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a matrix/table of values.
* Tables are collapsed to a 1 dimensional list.
*/
QVariantList parameterAsMatrix( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a list of map layers.
* The \a flags are used to set options for loading layers (e.g. skip index generation).
*/
QList< QgsMapLayer *> parameterAsLayerList( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() ) const;
/**
* Evaluates the parameter with matching \a name to a list of files (for QgsProcessingParameterMultipleLayers in QgsProcessing:TypeFile mode).
*
* \since QGIS 3.10
*/
QStringList parameterAsFileList( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a range of values.
*/
QList<double> parameterAsRange( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a list of fields.
*
* \deprecated QGIS 3.40. Use parameterAsStrings() instead.
*/
Q_DECL_DEPRECATED QStringList parameterAsFields( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const SIP_DEPRECATED;
/**
* Evaluates the parameter with matching \a name to a list of strings (e.g. field names or point cloud attributes).
*
* \since QGIS 3.32
*/
QStringList parameterAsStrings( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a print layout.
*
* \warning This method is not safe to run in a background thread, so it must either be used within a prepareAlgorithm
* implementation (which runs in the main thread), or the algorithm must return the FlagNoThreading flag.
*
* \since QGIS 3.8
*/
QgsPrintLayout *parameterAsLayout( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context );
/**
* Evaluates the parameter with matching \a name to a print layout item, taken from the specified \a layout.
*
* \warning This method is not safe to run in a background thread, so it must either be used within a prepareAlgorithm
* implementation (which runs in the main thread), or the algorithm must return the FlagNoThreading flag.
*
* \since QGIS 3.8
*/
QgsLayoutItem *parameterAsLayoutItem( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context, QgsPrintLayout *layout );
/**
* Evaluates the parameter with matching \a name to a color, or returns an invalid color if the parameter was not set.
*
* \since QGIS 3.10
*/
QColor parameterAsColor( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a connection name string.
*
* \since QGIS 3.14
*/
QString parameterAsConnectionName( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a database schema name string.
*
* \since QGIS 3.14
*/
QString parameterAsSchema( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a database table name string.
*
* \since QGIS 3.14
*/
QString parameterAsDatabaseTableName( const QVariantMap ¶meters, const QString &name, QgsProcessingContext &context ) const;
/**
* Evaluates the parameter with matching \a name to a DateTime, or returns an invalid date time if the parameter was not set.
*
* \since QGIS 3.14
*/