QGIS API Documentation  3.0.2-Girona (307d082)
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 "qgsvectorlayer.h"
25 #include "qgsmessagelog.h"
26 #include "qgsspatialindex.h"
27 
28 class QgsProject;
30 class QgsMapLayerStore;
33 
34 #include <QString>
35 #include <QVariant>
36 
43 class CORE_EXPORT QgsProcessingUtils
44 {
45 
46  public:
47 
57  static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
58 
72  static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
73  const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >(),
74  bool sort = true );
75 
85  static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
86 
97  static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true );
98 
110  static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
111 
116  static QString normalizeLayerSource( const QString &source );
117 
121  static QString stringToPythonLiteral( const QString &string );
122 
140 #ifndef SIP_RUN
141  static QgsFeatureSink *createFeatureSink(
142  QString &destination,
143  QgsProcessingContext &context,
144  const QgsFields &fields,
145  QgsWkbTypes::Type geometryType,
146  const QgsCoordinateReferenceSystem &crs,
147  const QVariantMap &createOptions = QVariantMap() ) SIP_FACTORY;
148 #endif
149 
169  static void createFeatureSinkPython(
171  QString &destination SIP_INOUT,
172  QgsProcessingContext &context,
173  const QgsFields &fields,
174  QgsWkbTypes::Type geometryType,
175  const QgsCoordinateReferenceSystem &crs,
176  const QVariantMap &createOptions = QVariantMap() ) SIP_PYNAME( createFeatureSink );
177 
182  static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
183 
190  static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
191 
196  static QString tempFolder();
197 
204  static QString generateTempFilename( const QString &basename );
205 
210  static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
211 
224  static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
225  bool selectedFeaturesOnly,
226  const QString &baseName,
227  const QStringList &compatibleFormats,
228  const QString &preferredFormat,
229  QgsProcessingContext &context,
230  QgsProcessingFeedback *feedback );
231 
242  static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB );
243 
244  private:
245 
246  static bool canUseLayer( const QgsRasterLayer *layer );
247  static bool canUseLayer( const QgsVectorLayer *layer,
248  const QList< QgsWkbTypes::GeometryType > &geometryTypes = QList< QgsWkbTypes::GeometryType >() );
249 
260  static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store );
261 
268  static QgsMapLayer *loadMapLayerFromString( const QString &string );
269 
270  static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter );
271 
272  friend class TestQgsProcessing;
273 
274 };
275 
284 {
285  public:
286 
288  enum Flag
289  {
290  FlagSkipGeometryValidityChecks = 1 << 1,
291  };
292  Q_DECLARE_FLAGS( Flags, Flag )
293 
294 
301  QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false );
302 
303  ~QgsProcessingFeatureSource() override;
304 
310  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
311 
312  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
313  QgsCoordinateReferenceSystem sourceCrs() const override;
314  QgsFields fields() const override;
315  QgsWkbTypes::Type wkbType() const override;
316  long featureCount() const override;
317  QString sourceName() const override;
318  QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
319  QVariant minimumValue( int fieldIndex ) const override;
320  QVariant maximumValue( int fieldIndex ) const override;
321  QgsRectangle sourceExtent() const override;
322  QgsFeatureIds allFeatureIds() const override;
323 
327  QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
328 
329  private:
330 
331  QgsFeatureSource *mSource = nullptr;
332  bool mOwnsSource = false;
333  QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
334  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
335  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
336 
337 };
338 
339 #ifndef SIP_RUN
340 
349 {
350  public:
351 
352 
366  QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
368  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
369  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
370  bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = nullptr ) override;
371 
372  private:
373 
374  QgsProcessingContext &mContext;
375  QString mSinkName;
376  bool mOwnsSink = false;
377 
378 };
379 #endif
380 
381 #endif // QGSPROCESSINGUTILS_H
382 
383 
Wrapper for iterator of features from vector data provider or vector layer.
virtual QgsRectangle sourceExtent() const
Returns the extent of all geometries from the source.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
Base class for providing feedback from a processing algorithm.
virtual QgsFields fields() const =0
Returns the fields associated with features in the source.
A simple feature sink which proxies feature addition on to another feature sink.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
virtual QgsWkbTypes::Type wkbType() const =0
Returns the geometry type for features returned by this source.
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
#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:62
#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:67
virtual QVariant minimumValue(int fieldIndex) const
Returns the minimum value for an attribute column or an invalid variant in case of error...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
Reads and writes project states.
Definition: qgsproject.h:82
#define SIP_FACTORY
Definition: qgis_sip.h:69
Single scope for storing variables and functions for use within a QgsExpressionContext.
virtual QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const
Returns the set of unique values contained within the specified fieldIndex from this source...
virtual QgsCoordinateReferenceSystem sourceCrs() const =0
Returns the coordinate reference system for features in the source.
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.
#define SIP_OUT
Definition: qgis_sip.h:51
This class represents a coordinate reference system (CRS).
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.
virtual QVariant maximumValue(int fieldIndex) const
Returns the maximum value for an attribute column or an invalid variant in case of error...
virtual QString sourceName() const =0
Returns a friendly display name for the source.
Represents a vector layer which manages a vector based data sets.
virtual QgsFeatureIds allFeatureIds() const
Returns a list of all feature IDs for features present in the source.
Contains information about the context in which a processing algorithm is executed.
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const =0
Returns an iterator for the features in the source.
#define SIP_PYNAME(name)
Definition: qgis_sip.h:74
virtual long featureCount() const =0
Returns the number of features contained in the source, or -1 if the feature count is unknown...