QGIS API Documentation  3.17.0-Master (a035f434f4)
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 QgsProject;
35 class QgsMapLayerStore;
39 class QgsVectorTileLayer;
40 
41 #include <QString>
42 #include <QVariant>
43 
50 class CORE_EXPORT QgsProcessingUtils
51 {
52  public:
53 
64  static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
65 
81  static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
82  const QList< int > &sourceTypes = QList< int >(),
83  bool sort = true );
84 
98  static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
99 
109  static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
110 
117  static QString encodeProviderKeyAndUri( const QString &providerKey, const QString &uri );
118 
130  static bool decodeProviderKeyAndUri( const QString &string, QString &providerKey SIP_OUT, QString &uri SIP_OUT );
131 
137  {
138  UnknownType,
139  Vector,
140  Raster,
141  Mesh,
142  };
143 
156  static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
157 
169  static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
170 
179  static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
180 
185  static QString normalizeLayerSource( const QString &source );
186 
193  static QString variantToPythonLiteral( const QVariant &value );
194 
200  static QString stringToPythonLiteral( const QString &string );
201 
219 #ifndef SIP_RUN
220  static QgsFeatureSink *createFeatureSink( QString &destination,
221  QgsProcessingContext &context,
222  const QgsFields &fields,
223  QgsWkbTypes::Type geometryType,
225  const QVariantMap &createOptions = QVariantMap(),
226  const QStringList &datasourceOptions = QStringList(),
227  const QStringList &layerOptions = QStringList(),
228  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags(),
229  QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
230 #endif
231 
251  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 );
252 
253 
260  static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
261 
267  Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
268 
275  static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
276 
281  static QString tempFolder();
282 
289  static QString generateTempFilename( const QString &basename );
290 
295  static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
296 
316  static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
317  bool selectedFeaturesOnly,
318  const QString &baseName,
319  const QStringList &compatibleFormats,
320  const QString &preferredFormat,
321  QgsProcessingContext &context,
322  QgsProcessingFeedback *feedback, long long featureLimit = -1 );
323 
357  static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
358  bool selectedFeaturesOnly,
359  const QString &baseName,
360  const QStringList &compatibleFormats,
361  const QString &preferredFormat,
362  QgsProcessingContext &context,
363  QgsProcessingFeedback *feedback,
364  QString &layerName SIP_OUT, long long featureLimit = -1 );
365 
376  static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() );
377 
383  static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
384 
389  static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
390 
401  static QString defaultVectorExtension();
402 
413  static QString defaultRasterExtension();
414 
415  private:
416  static bool canUseLayer( const QgsRasterLayer *layer );
417  static bool canUseLayer( const QgsMeshLayer *layer );
418  static bool canUseLayer( const QgsVectorTileLayer *layer );
419  static bool canUseLayer( const QgsVectorLayer *layer,
420  const QList< int > &sourceTypes = QList< int >() );
421 
432  static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
433 
444  static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType );
445 
454  Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
455 
456  static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
457 
458  friend class TestQgsProcessing;
459  friend class QgsProcessingProvider;
460 
461 };
462 
471 {
472  public:
473 
475  enum Flag
476  {
477  FlagSkipGeometryValidityChecks = 1 << 1,
478  };
479  Q_DECLARE_FLAGS( Flags, Flag )
480 
481 
491  QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
492  long long featureLimit = -1 );
493 
494  ~QgsProcessingFeatureSource() override;
495 
501  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
502 
503  QgsFeatureSource::FeatureAvailability hasFeatures() const override;
504 
505  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
506  QgsCoordinateReferenceSystem sourceCrs() const override;
507  QgsFields fields() const override;
508  QgsWkbTypes::Type wkbType() const override;
509  long featureCount() const override;
510  QString sourceName() const override;
511  QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
512  QVariant minimumValue( int fieldIndex ) const override;
513  QVariant maximumValue( int fieldIndex ) const override;
514  QgsRectangle sourceExtent() const override;
515  QgsFeatureIds allFeatureIds() const override;
516  SpatialIndexPresence hasSpatialIndex() const override;
517 
521  QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
522 
528  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck method );
529 
530  private:
531 
532  QgsFeatureSource *mSource = nullptr;
533  bool mOwnsSource = false;
534  QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
535  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
536  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
537 
538  std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
539  std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
540 
541  long long mFeatureLimit = -1;
542 
543 };
544 
545 #ifndef SIP_RUN
546 
555 {
556  public:
557 
558 
572  QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
573  ~QgsProcessingFeatureSink() override;
574  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
575  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
576  bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
577 
578  private:
579 
580  QgsProcessingContext &mContext;
581  QString mSinkName;
582  bool mOwnsSink = false;
583 
584 };
585 #endif
586 
587 #endif // QGSPROCESSINGUTILS_H
588 
589 
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
Base class for providing feedback from a processing algorithm.
A simple feature sink which proxies feature addition on to another feature sink.
Implements a map layer that is dedicated to rendering of vector tiles.
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
LayerHint
Layer type hints.
Represents a raster layer.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:583
An interface for objects which accept features via addFeature(s) methods.
QgsFeatureSource subclass which proxies methods to an underlying QgsFeatureSource, modifying results according to the settings in a QgsProcessingContext.
Container of fields for a vector layer.
Definition: qgsfields.h:44
#define SIP_MONKEYPATCH_SCOPEENUM
Definition: qgis_sip.h:256
Abstract base class for processing providers.
#define SIP_INOUT
Definition: qgis_sip.h:71
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
Abstract base class for processing algorithms.
Utility functions for use with processing classes.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:69
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:94
#define SIP_FACTORY
Definition: qgis_sip.h:76
Single scope for storing variables and functions for use within a QgsExpressionContext.
Contains information about the context in which a coordinate transform is executed.
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
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
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Defines the parameters used to remap features when creating a QgsRemappingProxyFeatureSink.
QgsProxyFeatureSink subclass which reports feature addition errors to a QgsProcessingContext.
An interface for objects which provide features via a getFeatures method.
#define SIP_OUT
Definition: qgis_sip.h:58
This class represents a coordinate reference system (CRS).
#define SIP_THROW(name)
Definition: qgis_sip.h:189
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:94
SpatialIndexPresence
Enumeration of spatial index presence states.
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
Represents a vector layer which manages a vector based data sets.
Contains information about the context in which a processing algorithm is executed.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81