QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  QgsFeatureSink::SinkFlags sinkFlags = QgsFeatureSink::SinkFlags(),
227  QgsRemappingSinkDefinition *remappingDefinition = nullptr ) SIP_FACTORY;
228 #endif
229 
249  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 );
250 
251 
258  static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
259 
265  Q_DECL_DEPRECATED static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) SIP_DEPRECATED;
266 
273  static QVariant generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context );
274 
279  static QString tempFolder();
280 
287  static QString generateTempFilename( const QString &basename );
288 
293  static QString formatHelpMapAsHtml( const QVariantMap &map, const QgsProcessingAlgorithm *algorithm );
294 
314  static QString convertToCompatibleFormat( const QgsVectorLayer *layer,
315  bool selectedFeaturesOnly,
316  const QString &baseName,
317  const QStringList &compatibleFormats,
318  const QString &preferredFormat,
319  QgsProcessingContext &context,
320  QgsProcessingFeedback *feedback, long long featureLimit = -1 );
321 
355  static QString convertToCompatibleFormatAndLayerName( const QgsVectorLayer *layer,
356  bool selectedFeaturesOnly,
357  const QString &baseName,
358  const QStringList &compatibleFormats,
359  const QString &preferredFormat,
360  QgsProcessingContext &context,
361  QgsProcessingFeedback *feedback,
362  QString &layerName SIP_OUT, long long featureLimit = -1 );
363 
374  static QgsFields combineFields( const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix = QString() );
375 
381  static QList<int> fieldNamesToIndices( const QStringList &fieldNames, const QgsFields &fields );
382 
387  static QgsFields indicesToFields( const QList<int> &indices, const QgsFields &fields );
388 
399  static QString defaultVectorExtension();
400 
411  static QString defaultRasterExtension();
412 
413  private:
414  static bool canUseLayer( const QgsRasterLayer *layer );
415  static bool canUseLayer( const QgsMeshLayer *layer );
416  static bool canUseLayer( const QgsVectorTileLayer *layer );
417  static bool canUseLayer( const QgsVectorLayer *layer,
418  const QList< int > &sourceTypes = QList< int >() );
419 
430  static QgsMapLayer *mapLayerFromStore( const QString &string, QgsMapLayerStore *store, QgsProcessingUtils::LayerHint typeHint = QgsProcessingUtils::LayerHint::UnknownType );
431 
442  static QgsMapLayer *loadMapLayerFromString( const QString &string, const QgsCoordinateTransformContext &transformContext, LayerHint typeHint = LayerHint::UnknownType );
443 
452  Q_DECL_DEPRECATED static QgsMapLayer *loadMapLayerFromString( const QString &string, LayerHint typeHint = LayerHint::UnknownType ) SIP_DEPRECATED ;
453 
454  static void parseDestinationString( QString &destination, QString &providerKey, QString &uri, QString &layerName, QString &format, QMap<QString, QVariant> &options, bool &useWriter, QString &extension );
455 
456  friend class TestQgsProcessing;
457  friend class QgsProcessingProvider;
458 
459 };
460 
469 {
470  public:
471 
473  enum Flag
474  {
475  FlagSkipGeometryValidityChecks = 1 << 1,
476  };
477  Q_DECLARE_FLAGS( Flags, Flag )
478 
479 
489  QgsProcessingFeatureSource( QgsFeatureSource *originalSource, const QgsProcessingContext &context, bool ownsOriginalSource = false,
490  long long featureLimit = -1 );
491 
492  ~QgsProcessingFeatureSource() override;
493 
499  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request, Flags flags ) const;
500 
501  QgsFeatureSource::FeatureAvailability hasFeatures() const override;
502 
503  QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) const override;
504  QgsCoordinateReferenceSystem sourceCrs() const override;
505  QgsFields fields() const override;
506  QgsWkbTypes::Type wkbType() const override;
507  long featureCount() const override;
508  QString sourceName() const override;
509  QSet<QVariant> uniqueValues( int fieldIndex, int limit = -1 ) const override;
510  QVariant minimumValue( int fieldIndex ) const override;
511  QVariant maximumValue( int fieldIndex ) const override;
512  QgsRectangle sourceExtent() const override;
513  QgsFeatureIds allFeatureIds() const override;
514  SpatialIndexPresence hasSpatialIndex() const override;
515 
519  QgsExpressionContextScope *createExpressionContextScope() const SIP_FACTORY;
520 
526  void setInvalidGeometryCheck( QgsFeatureRequest::InvalidGeometryCheck method );
527 
528  private:
529 
530  QgsFeatureSource *mSource = nullptr;
531  bool mOwnsSource = false;
532  QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
533  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
534  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
535 
536  std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackSkip;
537  std::function< void( const QgsFeature & ) > mInvalidGeometryCallbackAbort;
538 
539  long long mFeatureLimit = -1;
540 
541 };
542 
543 #ifndef SIP_RUN
544 
553 {
554  public:
555 
556 
570  QgsProcessingFeatureSink( QgsFeatureSink *originalSink, const QString &sinkName, QgsProcessingContext &context, bool ownsOriginalSink = false );
571  ~QgsProcessingFeatureSink() override;
572  bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
573  bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
574  bool addFeatures( QgsFeatureIterator &iterator, QgsFeatureSink::Flags flags = QgsFeatureSink::Flags() ) override;
575 
576  private:
577 
578  QgsProcessingContext &mContext;
579  QString mSinkName;
580  bool mOwnsSink = false;
581 
582 };
583 #endif
584 
585 #endif // QGSPROCESSINGUTILS_H
586 
587 
SIP_PYNAME
#define SIP_PYNAME(name)
Definition: qgis_sip.h:81
QgsVectorTileLayer
Definition: qgsvectortilelayer.h:83
QgsCoordinateTransformContext
Definition: qgscoordinatetransformcontext.h:57
qgsrasterlayer.h
QgsRemappingSinkDefinition
Definition: qgsremappingproxyfeaturesink.h:37
QgsProcessingFeedback
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
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsProxyFeatureSink
Definition: qgsproxyfeaturesink.h:39
QgsFields
Definition: qgsfields.h:44
QgsProcessingProvider
Definition: qgsprocessingprovider.h:34
qgsfeaturesink.h
QgsFeatureSource
Definition: qgsfeaturesource.h:37
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
QgsProcessingUtils::LayerHint
LayerHint
Layer type hints.
Definition: qgsprocessingutils.h:136
QgsRectangle
Definition: qgsrectangle.h:41
QgsProject
Definition: qgsproject.h:92
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
Definition: qgsmaplayerstore.h:35
QgsFeatureRequest
Definition: qgsfeaturerequest.h:75
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
QgsProcessingContext
Definition: qgsprocessingcontext.h:43
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
Definition: qgsmeshlayer.h:94
QgsProcessingUtils
Definition: qgsprocessingutils.h:50
qgsfeaturesource.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
qgsproxyfeaturesink.h
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:111
SIP_INOUT
#define SIP_INOUT
Definition: qgis_sip.h:71
QgsProcessingFeatureSink
Definition: qgsprocessingutils.h:552
QgsFeatureSource::FeatureAvailability
FeatureAvailability
Possible return value for hasFeatures() to determine if a source is empty.
Definition: qgsfeaturesource.h:50
QgsProcessingFeatureSource::Flag
Flag
Flags controlling how QgsProcessingFeatureSource fetches features.
Definition: qgsprocessingutils.h:473
QgsProcessingAlgorithm
Definition: qgsprocessingalgorithm.h:51
qgsprocessing.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsProcessingFeatureSource
Definition: qgsprocessingutils.h:468
qgsspatialindex.h
qgsremappingproxyfeaturesink.h
QgsFeature
Definition: qgsfeature.h:55
QgsFeatureIterator
Definition: qgsfeatureiterator.h:263
QgsProcessingException
Definition: qgsexception.h:82
QgsFeatureSink
Definition: qgsfeaturesink.h:33
QgsProcessingUtils::LayerHint::UnknownType
@ UnknownType
Unknown layer type.
qgsmessagelog.h
SIP_MONKEYPATCH_SCOPEENUM
#define SIP_MONKEYPATCH_SCOPEENUM
Definition: qgis_sip.h:251