QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 QgsMeshLayer;
31 class QgsProject;
33 class QgsMapLayerStore;
37 
38 #include <QString>
39 #include <QVariant>
40 
47 class CORE_EXPORT QgsProcessingUtils
48 {
49  public:
50 
61  static QList< QgsRasterLayer * > compatibleRasterLayers( QgsProject *project, bool sort = true );
62 
78  static QList< QgsVectorLayer * > compatibleVectorLayers( QgsProject *project,
79  const QList< int > &sourceTypes = QList< int >(),
80  bool sort = true );
81 
95  static QList<QgsMeshLayer *> compatibleMeshLayers( QgsProject *project, bool sort = true );
96 
106  static QList< QgsMapLayer * > compatibleLayers( QgsProject *project, bool sort = true );
107 
113  {
114  UnknownType,
115  Vector,
116  Raster,
117  Mesh,
118  };
119 
132  static QgsMapLayer *mapLayerFromString( const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers = true, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
133 
145  static QgsProcessingFeatureSource *variantToSource( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() ) SIP_FACTORY;
146 
155  static QgsCoordinateReferenceSystem variantToCrs( const QVariant &value, QgsProcessingContext &context, const QVariant &fallbackValue = QVariant() );
156 
161  static QString normalizeLayerSource( const QString &source );
162 
169  static QString variantToPythonLiteral( const QVariant &value );
170 
176  static QString stringToPythonLiteral( const QString &string );
177 
195 #ifndef SIP_RUN
196  static QgsFeatureSink *createFeatureSink( QString &destination,
197  QgsProcessingContext &context,
198  const QgsFields &fields,
199  QgsWkbTypes::Type geometryType,
201  const QVariantMap &createOptions = QVariantMap(),
202  QgsFeatureSink::SinkFlags sinkFlags = nullptr ) SIP_FACTORY;
203 #endif
204 
224  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 );
225 
226 
233  static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
234 
240  Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
241 
248  static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
249 
254  static QString tempFolder();
255 
262  static QString generateTempFilename( const QString &basename );
263 
268  static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
269 
287  static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
288  bool selectedFeaturesOnly,
289  const QString &baseName,
290  const QStringList &compatibleFormats,
291  const QString &preferredFormat,
292  QgsProcessingContext &context,
293  QgsProcessingFeedback *feedback );
294 
325  static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
326  bool selectedFeaturesOnly,
327  const QString &baseName,
328  const QStringList &compatibleFormats,
329  const QString &preferredFormat,
330  QgsProcessingContext &context,
331  QgsProcessingFeedback *feedback,
332  QString &layerName SIP_OUT );
333 
344  static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() );
345 
351  static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
352 
357  static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
358 
369  static QString defaultVectorExtension();
370 
381  static QString defaultRasterExtension();
382 
383  private:
384  static bool canUseLayer( const QgsRasterLayer *layer );
385  static bool canUseLayer( const QgsMeshLayer *layer );
386  static bool canUseLayer( const QgsVectorLayer *layer,
387  const QList< int > &sourceTypes = QList< int >() );
388 
399  static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
400 
411  static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType );
412 
421  Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
422 
423  static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
424 
425  friend class TestQgsProcessing;
426  friend class QgsProcessingProvider;
427 
428 };
429 
438 {
439  public:
440 
442  enum Flag
443  {
444  FlagSkipGeometryValidityChecks = 1 << 1,
445  };
446  Q_DECLARE_FLAGS( Flags, Flag )
447 
448 
455  QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false );
456 
457  ~QgsProcessingFeatureSource() override;
458 
464  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
465 
466  QgsFeatureSource::FeatureAvailability hasFeatures() const override;
467 
468  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
469  QgsCoordinateReferenceSystem sourceCrs() const override;
470  QgsFields fields() const override;
471  QgsWkbTypes::Type wkbType() const override;
472  long featureCount() const override;
473  QString sourceName() const override;
474  QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
475  QVariant minimumValue( int fieldIndex ) const override;
476  QVariant maximumValue( int fieldIndex ) const override;
477  QgsRectangle sourceExtent() const override;
478  QgsFeatureIds allFeatureIds() const override;
479  SpatialIndexPresence hasSpatialIndex() const override;
480 
484  QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
485 
486  private:
487 
488  QgsFeatureSource *mSource = nullptr;
489  bool mOwnsSource = false;
490  QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
491  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
492  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
493 
494 };
495 
496 #ifndef SIP_RUN
497 
506 {
507  public:
508 
509 
523  QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
524  ~QgsProcessingFeatureSink() override;
525  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
526  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
527  bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = nullptr ) override;
528 
529  private:
530 
531  QgsProcessingContext &mContext;
532  QString mSinkName;
533  bool mOwnsSink = false;
534 
535 };
536 #endif
537 
538 #endif // QGSPROCESSINGUTILS_H
539 
540 
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:79
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.
Represents a raster layer.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
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_MONKEYPATCH_SCOPEENUM
Definition: qgis_sip.h:251
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: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
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:91
#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.
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:184
Represents a mesh layer supporting display of data on structured or unstructured meshes.
Definition: qgsmeshlayer.h:91
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