QGIS API Documentation 3.99.0-Master (26c88405ac0)
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:
56
72 static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
73
94 static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
95 const QList< int > &sourceTypes = QList< int >(),
96 bool sort = true );
97
116 static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
117
136 static QList<QgsPluginLayer *> compatiblePluginLayers( QgsProject *project, bool sort = true );
137
156 static QList<QgsPointCloudLayer *> compatiblePointCloudLayers( QgsProject *project, bool sort = true );
157
176 static QList<QgsAnnotationLayer *> compatibleAnnotationLayers( QgsProject *project, bool sort = true );
177
196 static QList<QgsVectorTileLayer *> compatibleVectorTileLayers( QgsProject *project, bool sort = true );
197
216 static QList<QgsTiledSceneLayer *> compatibleTiledSceneLayers( QgsProject *project, bool sort = true );
217
227 static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
228
235 static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
236
248 static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
249
255 {
256 UnknownType,
257 Vector,
258 Raster,
259 Mesh,
260 PointCloud,
261 Annotation,
262 VectorTile,
263 TiledScene,
264 };
265 Q_ENUM( LayerHint )
266
267
279 static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
280
292 static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
293
302 static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
303
308 static QString normalizeLayerSource( const QString &source ) SIP_HOLDGIL;
309
317 static QString layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName = QString() ) SIP_HOLDGIL;
318
325 static QString variantToPythonLiteral( const QVariant &value );
326
332 static QString stringToPythonLiteral( const QString &string ) SIP_HOLDGIL;
333
351#ifndef SIP_RUN
352 static QgsFeatureSink *createFeatureSink( QString &destination,
353 QgsProcessingContext &context,
354 const QgsFields &fields,
355 Qgis::WkbType geometryType,
357 const QVariantMap &createOptions = QVariantMap(),
358 const QStringList &datasourceOptions = QStringList(),
359 const QStringList &layerOptions = QStringList(),
361 QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
362#endif
363
383 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 );
384
385
392 static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
393
399 Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
400
407 static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
408
413 static QString tempFolder( const QgsProcessingContext *context = nullptr );
414
421 static QString generateTempFilename( const QString &basename, const QgsProcessingContext *context = nullptr );
422
427 static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
428
434 static int outputDefinitionIndex( const QgsProcessingAlgorithm *algorithm, const QString &name ) SIP_HOLDGIL;
435
458 static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
459 bool selectedFeaturesOnly,
460 const QString &baseName,
461 const QStringList &compatibleFormats,
462 const QString &preferredFormat,
463 QgsProcessingContext &context,
464 QgsProcessingFeedback *feedback,
465 long long featureLimit = -1,
466 const QString &filterExpression = QString() );
467
502 static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
503 bool selectedFeaturesOnly,
504 const QString &baseName,
505 const QStringList &compatibleFormats,
506 const QString &preferredFormat,
507 QgsProcessingContext &context,
508 QgsProcessingFeedback *feedback,
509 QString &layerName SIP_OUT,
510 long long featureLimit = -1,
511 const QString &filterExpression = QString() );
512
523 static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() ) SIP_HOLDGIL;
524
530 static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields ) SIP_HOLDGIL;
531
536 static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields ) SIP_HOLDGIL;
537
550 static QString defaultVectorExtension();
551
564 static QString defaultRasterExtension();
565
577 static QString defaultPointCloudExtension();
578
590 static QString defaultVectorTileExtension();
591
598 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
599
607 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
608
617 static QString resolveDefaultEncoding( const QString &defaultEncoding = "System" );
618
619 private:
620 static bool canUseLayer( const QgsRasterLayer *layer );
621 static bool canUseLayer( const QgsMeshLayer *layer );
622 static bool canUseLayer( const QgsPluginLayer *layer );
623 static bool canUseLayer( const QgsVectorTileLayer *layer );
624 static bool canUseLayer( const QgsPointCloudLayer *layer );
625 static bool canUseLayer( const QgsAnnotationLayer *layer );
626 static bool canUseLayer( const QgsTiledSceneLayer *layer );
627 static bool canUseLayer( const QgsVectorLayer *layer,
628 const QList< int > &sourceTypes = QList< int >() );
629
642 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
643
654 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
655
666 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
667
676 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
677
678 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
679
680 friend class TestQgsProcessing;
682
683};
684
692{
693 public:
694
707 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
708 long long featureLimit = -1, const QString &filterExpression = QString() );
709
711
718
719 Qgis::FeatureAvailability hasFeatures() const override;
720
721 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
723 QgsFields fields() const override;
724 Qgis::WkbType wkbType() const override;
725 long long featureCount() const override;
726 QString sourceName() const override;
727 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
728 QVariant minimumValue( int fieldIndex ) const override;
729 QVariant maximumValue( int fieldIndex ) const override;
730 QgsRectangle sourceExtent() const override;
731 QgsFeatureIds allFeatureIds() const override;
733
738
746
754
755 private:
756
757 QgsFeatureSource *mSource = nullptr;
758 bool mOwnsSource = false;
760 QgsFields mSourceFields;
761 Qgis::WkbType mSourceWkbType = Qgis::WkbType::Unknown;
762 QString mSourceName;
763 QgsRectangle mSourceExtent;
765
767 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
768 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
769
770 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
771 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
772
773 long long mFeatureLimit = -1;
774 QString mFilterExpression;
775
776};
777
778#ifndef SIP_RUN
779
787{
788 public:
789
790
804 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
805 ~QgsProcessingFeatureSink() override;
806
816 void finalize() override SIP_THROW( QgsProcessingException );
817
818 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
819 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
821
822 private:
823
824 QgsProcessingContext &mContext;
825 QString mSinkName;
826 bool mOwnsSink = false;
827
828};
829#endif
830
831#endif // QGSPROCESSINGUTILS_H
832
833
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition qgis.h:558
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
Definition qgis.h:559
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:577
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2238
@ NoCheck
No invalid geometry checking.
Definition qgis.h:2239
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:277
@ Unknown
Unknown.
Definition qgis.h:278
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3722
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:58
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:80
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:109
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:114
#define SIP_PYNAME(name)
Definition qgis_sip.h:89
#define SIP_MONKEYPATCH_SCOPEENUM
Definition qgis_sip.h:267
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
#define SIP_HOLDGIL
Definition qgis_sip.h:179
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_INOUT
Definition qgis_sip.h:79
#define SIP_THROW(name,...)
Definition qgis_sip.h:211
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds