QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 "qgsmessagelog.h"
25 #include "qgsspatialindex.h"
26 #include "qgsprocessing.h"
27 #include "qgsfeaturesink.h"
28 #include "qgsfeaturesource.h"
29 #include "qgsproxyfeaturesink.h"
31 
32 class QgsMeshLayer;
33 class QgsPluginLayer;
34 class QgsProject;
36 class QgsMapLayerStore;
40 class QgsVectorTileLayer;
41 class QgsPointCloudLayer;
42 class QgsAnnotationLayer;
43 
44 #include <QString>
45 #include <QVariant>
46 
53 class CORE_EXPORT QgsProcessingUtils
54 {
55  public:
56 
70  static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
71 
90  static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
91  const QList< int > &sourceTypes = QList< int >(),
92  bool sort = true );
93 
110  static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
111 
128  static QList<QgsPluginLayer *> compatiblePluginLayers( QgsProject *project, bool sort = true );
129 
146  static QList<QgsPointCloudLayer *> compatiblePointCloudLayers( QgsProject *project, bool sort = true );
147 
164  static QList<QgsAnnotationLayer *> compatibleAnnotationLayers( QgsProject *project, bool sort = true );
165 
175  static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
176 
183  static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
184 
196  static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
197 
203  {
204  UnknownType,
205  Vector,
206  Raster,
207  Mesh,
208  PointCloud,
209  Annotation,
210  };
211 
224  static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
225 
237  static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
238 
247  static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
248 
253  static QString normalizeLayerSource( const QString &source );
254 
261  static QString variantToPythonLiteral( const QVariant &value );
262 
268  static QString stringToPythonLiteral( const QString &string );
269 
287 #ifndef SIP_RUN
288  static QgsFeatureSink *createFeatureSink( QString &destination,
289  QgsProcessingContext &context,
290  const QgsFields &fields,
291  QgsWkbTypes::Type geometryType,
293  const QVariantMap &createOptions = QVariantMap(),
294  const QStringList &datasourceOptions = QStringList(),
295  const QStringList &layerOptions = QStringList(),
296  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags(),
297  QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
298 #endif
299 
319  static void createFeatureSinkPython( QgsFeatureSink **sink SIP_OUT SIP_TRANSFERBACK, QString &destination SIP_INOUT, QgsProcessingContext &context, const QgsFields &fields, QgsWkbTypes::Type geometryType, const QgsCoordinateReferenceSystem &crs, const QVariantMap &createOptions = QVariantMap() ) SIP_THROW( QgsProcessingException ) SIP_PYNAME( createFeatureSink );
320 
321 
328  static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
329 
335  Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
336 
343  static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
344 
349  static QString tempFolder();
350 
357  static QString generateTempFilename( const QString &basename );
358 
363  static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
364 
384  static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
385  bool selectedFeaturesOnly,
386  const QString &baseName,
387  const QStringList &compatibleFormats,
388  const QString &preferredFormat,
389  QgsProcessingContext &context,
390  QgsProcessingFeedback *feedback, long long featureLimit = -1 );
391 
425  static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
426  bool selectedFeaturesOnly,
427  const QString &baseName,
428  const QStringList &compatibleFormats,
429  const QString &preferredFormat,
430  QgsProcessingContext &context,
431  QgsProcessingFeedback *feedback,
432  QString &layerName SIP_OUT, long long featureLimit = -1 );
433 
444  static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() );
445 
451  static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
452 
457  static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
458 
470  static QString defaultVectorExtension();
471 
483  static QString defaultRasterExtension();
484 
495  static QString defaultPointCloudExtension();
496 
503  static QVariantMap removePointerValuesFromMap( const QVariantMap &map );
504 
505  private:
506  static bool canUseLayer( const QgsRasterLayer *layer );
507  static bool canUseLayer( const QgsMeshLayer *layer );
508  static bool canUseLayer( const QgsPluginLayer *layer );
509  static bool canUseLayer( const QgsVectorTileLayer *layer );
510  static bool canUseLayer( const QgsPointCloudLayer *layer );
511  static bool canUseLayer( const QgsAnnotationLayer *layer );
512  static bool canUseLayer( const QgsVectorLayer *layer,
513  const QList< int > &sourceTypes = QList< int >() );
514 
527  template< typename T> static QList< T * > compatibleMapLayers( QgsProject *project, bool sort = true );
528 
539  static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
540 
551  static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType );
552 
561  Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
562 
563  static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
564 
565  friend class TestQgsProcessing;
566  friend class QgsProcessingProvider;
567 
568 };
569 
578 {
579  public:
580 
582  enum Flag
583  {
584  FlagSkipGeometryValidityChecks = 1 << 1,
585  };
586  Q_DECLARE_FLAGS( Flags, Flag )
587 
588 
598  QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
599  long long featureLimit = -1 );
600 
601  ~QgsProcessingFeatureSource() override;
602 
608  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
609 
610  QgsFeatureSource::FeatureAvailability hasFeatures() const override;
611 
612  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
613  QgsCoordinateReferenceSystem sourceCrs() const override;
614  QgsFields fields() const override;
615  QgsWkbTypes::Type wkbType() const override;
616  long long featureCount() const override;
617  QString sourceName() const override;
618  QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
619  QVariant minimumValue( int fieldIndex ) const override;
620  QVariant maximumValue( int fieldIndex ) const override;
621  QgsRectangle sourceExtent() const override;
622  QgsFeatureIds allFeatureIds() const override;
623  SpatialIndexPresence hasSpatialIndex() const override;
624 
628  QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
629 
635  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck method );
636 
637  private:
638 
639  QgsFeatureSource *mSource = nullptr;
640  bool mOwnsSource = false;
641  QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
642  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
643  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
644 
645  std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
646  std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
647 
648  long long mFeatureLimit = -1;
649 
650 };
651 
652 #ifndef SIP_RUN
653 
662 {
663  public:
664 
665 
679  QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
680  ~QgsProcessingFeatureSink() override;
681  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
682  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
683  bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
684 
685  private:
686 
687  QgsProcessingContext &mContext;
688  QString mSinkName;
689  bool mOwnsSink = false;
690 
691 };
692 #endif
693 
694 #endif // QGSPROCESSINGUTILS_H
695 
696 
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsVectorTileLayer
Implements a map layer that is dedicated to rendering of vector tiles. Vector tiles compared to "ordi...
Definition: qgsvectortilelayer.h:84
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
qgsrasterlayer.h
QgsRemappingSinkDefinition
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
Definition: qgsremappingproxyfeaturesink.h:38
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
algorithm
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
QgsPointCloudLayer
Represents a map layer supporting display of point clouds.
Definition: qgspointcloudlayer.h:45
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsProxyFeatureSink
A simple feature sink which proxies feature addition on to another feature sink.
Definition: qgsproxyfeaturesink.h:39
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsProcessingProvider
Abstract base class for processing providers.
Definition: qgsprocessingprovider.h:35
qgsfeaturesink.h
QgsFeatureSource
An interface for objects which provide features via a getFeatures method.
Definition: qgsfeaturesource.h:37
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
QgsProcessingUtils::LayerHint
LayerHint
Layer type hints.
Definition: qgsprocessingutils.h:202
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:103
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsProxyFeatureSink::addFeature
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
Definition: qgsproxyfeaturesink.h:47
QgsMapLayerStore
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Definition: qgsmaplayerstore.h:35
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:198
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
QgsProxyFeatureSink::addFeatures
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Definition: qgsproxyfeaturesink.h:48
QgsMeshLayer
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:98
QgsProcessingUtils
Utility functions for use with processing classes.
Definition: qgsprocessingutils.h:53
qgsfeaturesource.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
qgsproxyfeaturesink.h
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:76
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:113
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
QgsProcessingFeatureSink
QgsProxyFeatureSink subclass which reports feature addition errors to a QgsProcessingContext.
Definition: qgsprocessingutils.h:661
QgsFeatureSource::FeatureAvailability
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Definition: qgsfeaturesource.h:50
QgsPluginLayer
Base class for plugin layers. These can be implemented by plugins and registered in QgsPluginLayerReg...
Definition: qgspluginlayer.h:31
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:582
QgsProcessingAlgorithm
Abstract base class for processing algorithms.
Definition: qgsprocessingalgorithm.h:52
qgsprocessing.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsAnnotationLayer
Represents a map layer containing a set of georeferenced annotations, e.g. markers,...
Definition: qgsannotationlayer.h:46
QgsProcessingFeatureSource
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource,...
Definition: qgsprocessingutils.h:577
qgsspatialindex.h
qgsremappingproxyfeaturesink.h
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsFeatureSink
An interface for objects which accept features via addFeature(s) methods.
Definition: qgsfeaturesink.h:33
QgsProcessingUtils::LayerHint::UnknownType
@ UnknownType
Unknown layer type.
qgsmessagelog.h
SIP_MONKEYPATCH_SCOPEENUM
#define SIP_MONKEYPATCH_SCOPEENUM
Definition: qgis_sip.h:265