Skip to content

Commit

Permalink
Allow specifying a callback for encountering invalid geometries while…
Browse files Browse the repository at this point in the history
… iterating
  • Loading branch information
nyalldawson committed Apr 25, 2017
1 parent 80d07cb commit 595f104
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/core/qgsfeaturerequest.cpp
Expand Up @@ -111,6 +111,12 @@ QgsFeatureRequest &QgsFeatureRequest::setInvalidGeometryCheck( QgsFeatureRequest
return *this;
}

QgsFeatureRequest &QgsFeatureRequest::setInvalidGeometryCallback( std::function<void ( const QgsFeature & )> callback )
{
mInvalidGeometryCallback = callback;
return *this;
}

QgsFeatureRequest &QgsFeatureRequest::setFilterExpression( const QString &expression )
{
mFilter = FilterExpression;
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgsfeaturerequest.h
Expand Up @@ -294,6 +294,25 @@ class CORE_EXPORT QgsFeatureRequest
*/
InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }

/**
* 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
* \note not available in Python bindings
* \see invalidGeometryCallback()
*/
QgsFeatureRequest &setInvalidGeometryCallback( std::function< void( const QgsFeature & ) > callback );

/**
* 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 { return mInvalidGeometryCallback; }

/** Set the filter expression. {\see QgsExpression}
* \param expression expression string
* \see filterExpression
Expand Down Expand Up @@ -440,6 +459,7 @@ class CORE_EXPORT QgsFeatureRequest
long mLimit = -1;
OrderBy mOrderBy;
InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
};

Q_DECLARE_OPERATORS_FOR_FLAGS( QgsFeatureRequest::Flags )
Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -698,8 +698,12 @@ bool QgsVectorLayerFeatureIterator::checkGeometry( const QgsFeature &feature )
case QgsFeatureRequest::GeometryAbortOnInvalid:
if ( !feature.geometry().isGeosValid() )
{
QgsMessageLog::logMessage( QObject::tr( "Geometry error: One or more input features have invalid geometry." ), QString(), QgsMessageLog::CRITICAL);
QgsMessageLog::logMessage( QObject::tr( "Geometry error: One or more input features have invalid geometry." ), QString(), QgsMessageLog::CRITICAL );
close();
if ( mRequest.invalidGeometryCallback() )
{
mRequest.invalidGeometryCallback()( feature );
}
return false;
}
break;
Expand Down

0 comments on commit 595f104

Please sign in to comment.