17#include "moc_qgsvectorlayerfeaturecounter.cpp"
26 , mRenderer( layer->renderer()->clone() )
27 , mExpressionContext( context )
28 , mWithFids( storeSymbolFids )
29 , mFeatureCount( layer->featureCount() )
41 mSymbolFeatureCountMap.clear();
42 mSymbolFeatureIdMap.clear();
44 QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin();
46 for ( ; symbolIt != symbolList.constEnd(); ++symbolIt )
48 mSymbolFeatureCountMap.insert( symbolIt->ruleKey(), 0 );
50 mSymbolFeatureIdMap.insert( symbolIt->ruleKey(),
QgsFeatureIds() );
54 if ( mFeatureCount > 0 )
56 mFeedback = std::make_unique< QgsFeedback >();
58 int featuresCounted = 0;
66 if ( !mRenderer->filterNeedsGeometry() )
75 mRenderer->startRender( renderContext, mSource->fields() );
83 const QSet<QString> featureKeyList = mRenderer->legendKeysForFeature( f, renderContext );
84 for (
const QString &key : featureKeyList )
86 mSymbolFeatureCountMap[key] += 1;
88 mSymbolFeatureIdMap[key].insert( f.
id() );
92 const double p = (
static_cast< double >( featuresCounted ) / mFeatureCount ) * 100;
101 mRenderer->stopRender( renderContext );
107 mRenderer->stopRender( renderContext );
125 return mSymbolFeatureCountMap;
130 return mSymbolFeatureCountMap.value( legendKey, -1 );
135 return mSymbolFeatureIdMap;
140 return mSymbolFeatureIdMap.value( symbolkey,
QgsFeatureIds() );
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeedback(QgsFeedback *feedback)
Attach a feedback object that can be queried regularly by the expression engine to check if expressio...
int scopeCount() const
Returns the number of scopes contained in the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
void setFeedback(QgsFeedback *feedback)
Attach a feedback object that can be queried regularly by the iterator to check if it should be cance...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setRendererScale(double scale)
Sets the renderer map scale.
Abstract base class for long running background tasks.
double progress() const
Returns the task's progress (between 0.0 and 100.0)
virtual void cancel()
Notifies the task that it should terminate.
bool isCanceled() const
Will return true if task should terminate ASAP.
void setProgress(double progress)
Sets the task's current progress.
void symbolsCounted()
Emitted when the symbols have been counted.
~QgsVectorLayerFeatureCounter() override
QgsVectorLayerFeatureCounter(QgsVectorLayer *layer, const QgsExpressionContext &context=QgsExpressionContext(), bool storeSymbolFids=false)
Create a new feature counter for layer.
bool run() override
Calculates the feature count and Ids per symbol.
QHash< QString, long long > symbolFeatureCountMap() const
Returns the count for each symbol.
void cancel() override
Notifies the task that it should terminate.
long long featureCount(const QString &legendKey) const
Returns the feature count for a particular legendKey.
QgsFeatureIds featureIds(const QString &symbolkey) const
Returns the feature Ids for a particular legendKey.
QHash< QString, QgsFeatureIds > symbolFeatureIdMap() const
Returns the QgsFeatureIds for each symbol.
Partial snapshot of vector layer's state (only the members necessary for access to features)
Represents a vector layer which manages a vector based data sets.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QSet< QgsFeatureId > QgsFeatureIds
QList< QgsLegendSymbolItem > QgsLegendSymbolList