Skip to content

Commit 29f50b7

Browse files
committedJun 11, 2017
[processing] Catch transform errors in when iterating features
1 parent bc9b1b6 commit 29f50b7

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed
 

‎python/core/processing/qgsprocessingcontext.sip

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ Destination project
163163
%End
164164

165165

166+
void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
167+
%Docstring
168+
Sets a callback function to use when encountering a transform error when iterating
169+
features. This function will be
170+
called using the feature which encountered the transform error as a parameter.
171+
.. versionadded:: 3.0
172+
.. seealso:: transformErrorCallback()
173+
%End
174+
%MethodCode
175+
Py_BEGIN_ALLOW_THREADS
176+
177+
sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
178+
{
179+
SIP_BLOCK_THREADS
180+
Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
181+
SIP_UNBLOCK_THREADS
182+
} );
183+
184+
Py_END_ALLOW_THREADS
185+
%End
186+
187+
166188
QString defaultEncoding() const;
167189
%Docstring
168190
Returns the default encoding to use for newly created files.

‎python/plugins/processing/tools/dataobjects.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ def raise_error(f):
8585

8686
context.setInvalidGeometryCallback(raise_error)
8787

88+
def raise_transform_error(f):
89+
raise GeoAlgorithmExecutionException(QCoreApplication.translate("FeatureIterator",
90+
'Encountered a transform error when reprojecting feature with id {}.'.format(f.id())))
91+
context.setTransformErrorCallback(raise_transform_error)
92+
8893
settings = QgsSettings()
8994
context.setDefaultEncoding(settings.value("/Processing/encoding", "System"))
9095

‎src/core/processing/qgsprocessingcontext.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,41 @@ class CORE_EXPORT QgsProcessingContext
206206
*/
207207
SIP_SKIP std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; }
208208

209+
/**
210+
* Sets a callback function to use when encountering a transform error when iterating
211+
* features. This function will be
212+
* called using the feature which encountered the transform error as a parameter.
213+
* \since QGIS 3.0
214+
* \see transformErrorCallback()
215+
*/
216+
#ifndef SIP_RUN
217+
void setTransformErrorCallback( std::function< void( const QgsFeature & ) > callback ) { mTransformErrorCallback = callback; }
218+
#else
219+
void setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
220+
% MethodCode
221+
Py_BEGIN_ALLOW_THREADS
222+
223+
sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
224+
{
225+
SIP_BLOCK_THREADS
226+
Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
227+
SIP_UNBLOCK_THREADS
228+
} );
229+
230+
Py_END_ALLOW_THREADS
231+
% End
232+
#endif
233+
234+
/**
235+
* Returns the callback function to use when encountering a transform error when iterating
236+
* features.
237+
* \since QGIS 3.0
238+
* \note not available in Python bindings
239+
* \see setTransformErrorCallback()
240+
* \see destinationCrs()
241+
*/
242+
std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
243+
209244
/**
210245
* Returns the default encoding to use for newly created files.
211246
* \see setDefaultEncoding()
@@ -228,6 +263,7 @@ class CORE_EXPORT QgsProcessingContext
228263
QgsExpressionContext mExpressionContext;
229264
QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
230265
std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
266+
std::function< void( const QgsFeature & ) > mTransformErrorCallback;
231267
QString mDefaultEncoding;
232268
QMap< QString, LayerDetails > mLayersToLoadOnCompletion;
233269

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ QgsProcessingFeatureSource::QgsProcessingFeatureSource( QgsFeatureSource *origin
375375
, mOwnsSource( ownsOriginalSource )
376376
, mInvalidGeometryCheck( context.invalidGeometryCheck() )
377377
, mInvalidGeometryCallback( context.invalidGeometryCallback() )
378+
, mTransformErrorCallback( context.transformErrorCallback() )
378379
{}
379380

380381
QgsProcessingFeatureSource::~QgsProcessingFeatureSource()
@@ -388,6 +389,7 @@ QgsFeatureIterator QgsProcessingFeatureSource::getFeatures( const QgsFeatureRequ
388389
QgsFeatureRequest req( request );
389390
req.setInvalidGeometryCheck( mInvalidGeometryCheck );
390391
req.setInvalidGeometryCallback( mInvalidGeometryCallback );
392+
req.setTransformErrorCallback( mTransformErrorCallback );
391393
return mSource->getFeatures( req );
392394
}
393395

‎src/core/processing/qgsprocessingutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ class QgsProcessingFeatureSource : public QgsFeatureSource
228228
bool mOwnsSource = false;
229229
QgsFeatureRequest::InvalidGeometryCheck mInvalidGeometryCheck = QgsFeatureRequest::GeometryNoCheck;
230230
std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
231+
std::function< void( const QgsFeature & ) > mTransformErrorCallback;
231232

232233
};
233234

0 commit comments

Comments
 (0)
Please sign in to comment.