Skip to content
Permalink
Browse files

Make counting and testing filters in rule based renderers faster

Hand filters off to the provider where possible and avoid fetching
unnecessary attributes/geometry

(cherry-picked from 9900036)
  • Loading branch information
nyalldawson committed Sep 26, 2016
1 parent 0497e4a commit 09ffbe732f863bc1ce162768c2626e5c11e3d768
Showing with 12 additions and 9 deletions.
  1. +12 −9 src/gui/symbology-ng/qgsrulebasedrendererv2widget.cpp
@@ -512,7 +512,7 @@ void QgsRuleBasedRendererV2Widget::countFeatures()
countMap[rule].duplicateCount = 0;
}

QgsFeatureIterator fit = mLayer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ) );
QgsFeatureRequest req = QgsFeatureRequest().setFilterExpression( mRenderer->filter( mLayer->fields() ) );

QgsRenderContext renderContext;
renderContext.setRendererScale( 0 ); // ignore scale
@@ -533,9 +533,13 @@ void QgsRuleBasedRendererV2Widget::countFeatures()
context << QgsExpressionContextUtils::layerScope( mLayer );

renderContext.setExpressionContext( context );
req.setExpressionContext( context );

mRenderer->startRender( renderContext, mLayer->fields() );

req.setSubsetOfAttributes( mRenderer->usedAttributes(), mLayer->fields() );
QgsFeatureIterator fit = mLayer->getFeatures( req );

int nFeatures = mLayer->featureCount();
QProgressDialog p( tr( "Calculating feature count." ), tr( "Abort" ), 0, nFeatures );
p.setWindowModality( Qt::WindowModal );
@@ -760,19 +764,18 @@ void QgsRendererRulePropsWidget::testFilter()

QApplication::setOverrideCursor( Qt::WaitCursor );

QgsFeatureIterator fit = mLayer->getFeatures();
QgsFeatureRequest req = QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() )
.setFlags( QgsFeatureRequest::NoGeometry )
.setFilterExpression( editFilter->text() )
.setExpressionContext( context );

QgsFeatureIterator fit = mLayer->getFeatures( req );

int count = 0;
QgsFeature f;
while ( fit.nextFeature( f ) )
{
context.setFeature( f );

QVariant value = filter.evaluate( &context );
if ( value.toInt() != 0 )
count++;
if ( filter.hasEvalError() )
break;
count++;
}

QApplication::restoreOverrideCursor();

0 comments on commit 09ffbe7

Please sign in to comment.
You can’t perform that action at this time.