-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send feature counting to background task
- Loading branch information
Showing
8 changed files
with
195 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsvectorlayerfeaturecounter.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
class QgsVectorLayerFeatureCounter : QgsTask | ||
{ | ||
%Docstring | ||
|
||
Counts the features in a QgsVectorLayer in task. | ||
You should most likely not use this directly and instead call | ||
QgsVectorLayer.countSymbolFeatures() and connect to the signal | ||
QgsVectorLayer.symbolFeatureCountMapChanged(). | ||
|
||
.. versionadded:: 3.0 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsvectorlayerfeaturecounter.h" | ||
%End | ||
public: | ||
|
||
QgsVectorLayerFeatureCounter( QgsVectorLayer *layer ); | ||
%Docstring | ||
Create a new feature counter for ``layer``. | ||
%End | ||
|
||
virtual bool run(); | ||
|
||
signals: | ||
void symbolsCounted( const QHash<QString, long> &symbolFeatureCountMap ); | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsvectorlayerfeaturecounter.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "qgsvectorlayerfeaturecounter.h" | ||
|
||
QgsVectorLayerFeatureCounter::QgsVectorLayerFeatureCounter( QgsVectorLayer *layer ) | ||
: mSource( new QgsVectorLayerFeatureSource( layer ) ) | ||
, mRenderer( layer->renderer()->clone() ) | ||
, mExpressionContextScopes( QgsExpressionContextUtils::globalProjectLayerScopes( layer ) ) | ||
, mFeatureCount( layer->featureCount() ) | ||
{ | ||
} | ||
|
||
bool QgsVectorLayerFeatureCounter::run() | ||
{ | ||
QgsLegendSymbolList symbolList = mRenderer->legendSymbolItems(); | ||
QgsLegendSymbolList::const_iterator symbolIt = symbolList.constBegin(); | ||
|
||
for ( ; symbolIt != symbolList.constEnd(); ++symbolIt ) | ||
{ | ||
mSymbolFeatureCountMap.insert( symbolIt->first, 0 ); | ||
} | ||
|
||
int featuresCounted = 0; | ||
|
||
// Renderer (rule based) may depend on context scale, with scale is ignored if 0 | ||
QgsRenderContext renderContext; | ||
renderContext.setRendererScale( 0 ); | ||
renderContext.expressionContext().appendScopes( mExpressionContextScopes ); | ||
|
||
QgsFeatureRequest request; | ||
if ( !mRenderer->filterNeedsGeometry() ) | ||
request.setFlags( QgsFeatureRequest::NoGeometry ); | ||
request.setSubsetOfAttributes( mRenderer->usedAttributes( renderContext ), mSource->fields() ); | ||
QgsFeatureIterator fit = mSource->getFeatures( request ); | ||
|
||
// TODO: replace QgsInterruptionChecker with QgsFeedback | ||
// fit.setInterruptionChecker( mFeedback ); | ||
|
||
mRenderer->startRender( renderContext, mSource->fields() ); | ||
|
||
double progress = 0; | ||
QgsFeature f; | ||
while ( fit.nextFeature( f ) ) | ||
{ | ||
renderContext.expressionContext().setFeature( f ); | ||
QSet<QString> featureKeyList = mRenderer->legendKeysForFeature( f, renderContext ); | ||
Q_FOREACH ( const QString &key, featureKeyList ) | ||
{ | ||
mSymbolFeatureCountMap[key] += 1; | ||
} | ||
++featuresCounted; | ||
|
||
double p = ( featuresCounted / mFeatureCount ) * 100; | ||
if ( p - progress > 1 ) | ||
{ | ||
progress = p; | ||
setProgress( progress ); | ||
} | ||
|
||
if ( isCanceled() ) | ||
{ | ||
mRenderer->stopRender( renderContext ); | ||
return false; | ||
} | ||
} | ||
mRenderer->stopRender( renderContext ); | ||
setProgress( 100 ); | ||
|
||
emit symbolsCounted( mSymbolFeatureCountMap ); | ||
return true; | ||
} |
Oops, something went wrong.