QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgsvectorlayerfeaturecounter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerfeaturecounter.cpp
3 ---------------------
4 begin : May 2017
5 copyright : (C) 2017 by Matthias Kuhn
6 email : matthias at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "moc_qgsvectorlayerfeaturecounter.cpp"
18#include "qgsvectorlayer.h"
19#include "qgsfeatureid.h"
20#include "qgsfeedback.h"
21#include "qgsrendercontext.h"
22
24 : QgsTask( tr( "Counting features in %1" ).arg( layer->name() ), QgsTask::CanCancel | QgsTask::CancelWithoutPrompt | QgsTask::Silent )
25 , mSource( new QgsVectorLayerFeatureSource( layer ) )
26 , mRenderer( layer->renderer()->clone() )
27 , mExpressionContext( context )
28 , mWithFids( storeSymbolFids )
29 , mFeatureCount( layer->featureCount() )
30{
31 if ( !mExpressionContext.scopeCount() )
32 {
33 mExpressionContext = layer->createExpressionContext();
34 }
35}
36
38
40{
41 mSymbolFeatureCountMap.clear();
42 mSymbolFeatureIdMap.clear();
43 const QgsLegendSymbolList symbolList = mRenderer->legendSymbolItems();
44 QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin();
45
46 for ( ; symbolIt != symbolList.constEnd(); ++symbolIt )
47 {
48 mSymbolFeatureCountMap.insert( symbolIt->ruleKey(), 0 );
49 if ( mWithFids )
50 mSymbolFeatureIdMap.insert( symbolIt->ruleKey(), QgsFeatureIds() );
51 }
52
53 // If there are no features to be counted, we can spare us the trouble
54 if ( mFeatureCount > 0 )
55 {
56 mFeedback = std::make_unique< QgsFeedback >();
57
58 int featuresCounted = 0;
59
60 // Renderer (rule based) may depend on context scale, with scale is ignored if 0
61 QgsRenderContext renderContext;
62 renderContext.setRendererScale( 0 );
63 renderContext.setExpressionContext( mExpressionContext );
64
65 QgsFeatureRequest request;
66 if ( !mRenderer->filterNeedsGeometry() )
68 request.setSubsetOfAttributes( mRenderer->usedAttributes( renderContext ), mSource->fields() );
69
70 request.setFeedback( mFeedback.get() );
71 mExpressionContext.setFeedback( mFeedback.get() );
72
73 QgsFeatureIterator fit = mSource->getFeatures( request );
74
75 mRenderer->startRender( renderContext, mSource->fields() );
76
77 double progress = 0;
78 QgsFeature f;
79 while ( fit.nextFeature( f ) )
80 {
81 renderContext.expressionContext().setFeature( f );
82
83 const QSet<QString> featureKeyList = mRenderer->legendKeysForFeature( f, renderContext );
84 for ( const QString &key : featureKeyList )
85 {
86 mSymbolFeatureCountMap[key] += 1;
87 if ( mWithFids )
88 mSymbolFeatureIdMap[key].insert( f.id() );
89 }
90 ++featuresCounted;
91
92 const double p = ( static_cast< double >( featuresCounted ) / mFeatureCount ) * 100;
93 if ( p - progress > 1 )
94 {
95 progress = p;
97 }
98
99 if ( isCanceled() )
100 {
101 mRenderer->stopRender( renderContext );
102 mExpressionContext.setFeedback( nullptr );
103 mFeedback.reset();
104 return false;
105 }
106 }
107 mRenderer->stopRender( renderContext );
108 mExpressionContext.setFeedback( nullptr );
109 mFeedback.reset();
110 }
111 setProgress( 100 );
112 emit symbolsCounted();
113 return true;
114}
115
117{
118 if ( mFeedback )
119 mFeedback->cancel();
121}
122
124{
125 return mSymbolFeatureCountMap;
126}
127
128long long QgsVectorLayerFeatureCounter::featureCount( const QString &legendKey ) const
129{
130 return mSymbolFeatureCountMap.value( legendKey, -1 );
131}
132
133QHash<QString, QgsFeatureIds> QgsVectorLayerFeatureCounter::symbolFeatureIdMap() const
134{
135 return mSymbolFeatureIdMap;
136}
137
139{
140 return mSymbolFeatureIdMap.value( symbolkey, QgsFeatureIds() );
141}
@ 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...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
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