QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgsprocessingutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingutils.h
3 ------------------------
4 begin : April 2017
5 copyright : (C) 2017 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 QGSPROCESSINGUTILS_H
19#define QGSPROCESSINGUTILS_H
20
21#include "qgis_core.h"
22
23#include "qgsrasterlayer.h"
24#include "qgsfeaturesink.h"
25#include "qgsfeaturesource.h"
26#include "qgsprocessing.h"
27#include "qgsproxyfeaturesink.h"
29
30class QgsMeshLayer;
31class QgsPluginLayer;
32class QgsProject;
42
43#include <QString>
44#include <QVariant>
45
52class CORE_EXPORT QgsProcessingUtils
53{
54 public:
55
70 static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
71
91 static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
92 const QList< int > &sourceTypes = QList< int >(),
93 bool sort = true );
94
112 static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
113
131 static QList<QgsPluginLayer *> compatiblePluginLayers( QgsProject *project, bool sort = true );
132
150 static QList<QgsPointCloudLayer *> compatiblePointCloudLayers( QgsProject *project, bool sort = true );
151
169 static QList<QgsAnnotationLayer *> compatibleAnnotationLayers( QgsProject *project, bool sort = true );
170
188 static QList<QgsVectorTileLayer *> compatibleVectorTileLayers( QgsProject *project, bool sort = true );
189
199 static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
200
207 static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
208
220 static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
221
227 {
228 UnknownType,
229 Vector,
230 Raster,
231 Mesh,
232 PointCloud,
233 Annotation,
234 VectorTile,
235 };
236
249 static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
250
262 static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
263
272 static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
273
278 static QString normalizeLayerSource( const QString &source );
279
286 static QString variantToPythonLiteral( const QVariant &value );
287
293 static QString stringToPythonLiteral( const QString &string );
294
312#ifndef SIP_RUN
313 static QgsFeatureSink *createFeatureSink( QString &destination,
314 QgsProcessingContext &context,
315 const QgsFields &fields,
316 Qgis::WkbType geometryType,
318 const QVariantMap &createOptions = QVariantMap(),
319 const QStringList &datasourceOptions = QStringList(),
320 const QStringList &layerOptions = QStringList(),
321 QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags(),
322 QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
323#endif
324
344 static void createFeatureSinkPython( QgsFeatureSink **sink SIP_OUT SIP_TRANSFERBACK, QString &destination SIP_INOUT, QgsProcessingContext &context, const QgsFields &fields, Qgis::WkbType geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions = QVariantMap() ) SIP_THROW( QgsProcessingException ) SIP_PYNAME( createFeatureSink );
345
346
353 static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
354
360 Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
361
368 static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
369
374 static QString tempFolder( const QgsProcessingContext *context = nullptr );
375
382 static QString generateTempFilename( const QString &basename, const QgsProcessingContext *context = nullptr );
383
388 static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
389
412 static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
413 bool selectedFeaturesOnly,
414 const QString &baseName,
415 const QStringList &compatibleFormats,
416 const QString &preferredFormat,
417 QgsProcessingContext &context,
418 QgsProcessingFeedback *feedback,
419 long long featureLimit = -1,
420 const QString &filterExpression = QString() );
421
456 static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
457 bool selectedFeaturesOnly,
458 const QString &baseName,
459 const QStringList &compatibleFormats,
460 const QString &preferredFormat,
461 QgsProcessingContext &context,
462 QgsProcessingFeedback *feedback,
463 QString &layerName SIP_OUT,
464 long long featureLimit = -1,
465 const QString &filterExpression = QString() );
466
477 static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() );
478
484 static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
485
490 static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
491
504 static QString defaultVectorExtension();
505
518 static QString defaultRasterExtension();
519
531 static QString defaultPointCloudExtension();
532
544 static QString defaultVectorTileExtension();
545
552 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
553
561 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
562
563 private:
564 static bool canUseLayer( const QgsRasterLayer *layer );
565 static bool canUseLayer( const QgsMeshLayer *layer );
566 static bool canUseLayer( const QgsPluginLayer *layer );
567 static bool canUseLayer( const QgsVectorTileLayer *layer );
568 static bool canUseLayer( const QgsPointCloudLayer *layer );
569 static bool canUseLayer( const QgsAnnotationLayer *layer );
570 static bool canUseLayer( const QgsVectorLayer *layer,
571 const QList< int > &sourceTypes = QList< int >() );
572
585 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
586
597 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
598
609 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
610
619 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
620
621 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
622
623 friend class TestQgsProcessing;
625
626};
627
636{
637 public:
638
640 enum Flag
641 {
642 FlagSkipGeometryValidityChecks = 1 << 1,
643 };
644 Q_DECLARE_FLAGS( Flags, Flag )
645
646
658 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
659 long long featureLimit = -1, const QString &filterExpression = QString() );
660
662
668 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
669
670 QgsFeatureSource::FeatureAvailability hasFeatures() const override;
671
672 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
673 QgsCoordinateReferenceSystem sourceCrs() const override;
674 QgsFields fields() const override;
675 Qgis::WkbType wkbType() const override;
676 long long featureCount() const override;
677 QString sourceName() const override;
678 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
679 QVariant minimumValue( int fieldIndex ) const override;
680 QVariant maximumValue( int fieldIndex ) const override;
681 QgsRectangle sourceExtent() const override;
682 QgsFeatureIds allFeatureIds() const override;
683 SpatialIndexPresence hasSpatialIndex() const override;
684
688 QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
689
695 void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck method );
696
697 private:
698
699 QgsFeatureSource *mSource = nullptr;
700 bool mOwnsSource = false;
701 QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
702 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
703 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
704
705 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
706 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
707
708 long long mFeatureLimit = -1;
709 QString mFilterExpression;
710
711};
712
713#ifndef SIP_RUN
714
723{
724 public:
725
726
740 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
741 ~QgsProcessingFeatureSink() override;
742 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
743 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
744 bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
745
746 private:
747
748 QgsProcessingContext &mContext;
749 QString mSinkName;
750 bool mOwnsSink = false;
751
752};
753#endif
754
755#endif // QGSPROCESSINGUTILS_H
756
757
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:154
Represents a map layer containing a set of georeferenced annotations, e.g.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Single scope for storing variables and functions for use within a QgsExpressionContext.
Wrapper for iterator of features from vector data provider or vector layer.
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.
An interface for objects which provide features via a getFeatures method.
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Container of fields for a vector layer.
Definition: qgsfields.h:45
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Base class for all map layer types.
Definition: qgsmaplayer.h:73
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:100
Base class for plugin layers.
Represents a map layer supporting display of point clouds.
Abstract base class for processing algorithms.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:84
QgsProxyFeatureSink subclass which reports feature addition errors to a QgsProcessingContext.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Base class for providing feedback from a processing algorithm.
Abstract base class for processing providers.
Utility functions for use with processing classes.
LayerHint
Layer type hints.
@ UnknownType
Unknown layer type.
Contains enumerations and other constants for use in processing algorithms and parameters.
Definition: qgsprocessing.h:41
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:107
A simple feature sink which proxies feature addition on to another feature sink.
Represents a raster layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
Represents a vector layer which manages a vector based data sets.
Implements a map layer that is dedicated to rendering of vector tiles.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into allowing algorithms to be written in pure substantial changes are required in order to port existing x Processing algorithms for QGIS x The most significant changes are outlined not GeoAlgorithm For algorithms which operate on features one by consider subclassing the QgsProcessingFeatureBasedAlgorithm class This class allows much of the boilerplate code for looping over features from a vector layer to be bypassed and instead requires implementation of a processFeature method Ensure that your algorithm(or algorithm 's parent class) implements the new pure virtual createInstance(self) call
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
#define SIP_MONKEYPATCH_SCOPEENUM
Definition: qgis_sip.h:265
#define SIP_OUT
Definition: qgis_sip.h:58
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_INOUT
Definition: qgis_sip.h:71
#define SIP_THROW(name,...)
Definition: qgis_sip.h:198
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:920
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
const QgsCoordinateReferenceSystem & crs