From f210668cb2134144f50f06bc366a3ce620e6c6f2 Mon Sep 17 00:00:00 2001 From: Larry Shaffer Date: Tue, 13 Nov 2012 01:56:33 -0700 Subject: [PATCH] Add option to limit number of labels rendered per layer (OFF by default) - Initial limit set to 2000 - Results would look better if limited subset was a random sampling of all features --- python/core/qgspallabeling.sip | 4 + src/app/qgslabelinggui.cpp | 4 + src/core/qgspallabeling.cpp | 15 ++++ src/core/qgspallabeling.h | 4 + src/ui/qgslabelingguibase.ui | 142 +++++++++++++++++++++++++++------ 5 files changed, 146 insertions(+), 23 deletions(-) diff --git a/python/core/qgspallabeling.sip b/python/core/qgspallabeling.sip index 0d8bbeed5f4..bb785b729a1 100644 --- a/python/core/qgspallabeling.sip +++ b/python/core/qgspallabeling.sip @@ -124,6 +124,10 @@ class QgsPalLayerSettings bool displayAll; // if true, all features will be labelled even though overlaps occur bool mergeLines; double minFeatureSize; // minimum feature size to be labelled (in mm) + bool limitNumLabels; // whether to limit the number of labels to be drawn + int maxNumLabels; // maximum number of labels to be drawn + //bool rndMaxNumLabels; // whether to take a randomized maxNumLabels subset of features to be labeled + // Adds '<' or '>', or user-defined symbol to the label string pointing to the // direction of the line / polygon ring // Works only if Placement == Line diff --git a/src/app/qgslabelinggui.cpp b/src/app/qgslabelinggui.cpp index 6eb65e1609d..73e9e1f3050 100644 --- a/src/app/qgslabelinggui.cpp +++ b/src/app/qgslabelinggui.cpp @@ -174,6 +174,8 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM mPalShowAllLabelsForLayerChkBx->setChecked( lyr.displayAll ); chkMergeLines->setChecked( lyr.mergeLines ); mMinSizeSpinBox->setValue( lyr.minFeatureSize ); + mLimitLabelChkBox->setChecked( lyr.limitNumLabels ); + mLimitLabelSpinBox->setValue( lyr.maxNumLabels ); mDirectSymbGroupBox->setChecked( lyr.addDirectionSymbol ); mDirectSymbLeftLineEdit->setText( lyr.leftDirectionSymbol ); mDirectSymbRightLineEdit->setText( lyr.rightDirectionSymbol ); @@ -500,6 +502,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings() lyr.upsidedownLabels = QgsPalLayerSettings::ShowAll; } lyr.minFeatureSize = mMinSizeSpinBox->value(); + lyr.limitNumLabels = mLimitLabelChkBox->isChecked(); + lyr.maxNumLabels = mLimitLabelSpinBox->value(); lyr.fontSizeInMapUnits = ( mFontSizeUnitComboBox->currentIndex() == 1 ); lyr.fontLimitPixelSize = mFontLimitPixelGroupBox->isChecked(); lyr.fontMinPixelSize = mFontMinPixelSpinBox->value(); diff --git a/src/core/qgspallabeling.cpp b/src/core/qgspallabeling.cpp index e1f98b4568e..76194f19884 100644 --- a/src/core/qgspallabeling.cpp +++ b/src/core/qgspallabeling.cpp @@ -210,6 +210,8 @@ QgsPalLayerSettings::QgsPalLayerSettings() displayAll = false; mergeLines = false; minFeatureSize = 0.0; + limitNumLabels = false; + maxNumLabels = 2000; vectorScaleFactor = 1.0; rasterCompressFactor = 1.0; addDirectionSymbol = false; @@ -267,6 +269,8 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s ) displayAll = s.displayAll; mergeLines = s.mergeLines; minFeatureSize = s.minFeatureSize; + limitNumLabels = s.limitNumLabels; + maxNumLabels = s.maxNumLabels; vectorScaleFactor = s.vectorScaleFactor; rasterCompressFactor = s.rasterCompressFactor; addDirectionSymbol = s.addDirectionSymbol; @@ -466,6 +470,8 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer ) placeDirectionSymbol = ( DirectionSymbols ) layer->customProperty( "labeling/placeDirectionSymbol", QVariant( SymbolLeftRight ) ).toUInt(); upsidedownLabels = ( UpsideDownLabels ) layer->customProperty( "labeling/upsidedownLabels", QVariant( Upright ) ).toUInt(); minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble(); + limitNumLabels = layer->customProperty( "labeling/limitNumLabels", QVariant( false ) ).toBool(); + maxNumLabels = layer->customProperty( "labeling/maxNumLabels", QVariant( 2000 ) ).toInt(); fontSizeInMapUnits = layer->customProperty( "labeling/fontSizeInMapUnits" ).toBool(); fontLimitPixelSize = layer->customProperty( "labeling/fontLimitPixelSize", QVariant( false ) ).toBool(); fontMinPixelSize = layer->customProperty( "labeling/fontMinPixelSize", QVariant( 0 ) ).toInt(); @@ -534,6 +540,8 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer ) layer->setCustomProperty( "labeling/placeDirectionSymbol", ( unsigned int )placeDirectionSymbol ); layer->setCustomProperty( "labeling/upsidedownLabels", ( unsigned int )upsidedownLabels ); layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize ); + layer->setCustomProperty( "labeling/limitNumLabels", limitNumLabels ); + layer->setCustomProperty( "labeling/maxNumLabels", maxNumLabels ); layer->setCustomProperty( "labeling/fontSizeInMapUnits", fontSizeInMapUnits ); layer->setCustomProperty( "labeling/fontLimitPixelSize", fontLimitPixelSize ); layer->setCustomProperty( "labeling/fontMinPixelSize", fontMinPixelSize ); @@ -650,6 +658,13 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f, const QgsRenderContext& context ) { + + // check if max number of labels to draw (already registered features) has been reached + if ( limitNumLabels && ( maxNumLabels == 0 || palLayer->getNbFeatures() >= maxNumLabels ) ) + { + return; + } + // data defined show label? defaults to show label if not 0 QMap< DataDefinedProperties, int >::const_iterator showIt = dataDefinedProperties.find( QgsPalLayerSettings::Show ); if ( showIt != dataDefinedProperties.constEnd() ) diff --git a/src/core/qgspallabeling.h b/src/core/qgspallabeling.h index 3314a37307f..02e973f79fd 100644 --- a/src/core/qgspallabeling.h +++ b/src/core/qgspallabeling.h @@ -178,6 +178,10 @@ class CORE_EXPORT QgsPalLayerSettings bool displayAll; // if true, all features will be labelled even though overlaps occur bool mergeLines; double minFeatureSize; // minimum feature size to be labelled (in mm) + bool limitNumLabels; // whether to limit the number of labels to be drawn + int maxNumLabels; // maximum number of labels to be drawn + //bool rndMaxNumLabels; // whether to take a randomized maxNumLabels subset of features to be labeled + // Adds '<' or '>', or user-defined symbol to the label string pointing to the // direction of the line / polygon ring // Works only if Placement == Line diff --git a/src/ui/qgslabelingguibase.ui b/src/ui/qgslabelingguibase.ui index c570e457a6f..545936a61a5 100644 --- a/src/ui/qgslabelingguibase.ui +++ b/src/ui/qgslabelingguibase.ui @@ -1921,7 +1921,7 @@ 0 0 686 - 532 + 574 @@ -2620,10 +2620,16 @@ - + + + + 0 + 0 + + Suppress labeling of features smaller than @@ -2631,6 +2637,12 @@ + + + 200 + 0 + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter @@ -2639,9 +2651,22 @@ + + + + Qt::Horizontal + + + + 40 + 20 + + + + - + @@ -2744,6 +2769,61 @@ + + + + + + + 0 + 0 + + + + Limit number of labels drawn to + + + + + + + false + + + + 200 + 0 + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + 0 + + + 999999999 + + + 2000 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + @@ -3301,12 +3381,12 @@ setValue(int) - 336 - 120 + 319 + 408 - 410 - 122 + 393 + 410 @@ -3318,11 +3398,11 @@ 319 - 259 + 547 391 - 261 + 549 @@ -3334,11 +3414,11 @@ 391 - 261 + 549 319 - 259 + 547 @@ -3349,12 +3429,12 @@ setValue(int) - 410 - 122 + 393 + 410 - 336 - 120 + 319 + 408 @@ -3365,12 +3445,12 @@ setVisible(bool) - 117 - 127 + 128 + 103 - 362 - 131 + 601 + 106 @@ -3381,12 +3461,28 @@ setVisible(bool) - 117 - 130 + 128 + 103 - 404 - 128 + 643 + 106 + + + + + mLimitLabelChkBox + toggled(bool) + mLimitLabelSpinBox + setEnabled(bool) + + + 57 + 493 + + + 379 + 493