QGIS API Documentation 3.99.0-Master (752b475928d)
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
562 static QString defaultRasterFormat();
563
576 static QString defaultRasterExtension();
577
589 static QString defaultPointCloudExtension();
590
602 static QString defaultVectorTileExtension();
603
610 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
611
619 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
620
629 static QString resolveDefaultEncoding( const QString &defaultEncoding = "System" );
630
631 private:
632 static bool canUseLayer( const QgsRasterLayer *layer );
633 static bool canUseLayer( const QgsMeshLayer *layer );
634 static bool canUseLayer( const QgsPluginLayer *layer );
635 static bool canUseLayer( const QgsVectorTileLayer *layer );
636 static bool canUseLayer( const QgsPointCloudLayer *layer );
637 static bool canUseLayer( const QgsAnnotationLayer *layer );
638 static bool canUseLayer( const QgsTiledSceneLayer *layer );
639 static bool canUseLayer( const QgsVectorLayer *layer,
640 const QList< int > &sourceTypes = QList< int >() );
641
654 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
655
666 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
667
678 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
679
688 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
689
690 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
691
692 friend class TestQgsProcessing;
694
695};
696
704{
705 public:
706
719 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
720 long long featureLimit = -1, const QString &filterExpression = QString() );
721
723
730
731 Qgis::FeatureAvailability hasFeatures() const override;
732
733 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
735 QgsFields fields() const override;
736 Qgis::WkbType wkbType() const override;
737 long long featureCount() const override;
738 QString sourceName() const override;
739 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
740 QVariant minimumValue( int fieldIndex ) const override;
741 QVariant maximumValue( int fieldIndex ) const override;
742 QgsRectangle sourceExtent() const override;
743 QgsFeatureIds allFeatureIds() const override;
745
750
758
766
767 private:
768
769 QgsFeatureSource *mSource = nullptr;
770 bool mOwnsSource = false;
772 QgsFields mSourceFields;
773 Qgis::WkbType mSourceWkbType = Qgis::WkbType::Unknown;
774 QString mSourceName;
775 QgsRectangle mSourceExtent;
777
779 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
780 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
781
782 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
783 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
784
785 long long mFeatureLimit = -1;
786 QString mFilterExpression;
787
788};
789
790#ifndef SIP_RUN
791
799{
800 public:
801
802
816 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
817 ~QgsProcessingFeatureSink() override;
818
828 void finalize() override SIP_THROW( QgsProcessingException );
829
830 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
831 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
833
834 private:
835
836 QgsProcessingContext &mContext;
837 QString mSinkName;
838 bool mOwnsSink = false;
839
840};
841#endif
842
843#endif // QGSPROCESSINGUTILS_H
844
845
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