QGIS API Documentation  3.0.2-Girona (307d082)
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 {
23  mInvalidGeometryCheck = check;
24 
25  switch ( mInvalidGeometryCheck )
26  {
28  {
29  auto callback = []( const QgsFeature & feature )
30  {
31  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() ) );
32  };
33  mInvalidGeometryCallback = callback;
34  break;
35  }
36 
38  {
39  auto callback = [ = ]( const QgsFeature & feature )
40  {
41  if ( mFeedback )
42  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() ) );
43  };
44  mInvalidGeometryCallback = callback;
45  break;
46  }
47 
48  default:
49  break;
50  }
51 }
52 
54 {
55  QMap< QString, LayerDetails > loadOnCompletion = context.layersToLoadOnCompletion();
56  QMap< QString, LayerDetails >::const_iterator llIt = loadOnCompletion.constBegin();
57  for ( ; llIt != loadOnCompletion.constEnd(); ++llIt )
58  {
59  mLayersToLoadOnCompletion.insert( llIt.key(), llIt.value() );
60  }
61  context.setLayersToLoadOnCompletion( QMap< QString, LayerDetails >() );
62  tempLayerStore.transferLayersFromStore( context.temporaryLayerStore() );
63 }
64 
65 QgsMapLayer *QgsProcessingContext::getMapLayer( const QString &identifier )
66 {
67  return QgsProcessingUtils::mapLayerFromString( identifier, *this, false );
68 }
69 
71 {
72  return tempLayerStore.takeMapLayer( tempLayerStore.mapLayer( id ) );
73 }
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true)
Interprets a string as a map layer within the supplied context.
Base class for all map layer types.
Definition: qgsmaplayer.h:56
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...
QMap< QString, QgsProcessingContext::LayerDetails > layersToLoadOnCompletion() const
Returns a map of layers (by ID or datasource) to LayerDetails, to load into the canvas upon completio...
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:62
InvalidGeometryCheck
Handling of features with invalid geometries.
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.
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...