QGIS API Documentation 4.1.0-Master (31622b25bb0)
Loading...
Searching...
No Matches
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#include "qgsfeaturesink.h"
23#include "qgsfeaturesource.h"
24#include "qgsprocessing.h"
25#include "qgsproxyfeaturesink.h"
26#include "qgsrasterlayer.h"
28
29class QgsMeshLayer;
30class QgsPluginLayer;
31class QgsProject;
42
43#include <QString>
44#include <QVariant>
45
51class CORE_EXPORT QgsProcessingUtils
52{
53 Q_GADGET
54
55 public:
71 static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
72
93 static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project, const QList< int > &sourceTypes = QList< int >(), bool sort = true );
94
113 static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
114
133 static QList<QgsPluginLayer *> compatiblePluginLayers( QgsProject *project, bool sort = true );
134
153 static QList<QgsPointCloudLayer *> compatiblePointCloudLayers( QgsProject *project, bool sort = true );
154
173 static QList<QgsAnnotationLayer *> compatibleAnnotationLayers( QgsProject *project, bool sort = true );
174
193 static QList<QgsVectorTileLayer *> compatibleVectorTileLayers( QgsProject *project, bool sort = true );
194
213 static QList<QgsTiledSceneLayer *> compatibleTiledSceneLayers( QgsProject *project, bool sort = true );
214
224 static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
225
232 static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
233
245 static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
246
252 {
253 UnknownType,
254 Vector,
255 Raster,
256 Mesh,
257 PointCloud,
258 Annotation,
259 VectorTile,
260 TiledScene,
261 };
262 Q_ENUM( LayerHint )
263
264
276 static QgsMapLayer *mapLayerFromString(
277 const QString &string,
278 QgsProcessingContext &context,
279 bool allowLoadingNewLayers = true,
282 );
283
295 static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
296
305 static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
306
307 // clang-format off
312 static QString normalizeLayerSource( const QString &source ) SIP_HOLDGIL;
313 // clang-format on
314
322 static QString layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName = QString() ) SIP_HOLDGIL;
323
330 static QString variantToPythonLiteral( const QVariant &value );
331
337 static QString stringToPythonLiteral( const QString &string ) SIP_HOLDGIL;
338
356#ifndef SIP_RUN
357 static QgsFeatureSink *createFeatureSink( QString &destination,
358 QgsProcessingContext &context,
359 const QgsFields &fields,
360 Qgis::WkbType geometryType,
362 const QVariantMap &createOptions = QVariantMap(),
363 const QStringList &datasourceOptions = QStringList(),
364 const QStringList &layerOptions = QStringList(),
366 QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
367#endif
368
388 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 );
389
390
397 static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
398
404 Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
405
412 static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
413
418 static QString tempFolder( const QgsProcessingContext *context = nullptr );
419
426 static QString generateTempFilename( const QString &basename, const QgsProcessingContext *context = nullptr );
427
432 static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
433
442 static int parameterDefinitionIndex( const QgsProcessingAlgorithm *algorithm, const QString &name ) SIP_HOLDGIL;
443
452 static int outputDefinitionIndex( const QgsProcessingAlgorithm *algorithm, const QString &name ) SIP_HOLDGIL;
453
476 static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
477 bool selectedFeaturesOnly,
478 const QString &baseName,
479 const QStringList &compatibleFormats,
480 const QString &preferredFormat,
481 QgsProcessingContext &context,
482 QgsProcessingFeedback *feedback,
483 long long featureLimit = -1,
484 const QString &filterExpression = QString() );
485
520 static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
521 bool selectedFeaturesOnly,
522 const QString &baseName,
523 const QStringList &compatibleFormats,
524 const QString &preferredFormat,
525 QgsProcessingContext &context,
526 QgsProcessingFeedback *feedback,
527 QString &layerName SIP_OUT,
528 long long featureLimit = -1,
529 const QString &filterExpression = QString() );
530
541 static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() ) SIP_HOLDGIL;
542
548 static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields ) SIP_HOLDGIL;
549
554 static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields ) SIP_HOLDGIL;
555
568 static QString defaultVectorExtension();
569
580 static QString defaultRasterFormat();
581
594 static QString defaultRasterExtension();
595
607 static QString defaultPointCloudExtension();
608
620 static QString defaultVectorTileExtension();
621
628 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
629
637 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
638
647 static QString resolveDefaultEncoding( const QString &defaultEncoding = "System" );
648
649 private:
650 static bool canUseLayer( const QgsRasterLayer *layer );
651 static bool canUseLayer( const QgsMeshLayer *layer );
652 static bool canUseLayer( const QgsPluginLayer *layer );
653 static bool canUseLayer( const QgsVectorTileLayer *layer );
654 static bool canUseLayer( const QgsPointCloudLayer *layer );
655 static bool canUseLayer( const QgsAnnotationLayer *layer );
656 static bool canUseLayer( const QgsTiledSceneLayer *layer );
657 static bool canUseLayer( const QgsVectorLayer *layer,
658 const QList< int > &sourceTypes = QList< int >() );
659
672 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
673
684 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
685
696 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
697
706 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
707
708 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
709
710 friend class TestQgsProcessing;
712
713};
714
722{
723 public:
724
737 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
738 long long featureLimit = -1, const QString &filterExpression = QString() );
739
741
748
749 Qgis::FeatureAvailability hasFeatures() const override;
750
751 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
753 QgsFields fields() const override;
754 Qgis::WkbType wkbType() const override;
755 long long featureCount() const override;
756 QString sourceName() const override;
757 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
758 QVariant minimumValue( int fieldIndex ) const override;
759 QVariant maximumValue( int fieldIndex ) const override;
760 QgsRectangle sourceExtent() const override;
761 QgsFeatureIds allFeatureIds() const override;
763
768
776
784
785 private:
786
787 QgsFeatureSource *mSource = nullptr;
788 bool mOwnsSource = false;
790 QgsFields mSourceFields;
791 Qgis::WkbType mSourceWkbType = Qgis::WkbType::Unknown;
792 QString mSourceName;
793 QgsRectangle mSourceExtent;
795
797 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
798 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
799
800 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
801 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
802
803 long long mFeatureLimit = -1;
804 QString mFilterExpression;
805
806};
807
808#ifndef SIP_RUN
809
817{
818 public:
819
820
834 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
835 ~QgsProcessingFeatureSink() override;
836
846 void finalize() override SIP_THROW( QgsProcessingException );
847
848 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
849 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
851
852 private:
853
854 QgsProcessingContext &mContext;
855 QString mSinkName;
856 bool mOwnsSink = false;
857
858};
859#endif
860
861#endif // QGSPROCESSINGUTILS_H
862
863
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition qgis.h:585
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
Definition qgis.h:586
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:604
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2372
@ NoCheck
No invalid geometry checking.
Definition qgis.h:2373
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:294
@ Unknown
Unknown.
Definition qgis.h:295
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3906
Represents a map layer containing a set of georeferenced annotations, e.g.
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.
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.
QFlags< SinkFlag > SinkFlags
QFlags< Flag > Flags
An interface for objects which provide features via a getFeatures method.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
Container of fields for a vector layer.
Definition qgsfields.h:46
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:83
Represents a mesh layer supporting display of data on structured or unstructured meshes.
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.
void finalize() override
Finalizes the sink, flushing any buffered features to the destination.
QgsProcessingFeatureSink(QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink=false)
Constructor for QgsProcessingFeatureSink, accepting an original feature sink originalSink and process...
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
QgsRectangle sourceExtent() const override
Returns the extent of all geometries from the source.
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const override
Returns the set of unique values contained within the specified fieldIndex from this source.
QgsExpressionContextScope * createExpressionContextScope() const
Returns an expression context scope suitable for this source.
QgsProcessingFeatureSource(QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource=false, long long featureLimit=-1, const QString &filterExpression=QString())
Constructor for QgsProcessingFeatureSource, accepting an original feature source originalSource and p...
void setInvalidGeometryCheck(Qgis::InvalidGeometryCheck method)
Overrides the default geometry check method for the source.
Qgis::InvalidGeometryCheck invalidGeometryCheck() const
Returns the geometry check method for the source.
QVariant maximumValue(int fieldIndex) const override
Returns the maximum value for an attribute column or an invalid variant in case of error.
QgsCoordinateReferenceSystem sourceCrs() const override
Returns the coordinate reference system for features in the source.
Qgis::WkbType wkbType() const override
Returns the geometry type for features returned by this source.
QVariant minimumValue(int fieldIndex) const override
Returns the minimum value for an attribute column or an invalid variant in case of error.
long long featureCount() const override
Returns the number of features contained in the source, or -1 if the feature count is unknown.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request, Qgis::ProcessingFeatureSourceFlags flags) const
Returns an iterator for the features in the source, respecting the supplied feature flags.
Qgis::FeatureAvailability hasFeatures() const override
Determines if there are any features available in the source.
QString sourceName() const override
Returns a friendly display name for the source.
QgsFeatureIds allFeatureIds() const override
Returns a list of all feature IDs for features present in the source.
Qgis::SpatialIndexPresence hasSpatialIndex() const override
Returns an enum value representing the presence of a valid spatial index on the source,...
QgsFields fields() const override
Returns the fields associated with features in the source.
Base class for providing feedback from a processing algorithm.
Utility functions for use with processing classes.
static QList< QgsTiledSceneLayer * > compatibleTiledSceneLayers(QgsProject *project, bool sort=true)
Returns a list of tiled scene layers from a project which are compatible with the processing framewor...
friend class TestQgsProcessing
static QList< QgsAnnotationLayer * > compatibleAnnotationLayers(QgsProject *project, bool sort=true)
Returns a list of annotation layers from a project which are compatible with the processing framework...
static QString encodeProviderKeyAndUri(const QString &providerKey, const QString &uri)
Encodes a provider key and layer uri to a single string, for use with decodeProviderKeyAndUri().
LayerHint
Layer type hints.
static QList< QgsRasterLayer * > compatibleRasterLayers(QgsProject *project, bool sort=true)
Returns a list of raster layers from a project which are compatible with the processing framework.
static QList< QgsPluginLayer * > compatiblePluginLayers(QgsProject *project, bool sort=true)
Returns a list of plugin layers from a project which are compatible with the processing framework.
static QList< QgsVectorLayer * > compatibleVectorLayers(QgsProject *project, const QList< int > &sourceTypes=QList< int >(), bool sort=true)
Returns a list of vector layers from a project which are compatible with the processing framework.
static bool decodeProviderKeyAndUri(const QString &string, QString &providerKey, QString &uri)
Decodes a provider key and layer uri from an encoded string, for use with encodeProviderKeyAndUri().
static QList< QgsVectorTileLayer * > compatibleVectorTileLayers(QgsProject *project, bool sort=true)
Returns a list of vector tile layers from a project which are compatible with the processing framewor...
static QList< QgsMapLayer * > compatibleLayers(QgsProject *project, bool sort=true)
Returns a list of map layers from a project which are compatible with the processing framework.
friend class QgsProcessingProvider
static QList< QgsPointCloudLayer * > compatiblePointCloudLayers(QgsProject *project, bool sort=true)
Returns a list of point cloud layers from a project which are compatible with the processing framewor...
static QList< QgsMeshLayer * > compatibleMeshLayers(QgsProject *project, bool sort=true)
Returns a list of mesh layers from a project which are compatible with the processing framework.
Contains enumerations and other constants for use in processing algorithms and parameters.
QFlags< LayerOptionsFlag > LayerOptionsFlags
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
QgsProxyFeatureSink(QgsFeatureSink *sink)
Constructs a new QgsProxyFeatureSink which forwards features onto a destination sink.
Represents a raster layer.
A rectangle specified with double values.
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
Represents a map layer supporting display of tiled scene objects.
Represents a vector layer which manages a vector based dataset.
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:113
#define SIP_PYNAME(name)
Definition qgis_sip.h:88
#define SIP_MONKEYPATCH_SCOPEENUM
Definition qgis_sip.h:264
#define SIP_OUT
Definition qgis_sip.h:57
#define SIP_TRANSFERBACK
Definition qgis_sip.h:47
#define SIP_HOLDGIL
Definition qgis_sip.h:178
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_INOUT
Definition qgis_sip.h:78
#define SIP_THROW(name,...)
Definition qgis_sip.h:210
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds