QGIS API Documentation 4.1.0-Master (60fea48833c)
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
439 static int outputDefinitionIndex( const QgsProcessingAlgorithm *algorithm, const QString &name ) SIP_HOLDGIL;
440
463 static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
464 bool selectedFeaturesOnly,
465 const QString &baseName,
466 const QStringList &compatibleFormats,
467 const QString &preferredFormat,
468 QgsProcessingContext &context,
469 QgsProcessingFeedback *feedback,
470 long long featureLimit = -1,
471 const QString &filterExpression = QString() );
472
507 static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
508 bool selectedFeaturesOnly,
509 const QString &baseName,
510 const QStringList &compatibleFormats,
511 const QString &preferredFormat,
512 QgsProcessingContext &context,
513 QgsProcessingFeedback *feedback,
514 QString &layerName SIP_OUT,
515 long long featureLimit = -1,
516 const QString &filterExpression = QString() );
517
528 static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() ) SIP_HOLDGIL;
529
535 static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields ) SIP_HOLDGIL;
536
541 static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields ) SIP_HOLDGIL;
542
555 static QString defaultVectorExtension();
556
567 static QString defaultRasterFormat();
568
581 static QString defaultRasterExtension();
582
594 static QString defaultPointCloudExtension();
595
607 static QString defaultVectorTileExtension();
608
615 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
616
624 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
625
634 static QString resolveDefaultEncoding( const QString &defaultEncoding = "System" );
635
636 private:
637 static bool canUseLayer( const QgsRasterLayer *layer );
638 static bool canUseLayer( const QgsMeshLayer *layer );
639 static bool canUseLayer( const QgsPluginLayer *layer );
640 static bool canUseLayer( const QgsVectorTileLayer *layer );
641 static bool canUseLayer( const QgsPointCloudLayer *layer );
642 static bool canUseLayer( const QgsAnnotationLayer *layer );
643 static bool canUseLayer( const QgsTiledSceneLayer *layer );
644 static bool canUseLayer( const QgsVectorLayer *layer,
645 const QList< int > &sourceTypes = QList< int >() );
646
659 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
660
671 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
672
683 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
684
693 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
694
695 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
696
697 friend class TestQgsProcessing;
699
700};
701
709{
710 public:
711
724 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
725 long long featureLimit = -1, const QString &filterExpression = QString() );
726
728
735
736 Qgis::FeatureAvailability hasFeatures() const override;
737
738 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
740 QgsFields fields() const override;
741 Qgis::WkbType wkbType() const override;
742 long long featureCount() const override;
743 QString sourceName() const override;
744 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
745 QVariant minimumValue( int fieldIndex ) const override;
746 QVariant maximumValue( int fieldIndex ) const override;
747 QgsRectangle sourceExtent() const override;
748 QgsFeatureIds allFeatureIds() const override;
750
755
763
771
772 private:
773
774 QgsFeatureSource *mSource = nullptr;
775 bool mOwnsSource = false;
777 QgsFields mSourceFields;
778 Qgis::WkbType mSourceWkbType = Qgis::WkbType::Unknown;
779 QString mSourceName;
780 QgsRectangle mSourceExtent;
782
784 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
785 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
786
787 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
788 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
789
790 long long mFeatureLimit = -1;
791 QString mFilterExpression;
792
793};
794
795#ifndef SIP_RUN
796
804{
805 public:
806
807
821 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
822 ~QgsProcessingFeatureSink() override;
823
833 void finalize() override SIP_THROW( QgsProcessingException );
834
835 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
836 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
838
839 private:
840
841 QgsProcessingContext &mContext;
842 QString mSinkName;
843 bool mOwnsSink = false;
844
845};
846#endif
847
848#endif // QGSPROCESSINGUTILS_H
849
850
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition qgis.h:584
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
Definition qgis.h:585
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:603
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2319
@ NoCheck
No invalid geometry checking.
Definition qgis.h:2320
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:3839
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:113
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:266
#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