Skip to content
Permalink
Browse files
Expose invalid geometry callback to python
  • Loading branch information
nyalldawson committed Apr 25, 2017
1 parent 92cc88d commit 974383112372f2b6a41aae1a38eb0b1a76dfde2d
Showing with 47 additions and 1 deletion.
  1. +29 −0 python/core/qgsfeaturerequest.sip
  2. +1 −0 src/core/qgsfeaturerequest.cpp
  3. +0 −1 src/core/qgsfeaturerequest.h
  4. +17 −0 tests/src/python/test_qgsfeatureiterator.py
@@ -220,6 +220,35 @@ class QgsFeatureRequest
*/
InvalidGeometryCheck invalidGeometryCheck() const;

/**
* Sets a callback function to use when encountering an invalid geometry and
* invalidGeometryCheck() is set to GeometryAbortOnInvalid. This function will be
* called using the feature with invalid geometry as a parameter.
* \since QGIS 3.0
* \see invalidGeometryCallback()
*/
QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE /AllowNone/ );
%MethodCode
Py_BEGIN_ALLOW_THREADS

sipRes = new QgsFeatureRequest( sipCpp->setInvalidGeometryCallback([a0](const QgsFeature &arg) {
SIP_BLOCK_THREADS
Py_XDECREF( sipCallMethod(NULL, a0, "N", new QgsFeature(arg), sipType_QgsFeature, NULL) );
SIP_UNBLOCK_THREADS
}) );

Py_END_ALLOW_THREADS
%End

/**
* Returns the callback function to use when encountering an invalid geometry and
* invalidGeometryCheck() is set to GeometryAbortOnInvalid.
* \since QGIS 3.0
* \note not available in Python bindings
* \see setInvalidGeometryCallback()
*/
// std::function< void( const QgsFeature & ) > invalidGeometryCallback() const;

/** Set the filter expression. {@see QgsExpression}
* @param expression expression string
* @see filterExpression
@@ -77,6 +77,7 @@ QgsFeatureRequest &QgsFeatureRequest::operator=( const QgsFeatureRequest &rh )
mFilterExpression.reset( nullptr );
}
mInvalidGeometryFilter = rh.mInvalidGeometryFilter;
mInvalidGeometryCallback = rh.mInvalidGeometryCallback;
mExpressionContext = rh.mExpressionContext;
mAttrs = rh.mAttrs;
mSimplifyMethod = rh.mSimplifyMethod;
@@ -299,7 +299,6 @@ class CORE_EXPORT QgsFeatureRequest
* invalidGeometryCheck() is set to GeometryAbortOnInvalid. This function will be
* called using the feature with invalid geometry as a parameter.
* \since QGIS 3.0
* \note not available in Python bindings
* \see invalidGeometryCallback()
*/
QgsFeatureRequest &setInvalidGeometryCallback( std::function< void( const QgsFeature & ) > callback );
@@ -308,6 +308,23 @@ def test_invalidGeometryFilter(self):
layer.getFeatures(QgsFeatureRequest().setInvalidGeometryCheck(QgsFeatureRequest.GeometryAbortOnInvalid))]
self.assertEqual(res, ['a'])

# with callback
self.callback_feature_val = None

def callback(feature):
self.callback_feature_val = feature['x']

res = [f['x'] for f in
layer.getFeatures(QgsFeatureRequest().setInvalidGeometryCheck(
QgsFeatureRequest.GeometryAbortOnInvalid).setInvalidGeometryCallback(callback))]
self.assertEqual(res, ['a'])
self.assertEqual(self.callback_feature_val, 'b')
# clear callback
res = [f['x'] for f in
layer.getFeatures(QgsFeatureRequest().setInvalidGeometryCheck(
QgsFeatureRequest.GeometryAbortOnInvalid).setInvalidGeometryCallback(None))]
self.assertEqual(res, ['a'])

# check with filter fids
res = [f['x'] for f in
layer.getFeatures(QgsFeatureRequest().setFilterFid(f2.id()).setInvalidGeometryCheck(QgsFeatureRequest.GeometryNoCheck))]

0 comments on commit 9743831

Please sign in to comment.