QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsprocessingcontext.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingcontext.cpp
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 #include "qgsprocessingcontext.h"
19 #include "qgsprocessingutils.h"
20 
22  : mPreferredVectorFormat( QgsProcessingUtils::defaultVectorExtension() )
23  , mPreferredRasterFormat( QgsProcessingUtils::defaultRasterExtension() )
24 {
25  auto callback = [ = ]( const QgsFeature & feature )
26  {
27  if ( mFeedback )
28  mFeedback->reportError( QObject::tr( "Encountered a transform error when reprojecting feature with id %1." ).arg( feature.id() ) );
29  };
30  mTransformErrorCallback = callback;
31 }
32 
34 {
35  for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
36  {
37  delete it.value().postProcessor();
38  }
39 }
40 
41 void QgsProcessingContext::setLayersToLoadOnCompletion( const QMap<QString, QgsProcessingContext::LayerDetails> &layers )
42 {
43  for ( auto it = mLayersToLoadOnCompletion.constBegin(); it != mLayersToLoadOnCompletion.constEnd(); ++it )
44  {
45  if ( !layers.contains( it.key() ) || layers.value( it.key() ).postProcessor() != it.value().postProcessor() )
46  delete it.value().postProcessor();
47  }
48  mLayersToLoadOnCompletion = layers;
49 }
50 
52 {
53  if ( mLayersToLoadOnCompletion.contains( layer ) && mLayersToLoadOnCompletion.value( layer ).postProcessor() != details.postProcessor() )
54  delete mLayersToLoadOnCompletion.value( layer ).postProcessor();
55 
56  mLayersToLoadOnCompletion.insert( layer, details );
57 }
58 
60 {
61  mInvalidGeometryCheck = check;
62 
63  switch ( mInvalidGeometryCheck )
64  {
66  {
67  auto callback = []( const QgsFeature & feature )
68  {
69  throw QgsProcessingException( QObject::tr( "Feature (%1) has invalid geometry. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
70  };
71  mInvalidGeometryCallback = callback;
72  break;
73  }
74 
76  {
77  auto callback = [ = ]( const QgsFeature & feature )
78  {
79  if ( mFeedback )
80  mFeedback->reportError( QObject::tr( "Feature (%1) has invalid geometry and has been skipped. Please fix the geometry or change the Processing setting to the \"Ignore invalid input features\" option." ).arg( feature.id() ) );
81  };
82  mInvalidGeometryCallback = callback;
83  break;
84  }
85 
86  default:
87  break;
88  }
89 }
90 
92 {
93  setLayersToLoadOnCompletion( context.mLayersToLoadOnCompletion );
94  context.mLayersToLoadOnCompletion.clear();
95  tempLayerStore.transferLayersFromStore( context.temporaryLayerStore() );
96 }
97 
98 QgsMapLayer *QgsProcessingContext::getMapLayer( const QString &identifier )
99 {
100  return QgsProcessingUtils::mapLayerFromString( identifier, *this, false );
101 }
102 
104 {
105  return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer( id ) );
106 }
107 
108 
109 
111 {
112  return mPostProcessor;
113 }
114 
116 {
117  if ( mPostProcessor && mPostProcessor != processor )
118  delete mPostProcessor;
119 
120  mPostProcessor = processor;
121 }
Base class for all map layer types.
Definition: qgsmaplayer.h:79
void setLayersToLoadOnCompletion(const QMap< QString, QgsProcessingContext::LayerDetails > &layers)
Sets the map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completion...
An interface for layer post-processing handlers for execution following a processing algorithm operat...
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
QgsMapLayer * takeResultLayer(const QString &id)
Takes the result map layer with matching id from the context and transfers ownership of it back to th...
Skip any features with invalid geometry. This requires a slow geometry validity check for every featu...
QgsMapLayer * takeMapLayer(QgsMapLayer *layer)
Takes a layer from the store.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
InvalidGeometryCheck
Handling of features with invalid geometries.
Utility functions for use with processing classes.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
QgsProcessingLayerPostProcessorInterface * postProcessor() const
Layer post-processor.
QgsMapLayerStore * temporaryLayerStore()
Returns a reference to the layer store used for storing temporary layers during algorithm execution...
void takeResultsFrom(QgsProcessingContext &context)
Takes the results from another context and merges them with the results currently stored in this cont...
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsMapLayer * getMapLayer(const QString &identifier)
Returns a map layer from the context with a matching identifier.
void setInvalidGeometryCheck(QgsFeatureRequest::InvalidGeometryCheck check)
Sets the behavior used for checking invalid geometries in input layers.
Details for layers to load into projects.
QgsProcessingContext()
Constructor for QgsProcessingContext.
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model...
void transferLayersFromStore(QgsMapLayerStore *other)
Transfers all the map layers contained within another map layer store and adds them to this store...
QgsMapLayer * mapLayer(const QString &id) const
Retrieve a pointer to a layer by layer id.
Contains information about the context in which a processing algorithm is executed.
Close iterator on encountering any features with invalid geometry. This requires a slow geometry vali...