Skip to content

Commit

Permalink
Make counting and testing filters in rule based renderers faster
Browse files Browse the repository at this point in the history
Hand filters off to the provider where possible and avoid fetching
unnecessary attributes/geometry
  • Loading branch information
nyalldawson committed Sep 23, 2016
1 parent e1386d4 commit 9900036
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/gui/symbology-ng/qgsrulebasedrendererwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ void QgsRuleBasedRendererWidget::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
Expand All @@ -535,9 +535,13 @@ void QgsRuleBasedRendererWidget::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 );
Expand Down Expand Up @@ -762,19 +766,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();
Expand Down

0 comments on commit 9900036

Please sign in to comment.