20 :
QgsTask( tr(
"Counting features in %1" ).arg( layer->name() ),
QgsTask::CanCancel )
22 , mRenderer( layer->renderer()->clone() )
23 , mExpressionContext( context )
24 , mFeatureCount( layer->featureCount() )
35 QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin();
37 for ( ; symbolIt != symbolList.constEnd(); ++symbolIt )
39 mSymbolFeatureCountMap.insert( symbolIt->label(), 0 );
43 if ( mFeatureCount > 0 )
45 int featuresCounted = 0;
53 if ( !mRenderer->filterNeedsGeometry() )
61 mRenderer->startRender( renderContext, mSource->fields() );
68 QSet<QString> featureKeyList = mRenderer->legendKeysForFeature( f, renderContext );
69 const auto constFeatureKeyList = featureKeyList;
70 for (
const QString &key : constFeatureKeyList )
72 mSymbolFeatureCountMap[key] += 1;
76 double p = (
static_cast< double >( featuresCounted ) / mFeatureCount ) * 100;
77 if ( p - progress > 1 )
85 mRenderer->stopRender( renderContext );
89 mRenderer->stopRender( renderContext );
100 return mSymbolFeatureCountMap;
105 return mSymbolFeatureCountMap.value( legendKey, -1 );
Wrapper for iterator of features from vector data provider or vector layer.
void setProgress(double progress)
Sets the task's current progress.
QList< QgsLegendSymbolItem > QgsLegendSymbolList
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
bool run() override
Performs the task's operation.
void setRendererScale(double scale)
Sets the renderer map scale.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
bool isCanceled() const
Will return true if task should terminate ASAP.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QgsVectorLayerFeatureCounter(QgsVectorLayer *layer, const QgsExpressionContext &context=QgsExpressionContext())
Create a new feature counter for layer.
void symbolsCounted()
Emitted when the symbols have been counted.
long featureCount(const QString &legendKey) const
Gets the feature count for a particular legendKey.
int scopeCount() const
Returns the number of scopes contained in the context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for long running background tasks.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Partial snapshot of vector layer's state (only the members necessary for access to features) ...
QgsExpressionContext & expressionContext()
Gets the expression context.
Contains information about the context of a rendering operation.
double progress() const
Returns the task's progress (between 0.0 and 100.0)
QHash< QString, long > symbolFeatureCountMap() const
Gets the count for each symbol.
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Represents a vector layer which manages a vector based data sets.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.