QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
30 class QgsProject;
32 class QgsMapLayerStore;
35 
36 #include <QString>
37 #include <QVariant>
38 
45 class CORE_EXPORT QgsProcessingUtils
46 {
47  public:
48 
58  static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
59 
74  static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
75  const QList< int > &sourceTypes = QList< int >(),
76  bool sort = true );
77 
87  static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
88 
93  enum LayerHint
94  {
98  };
99 
112  static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, LayerHint typeHint = UnknownType );
113 
125  static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
126 
131  static QString normalizeLayerSource( const QString &source );
132 
136  static QString stringToPythonLiteral( const QString &string );
137 
155 #ifndef SIP_RUN
156  static QgsFeatureSink *createFeatureSink( QString &destination,
157  QgsProcessingContext &context,
158  const QgsFields &fields,
159  QgsWkbTypes::Type geometryType,
161  const QVariantMap &createOptions = QVariantMap(),
162  QgsFeatureSink::SinkFlags sinkFlags = nullptr ) SIP_FACTORY;
163 #endif
164 
184  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 );
185 
190  static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
191 
198  static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
199 
204  static QString tempFolder();
205 
212  static QString generateTempFilename( const QString &basename );
213 
218  static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
219 
232  static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
233  bool selectedFeaturesOnly,
234  const QString &baseName,
235  const QStringList &compatibleFormats,
236  const QString &preferredFormat,
237  QgsProcessingContext &context,
238  QgsProcessingFeedback *feedback );
239 
250  static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB );
251 
257  static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
258 
263  static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
264 
265  private:
266 
267  static bool canUseLayer( const QgsRasterLayer *layer );
268  static bool canUseLayer( const QgsVectorLayer *layer,
269  const QList< int > &sourceTypes = QList< int >() );
270 
281  static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, LayerHint typeHint = UnknownType );
282 
289  static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = UnknownType );
290 
291  static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
292 
293  friend class TestQgsProcessing;
294  friend class QgsProcessingProvider;
295 
296 };
297 
306 {
307  public:
308 
310  enum Flag
311  {
312  FlagSkipGeometryValidityChecks = 1 << 1,
313  };
314  Q_DECLARE_FLAGS( Flags, Flag )
315 
316 
323  QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false );
324 
325  ~QgsProcessingFeatureSource() override;
326 
332  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
333 
334  QgsFeatureSource::FeatureAvailability hasFeatures() const override;
335 
336  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
337  QgsCoordinateReferenceSystem sourceCrs() const override;
338  QgsFields fields() const override;
339  QgsWkbTypes::Type wkbType() const override;
340  long featureCount() const override;
341  QString sourceName() const override;
342  QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
343  QVariant minimumValue( int fieldIndex ) const override;
344  QVariant maximumValue( int fieldIndex ) const override;
345  QgsRectangle sourceExtent() const override;
346  QgsFeatureIds allFeatureIds() const override;
347 
351  QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
352 
353  private:
354 
355  QgsFeatureSource *mSource = nullptr;
356  bool mOwnsSource = false;
357  QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
358  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
359  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
360 
361 };
362 
363 #ifndef SIP_RUN
364 
373 {
374  public:
375 
376 
390  QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
391  ~QgsProcessingFeatureSink() override;
392  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
393  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
394  bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = nullptr ) override;
395 
396  private:
397 
398  QgsProcessingContext &mContext;
399  QString mSinkName;
400  bool mOwnsSink = false;
401 
402 };
403 #endif
404 
405 #endif // QGSPROCESSINGUTILS_H
406 
407 
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
Base class for all map layer types.
Definition: qgsmaplayer.h:63
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
Base class for providing feedback from a processing algorithm.
A simple feature sink which proxies feature addition on to another feature sink.
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
LayerHint
Layer type hints.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
Handles storage of information regarding WKB types and their properties.
Definition: qgswkbtypes.h:40
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:42
Abstract base class for processing providers.
#define SIP_INOUT
Definition: qgis_sip.h:64
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:41
Abstract base class for processing algorithms.
Utility functions for use with processing classes.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) override
Adds a list of features to the sink.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
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
Reads and writes project states.
Definition: qgsproject.h:89
#define SIP_FACTORY
Definition: qgis_sip.h:69
Single scope for storing variables and functions for use within a QgsExpressionContext.
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
QgsProxyFeatureSink subclass which reports feature addition errors to a QgsProcessingContext.
An interface for objects which provide features via a getFeatures method.
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_OUT
Definition: qgis_sip.h:51
This class represents a coordinate reference system (CRS).
#define SIP_THROW(name)
Definition: qgis_sip.h:177
A storage object for map layers, in which the layers are owned by the store and have their lifetime b...
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a single feature to the sink.
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:74