QGIS API Documentation 3.31.0-Master (d2b117a3c8)
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 "qgsproxyfeaturesink.h"
28
29class QgsMeshLayer;
30class QgsPluginLayer;
31class QgsProject;
40
41#include <QString>
42#include <QVariant>
43
50class CORE_EXPORT QgsProcessingUtils
51{
52 public:
53
67 static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
68
87 static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
88 const QList< int > &sourceTypes = QList< int >(),
89 bool sort = true );
90
107 static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
108
125 static QList<QgsPluginLayer *> compatiblePluginLayers( QgsProject *project, bool sort = true );
126
143 static QList<QgsPointCloudLayer *> compatiblePointCloudLayers( QgsProject *project, bool sort = true );
144
161 static QList<QgsAnnotationLayer *> compatibleAnnotationLayers( QgsProject *project, bool sort = true );
162
172 static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
173
180 static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
181
193 static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
194
200 {
201 UnknownType,
202 Vector,
203 Raster,
204 Mesh,
205 PointCloud,
206 Annotation,
207 };
208
221 static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
222
234 static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
235
244 static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
245
250 static QString normalizeLayerSource( const QString &source );
251
258 static QString variantToPythonLiteral( const QVariant &value );
259
265 static QString stringToPythonLiteral( const QString &string );
266
284#ifndef SIP_RUN
285 static QgsFeatureSink *createFeatureSink( QString &destination,
286 QgsProcessingContext &context,
287 const QgsFields &fields,
288 Qgis::WkbType geometryType,
290 const QVariantMap &createOptions = QVariantMap(),
291 const QStringList &datasourceOptions = QStringList(),
292 const QStringList &layerOptions = QStringList(),
293 QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags(),
294 QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
295#endif
296
316 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 );
317
318
325 static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
326
332 Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
333
340 static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
341
346 static QString tempFolder( const QgsProcessingContext *context = nullptr );
347
354 static QString generateTempFilename( const QString &basename, const QgsProcessingContext *context = nullptr );
355
360 static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
361
384 static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
385 bool selectedFeaturesOnly,
386 const QString &baseName,
387 const QStringList &compatibleFormats,
388 const QString &preferredFormat,
389 QgsProcessingContext &context,
390 QgsProcessingFeedback *feedback,
391 long long featureLimit = -1,
392 const QString &filterExpression = QString() );
393
428 static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
429 bool selectedFeaturesOnly,
430 const QString &baseName,
431 const QStringList &compatibleFormats,
432 const QString &preferredFormat,
433 QgsProcessingContext &context,
434 QgsProcessingFeedback *feedback,
435 QString &layerName SIP_OUT,
436 long long featureLimit = -1,
437 const QString &filterExpression = QString() );
438
449 static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() );
450
456 static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
457
462 static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
463
475 static QString defaultVectorExtension();
476
488 static QString defaultRasterExtension();
489
500 static QString defaultPointCloudExtension();
501
508 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
509
517 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
518
519 private:
520 static bool canUseLayer( const QgsRasterLayer *layer );
521 static bool canUseLayer( const QgsMeshLayer *layer );
522 static bool canUseLayer( const QgsPluginLayer *layer );
523 static bool canUseLayer( const QgsVectorTileLayer *layer );
524 static bool canUseLayer( const QgsPointCloudLayer *layer );
525 static bool canUseLayer( const QgsAnnotationLayer *layer );
526 static bool canUseLayer( const QgsVectorLayer *layer,
527 const QList< int > &sourceTypes = QList< int >() );
528
541 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
542
553 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
554
565 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType );
566
575 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
576
577 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
578
579 friend class TestQgsProcessing;
581
582};
583
592{
593 public:
594
596 enum Flag
597 {
598 FlagSkipGeometryValidityChecks = 1 << 1,
599 };
600 Q_DECLARE_FLAGS( Flags, Flag )
601
602
614 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
615 long long featureLimit = -1, const QString &filterExpression = QString() );
616
618
624 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
625
626 QgsFeatureSource::FeatureAvailability hasFeatures() const override;
627
628 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
629 QgsCoordinateReferenceSystem sourceCrs() const override;
630 QgsFields fields() const override;
631 Qgis::WkbType wkbType() const override;
632 long long featureCount() const override;
633 QString sourceName() const override;
634 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
635 QVariant minimumValue( int fieldIndex ) const override;
636 QVariant maximumValue( int fieldIndex ) const override;
637 QgsRectangle sourceExtent() const override;
638 QgsFeatureIds allFeatureIds() const override;
639 SpatialIndexPresence hasSpatialIndex() const override;
640
644 QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
645
651 void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck method );
652
653 private:
654
655 QgsFeatureSource *mSource = nullptr;
656 bool mOwnsSource = false;
657 QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
658 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
659 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
660
661 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
662 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
663
664 long long mFeatureLimit = -1;
665 QString mFilterExpression;
666
667};
668
669#ifndef SIP_RUN
670
679{
680 public:
681
682
696 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
697 ~QgsProcessingFeatureSink() override;
698 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
699 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
700 bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
701
702 private:
703
704 QgsProcessingContext &mContext;
705 QString mSinkName;
706 bool mOwnsSink = false;
707
708};
709#endif
710
711#endif // QGSPROCESSINGUTILS_H
712
713
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition: qgis.h:155
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:83
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.
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