QGIS API Documentation 3.43.0-Master (56aa1fd18d7)
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 "qgsprocessing.h"
27#include "qgsproxyfeaturesink.h"
29
30class QgsMeshLayer;
31class QgsPluginLayer;
32class QgsProject;
43
44#include <QString>
45#include <QVariant>
46
52class CORE_EXPORT QgsProcessingUtils
53{
54 Q_GADGET
55
56 public:
57
73 static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
74
95 static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
96 const QList< int > &sourceTypes = QList< int >(),
97 bool sort = true );
98
117 static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
118
137 static QList<QgsPluginLayer *> compatiblePluginLayers( QgsProject *project, bool sort = true );
138
157 static QList<QgsPointCloudLayer *> compatiblePointCloudLayers( QgsProject *project, bool sort = true );
158
177 static QList<QgsAnnotationLayer *> compatibleAnnotationLayers( QgsProject *project, bool sort = true );
178
197 static QList<QgsVectorTileLayer *> compatibleVectorTileLayers( QgsProject *project, bool sort = true );
198
217 static QList<QgsTiledSceneLayer *> compatibleTiledSceneLayers( QgsProject *project, bool sort = true );
218
228 static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
229
236 static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
237
249 static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
250
256 {
257 UnknownType,
258 Vector,
259 Raster,
260 Mesh,
261 PointCloud,
262 Annotation,
263 VectorTile,
264 TiledScene,
265 };
266 Q_ENUM( LayerHint )
267
268
280 static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
281
293 static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
294
303 static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
304
309 static QString normalizeLayerSource( const QString &source ) SIP_HOLDGIL;
310
318 static QString layerToStringIdentifier( const QgsMapLayer *layer, const QString &layerName = QString() ) SIP_HOLDGIL;
319
326 static QString variantToPythonLiteral( const QVariant &value );
327
333 static QString stringToPythonLiteral( const QString &string ) SIP_HOLDGIL;
334
352#ifndef SIP_RUN
353 static QgsFeatureSink *createFeatureSink( QString &destination,
354 QgsProcessingContext &context,
355 const QgsFields &fields,
356 Qgis::WkbType geometryType,
358 const QVariantMap &createOptions = QVariantMap(),
359 const QStringList &datasourceOptions = QStringList(),
360 const QStringList &layerOptions = QStringList(),
362 QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
363#endif
364
384 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 );
385
386
393 static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
394
400 Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
401
408 static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
409
414 static QString tempFolder( const QgsProcessingContext *context = nullptr );
415
422 static QString generateTempFilename( const QString &basename, const QgsProcessingContext *context = nullptr );
423
428 static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
429
435 static int outputDefinitionIndex( const QgsProcessingAlgorithm *algorithm, const QString &name ) SIP_HOLDGIL;
436
459 static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
460 bool selectedFeaturesOnly,
461 const QString &baseName,
462 const QStringList &compatibleFormats,
463 const QString &preferredFormat,
464 QgsProcessingContext &context,
465 QgsProcessingFeedback *feedback,
466 long long featureLimit = -1,
467 const QString &filterExpression = QString() );
468
503 static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
504 bool selectedFeaturesOnly,
505 const QString &baseName,
506 const QStringList &compatibleFormats,
507 const QString &preferredFormat,
508 QgsProcessingContext &context,
509 QgsProcessingFeedback *feedback,
510 QString &layerName SIP_OUT,
511 long long featureLimit = -1,
512 const QString &filterExpression = QString() );
513
524 static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() ) SIP_HOLDGIL;
525
531 static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields ) SIP_HOLDGIL;
532
537 static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields ) SIP_HOLDGIL;
538
551 static QString defaultVectorExtension();
552
565 static QString defaultRasterExtension();
566
578 static QString defaultPointCloudExtension();
579
591 static QString defaultVectorTileExtension();
592
599 static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
600
608 static QVariantMap preprocessQgisProcessParameters( const QVariantMap &parameters, bool &ok, QString &error );
609
618 static QString resolveDefaultEncoding( const QString &defaultEncoding = "System" );
619
620 private:
621 static bool canUseLayer( const QgsRasterLayer *layer );
622 static bool canUseLayer( const QgsMeshLayer *layer );
623 static bool canUseLayer( const QgsPluginLayer *layer );
624 static bool canUseLayer( const QgsVectorTileLayer *layer );
625 static bool canUseLayer( const QgsPointCloudLayer *layer );
626 static bool canUseLayer( const QgsAnnotationLayer *layer );
627 static bool canUseLayer( const QgsTiledSceneLayer *layer );
628 static bool canUseLayer( const QgsVectorLayer *layer,
629 const QList< int > &sourceTypes = QList< int >() );
630
643 template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
644
655 static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
656
667 static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags = QgsProcessing::LayerOptionsFlags() );
668
677 Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
678
679 static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
680
681 friend class TestQgsProcessing;
683
684};
685
693{
694 public:
695
708 QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
709 long long featureLimit = -1, const QString &filterExpression = QString() );
710
712
718 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Qgis::ProcessingFeatureSourceFlags flags ) const;
719
720 Qgis::FeatureAvailability hasFeatures() const override;
721
722 QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
723 QgsCoordinateReferenceSystem sourceCrs() const override;
724 QgsFields fields() const override;
725 Qgis::WkbType wkbType() const override;
726 long long featureCount() const override;
727 QString sourceName() const override;
728 QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
729 QVariant minimumValue( int fieldIndex ) const override;
730 QVariant maximumValue( int fieldIndex ) const override;
731 QgsRectangle sourceExtent() const override;
732 QgsFeatureIds allFeatureIds() const override;
733 Qgis::SpatialIndexPresence hasSpatialIndex() const override;
734
738 QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
739
746 void setInvalidGeometryCheck( Qgis::InvalidGeometryCheck method );
747
754 Qgis::InvalidGeometryCheck invalidGeometryCheck() const;
755
756 private:
757
758 QgsFeatureSource *mSource = nullptr;
759 bool mOwnsSource = false;
761 QgsFields mSourceFields;
762 Qgis::WkbType mSourceWkbType = Qgis::WkbType::Unknown;
763 QString mSourceName;
764 QgsRectangle mSourceExtent;
766
768 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
769 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
770
771 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
772 std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
773
774 long long mFeatureLimit = -1;
775 QString mFilterExpression;
776
777};
778
779#ifndef SIP_RUN
780
788{
789 public:
790
791
805 QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
806 ~QgsProcessingFeatureSink() override;
807
817 void finalize() override SIP_THROW( QgsProcessingException );
818
819 bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
820 bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
821 bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
822
823 private:
824
825 QgsProcessingContext &mContext;
826 QString mSinkName;
827 bool mOwnsSink = false;
828
829};
830#endif
831
832#endif // QGSPROCESSINGUTILS_H
833
834
SpatialIndexPresence
Enumeration of spatial index presence states.
Definition qgis.h:522
@ Unknown
Spatial index presence cannot be determined, index may or may not exist.
FeatureAvailability
Possible return value for QgsFeatureSource::hasFeatures() to determine if a source is empty.
Definition qgis.h:541
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2188
@ NoCheck
No invalid geometry checking.
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:256
@ Unknown
Unknown.
QFlags< ProcessingFeatureSourceFlag > ProcessingFeatureSourceFlags
Flags which control how QgsProcessingFeatureSource fetches features.
Definition qgis.h:3588
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:77
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.
QgsProxyFeatureSink subclass which reports feature addition errors to a QgsProcessingContext.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
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.
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:107
A simple feature sink which proxies feature addition onto another feature 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:106
#define SIP_PYNAME(name)
Definition qgis_sip.h:81
#define SIP_MONKEYPATCH_SCOPEENUM
Definition qgis_sip.h:270
#define SIP_OUT
Definition qgis_sip.h:58
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
#define SIP_HOLDGIL
Definition qgis_sip.h:171
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_INOUT
Definition qgis_sip.h:71
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds
const QgsCoordinateReferenceSystem & crs