Skip to content

Commit f210668

Browse files
committed
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
1 parent a6033f0 commit f210668

File tree

5 files changed

+146
-23
lines changed

5 files changed

+146
-23
lines changed

python/core/qgspallabeling.sip

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class QgsPalLayerSettings
124124
bool displayAll; // if true, all features will be labelled even though overlaps occur
125125
bool mergeLines;
126126
double minFeatureSize; // minimum feature size to be labelled (in mm)
127+
bool limitNumLabels; // whether to limit the number of labels to be drawn
128+
int maxNumLabels; // maximum number of labels to be drawn
129+
//bool rndMaxNumLabels; // whether to take a randomized maxNumLabels subset of features to be labeled
130+
127131
// Adds '<' or '>', or user-defined symbol to the label string pointing to the
128132
// direction of the line / polygon ring
129133
// Works only if Placement == Line

src/app/qgslabelinggui.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
174174
mPalShowAllLabelsForLayerChkBx->setChecked( lyr.displayAll );
175175
chkMergeLines->setChecked( lyr.mergeLines );
176176
mMinSizeSpinBox->setValue( lyr.minFeatureSize );
177+
mLimitLabelChkBox->setChecked( lyr.limitNumLabels );
178+
mLimitLabelSpinBox->setValue( lyr.maxNumLabels );
177179
mDirectSymbGroupBox->setChecked( lyr.addDirectionSymbol );
178180
mDirectSymbLeftLineEdit->setText( lyr.leftDirectionSymbol );
179181
mDirectSymbRightLineEdit->setText( lyr.rightDirectionSymbol );
@@ -500,6 +502,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
500502
lyr.upsidedownLabels = QgsPalLayerSettings::ShowAll;
501503
}
502504
lyr.minFeatureSize = mMinSizeSpinBox->value();
505+
lyr.limitNumLabels = mLimitLabelChkBox->isChecked();
506+
lyr.maxNumLabels = mLimitLabelSpinBox->value();
503507
lyr.fontSizeInMapUnits = ( mFontSizeUnitComboBox->currentIndex() == 1 );
504508
lyr.fontLimitPixelSize = mFontLimitPixelGroupBox->isChecked();
505509
lyr.fontMinPixelSize = mFontMinPixelSpinBox->value();

src/core/qgspallabeling.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ QgsPalLayerSettings::QgsPalLayerSettings()
210210
displayAll = false;
211211
mergeLines = false;
212212
minFeatureSize = 0.0;
213+
limitNumLabels = false;
214+
maxNumLabels = 2000;
213215
vectorScaleFactor = 1.0;
214216
rasterCompressFactor = 1.0;
215217
addDirectionSymbol = false;
@@ -267,6 +269,8 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
267269
displayAll = s.displayAll;
268270
mergeLines = s.mergeLines;
269271
minFeatureSize = s.minFeatureSize;
272+
limitNumLabels = s.limitNumLabels;
273+
maxNumLabels = s.maxNumLabels;
270274
vectorScaleFactor = s.vectorScaleFactor;
271275
rasterCompressFactor = s.rasterCompressFactor;
272276
addDirectionSymbol = s.addDirectionSymbol;
@@ -466,6 +470,8 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
466470
placeDirectionSymbol = ( DirectionSymbols ) layer->customProperty( "labeling/placeDirectionSymbol", QVariant( SymbolLeftRight ) ).toUInt();
467471
upsidedownLabels = ( UpsideDownLabels ) layer->customProperty( "labeling/upsidedownLabels", QVariant( Upright ) ).toUInt();
468472
minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble();
473+
limitNumLabels = layer->customProperty( "labeling/limitNumLabels", QVariant( false ) ).toBool();
474+
maxNumLabels = layer->customProperty( "labeling/maxNumLabels", QVariant( 2000 ) ).toInt();
469475
fontSizeInMapUnits = layer->customProperty( "labeling/fontSizeInMapUnits" ).toBool();
470476
fontLimitPixelSize = layer->customProperty( "labeling/fontLimitPixelSize", QVariant( false ) ).toBool();
471477
fontMinPixelSize = layer->customProperty( "labeling/fontMinPixelSize", QVariant( 0 ) ).toInt();
@@ -534,6 +540,8 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
534540
layer->setCustomProperty( "labeling/placeDirectionSymbol", ( unsigned int )placeDirectionSymbol );
535541
layer->setCustomProperty( "labeling/upsidedownLabels", ( unsigned int )upsidedownLabels );
536542
layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize );
543+
layer->setCustomProperty( "labeling/limitNumLabels", limitNumLabels );
544+
layer->setCustomProperty( "labeling/maxNumLabels", maxNumLabels );
537545
layer->setCustomProperty( "labeling/fontSizeInMapUnits", fontSizeInMapUnits );
538546
layer->setCustomProperty( "labeling/fontLimitPixelSize", fontLimitPixelSize );
539547
layer->setCustomProperty( "labeling/fontMinPixelSize", fontMinPixelSize );
@@ -650,6 +658,13 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
650658

651659
void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f, const QgsRenderContext& context )
652660
{
661+
662+
// check if max number of labels to draw (already registered features) has been reached
663+
if ( limitNumLabels && ( maxNumLabels == 0 || palLayer->getNbFeatures() >= maxNumLabels ) )
664+
{
665+
return;
666+
}
667+
653668
// data defined show label? defaults to show label if not 0
654669
QMap< DataDefinedProperties, int >::const_iterator showIt = dataDefinedProperties.find( QgsPalLayerSettings::Show );
655670
if ( showIt != dataDefinedProperties.constEnd() )

src/core/qgspallabeling.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ class CORE_EXPORT QgsPalLayerSettings
178178
bool displayAll; // if true, all features will be labelled even though overlaps occur
179179
bool mergeLines;
180180
double minFeatureSize; // minimum feature size to be labelled (in mm)
181+
bool limitNumLabels; // whether to limit the number of labels to be drawn
182+
int maxNumLabels; // maximum number of labels to be drawn
183+
//bool rndMaxNumLabels; // whether to take a randomized maxNumLabels subset of features to be labeled
184+
181185
// Adds '<' or '>', or user-defined symbol to the label string pointing to the
182186
// direction of the line / polygon ring
183187
// Works only if Placement == Line

src/ui/qgslabelingguibase.ui

Lines changed: 119 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,7 +1921,7 @@
19211921
<x>0</x>
19221922
<y>0</y>
19231923
<width>686</width>
1924-
<height>532</height>
1924+
<height>574</height>
19251925
</rect>
19261926
</property>
19271927
<layout class="QGridLayout" name="gridLayout_13">
@@ -2620,17 +2620,29 @@
26202620
</property>
26212621
</widget>
26222622
</item>
2623-
<item row="4" column="0">
2623+
<item row="5" column="0">
26242624
<layout class="QHBoxLayout" name="horizontalLayout_2">
26252625
<item>
26262626
<widget class="QLabel" name="label_19">
2627+
<property name="sizePolicy">
2628+
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
2629+
<horstretch>0</horstretch>
2630+
<verstretch>0</verstretch>
2631+
</sizepolicy>
2632+
</property>
26272633
<property name="text">
26282634
<string>Suppress labeling of features smaller than</string>
26292635
</property>
26302636
</widget>
26312637
</item>
26322638
<item>
26332639
<widget class="QDoubleSpinBox" name="mMinSizeSpinBox">
2640+
<property name="minimumSize">
2641+
<size>
2642+
<width>200</width>
2643+
<height>0</height>
2644+
</size>
2645+
</property>
26342646
<property name="alignment">
26352647
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
26362648
</property>
@@ -2639,9 +2651,22 @@
26392651
</property>
26402652
</widget>
26412653
</item>
2654+
<item>
2655+
<spacer name="horizontalSpacer_11">
2656+
<property name="orientation">
2657+
<enum>Qt::Horizontal</enum>
2658+
</property>
2659+
<property name="sizeHint" stdset="0">
2660+
<size>
2661+
<width>40</width>
2662+
<height>20</height>
2663+
</size>
2664+
</property>
2665+
</spacer>
2666+
</item>
26422667
</layout>
26432668
</item>
2644-
<item row="5" column="0">
2669+
<item row="6" column="0">
26452670
<layout class="QHBoxLayout" name="horizontalLayout_20">
26462671
<item>
26472672
<widget class="QCheckBox" name="chkNoObstacle">
@@ -2744,6 +2769,61 @@
27442769
</item>
27452770
</layout>
27462771
</item>
2772+
<item row="4" column="0">
2773+
<layout class="QHBoxLayout" name="horizontalLayout_22">
2774+
<item>
2775+
<widget class="QCheckBox" name="mLimitLabelChkBox">
2776+
<property name="sizePolicy">
2777+
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
2778+
<horstretch>0</horstretch>
2779+
<verstretch>0</verstretch>
2780+
</sizepolicy>
2781+
</property>
2782+
<property name="text">
2783+
<string>Limit number of labels drawn to</string>
2784+
</property>
2785+
</widget>
2786+
</item>
2787+
<item>
2788+
<widget class="QSpinBox" name="mLimitLabelSpinBox">
2789+
<property name="enabled">
2790+
<bool>false</bool>
2791+
</property>
2792+
<property name="minimumSize">
2793+
<size>
2794+
<width>200</width>
2795+
<height>0</height>
2796+
</size>
2797+
</property>
2798+
<property name="alignment">
2799+
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
2800+
</property>
2801+
<property name="minimum">
2802+
<number>0</number>
2803+
</property>
2804+
<property name="maximum">
2805+
<number>999999999</number>
2806+
</property>
2807+
<property name="value">
2808+
<number>2000</number>
2809+
</property>
2810+
</widget>
2811+
</item>
2812+
<item>
2813+
<spacer name="horizontalSpacer_10">
2814+
<property name="orientation">
2815+
<enum>Qt::Horizontal</enum>
2816+
</property>
2817+
<property name="sizeHint" stdset="0">
2818+
<size>
2819+
<width>40</width>
2820+
<height>20</height>
2821+
</size>
2822+
</property>
2823+
</spacer>
2824+
</item>
2825+
</layout>
2826+
</item>
27472827
</layout>
27482828
</widget>
27492829
</item>
@@ -3301,12 +3381,12 @@
33013381
<slot>setValue(int)</slot>
33023382
<hints>
33033383
<hint type="sourcelabel">
3304-
<x>336</x>
3305-
<y>120</y>
3384+
<x>319</x>
3385+
<y>408</y>
33063386
</hint>
33073387
<hint type="destinationlabel">
3308-
<x>410</x>
3309-
<y>122</y>
3388+
<x>393</x>
3389+
<y>410</y>
33103390
</hint>
33113391
</hints>
33123392
</connection>
@@ -3318,11 +3398,11 @@
33183398
<hints>
33193399
<hint type="sourcelabel">
33203400
<x>319</x>
3321-
<y>259</y>
3401+
<y>547</y>
33223402
</hint>
33233403
<hint type="destinationlabel">
33243404
<x>391</x>
3325-
<y>261</y>
3405+
<y>549</y>
33263406
</hint>
33273407
</hints>
33283408
</connection>
@@ -3334,11 +3414,11 @@
33343414
<hints>
33353415
<hint type="sourcelabel">
33363416
<x>391</x>
3337-
<y>261</y>
3417+
<y>549</y>
33383418
</hint>
33393419
<hint type="destinationlabel">
33403420
<x>319</x>
3341-
<y>259</y>
3421+
<y>547</y>
33423422
</hint>
33433423
</hints>
33443424
</connection>
@@ -3349,12 +3429,12 @@
33493429
<slot>setValue(int)</slot>
33503430
<hints>
33513431
<hint type="sourcelabel">
3352-
<x>410</x>
3353-
<y>122</y>
3432+
<x>393</x>
3433+
<y>410</y>
33543434
</hint>
33553435
<hint type="destinationlabel">
3356-
<x>336</x>
3357-
<y>120</y>
3436+
<x>319</x>
3437+
<y>408</y>
33583438
</hint>
33593439
</hints>
33603440
</connection>
@@ -3365,12 +3445,12 @@
33653445
<slot>setVisible(bool)</slot>
33663446
<hints>
33673447
<hint type="sourcelabel">
3368-
<x>117</x>
3369-
<y>127</y>
3448+
<x>128</x>
3449+
<y>103</y>
33703450
</hint>
33713451
<hint type="destinationlabel">
3372-
<x>362</x>
3373-
<y>131</y>
3452+
<x>601</x>
3453+
<y>106</y>
33743454
</hint>
33753455
</hints>
33763456
</connection>
@@ -3381,12 +3461,28 @@
33813461
<slot>setVisible(bool)</slot>
33823462
<hints>
33833463
<hint type="sourcelabel">
3384-
<x>117</x>
3385-
<y>130</y>
3464+
<x>128</x>
3465+
<y>103</y>
3466+
</hint>
3467+
<hint type="destinationlabel">
3468+
<x>643</x>
3469+
<y>106</y>
3470+
</hint>
3471+
</hints>
3472+
</connection>
3473+
<connection>
3474+
<sender>mLimitLabelChkBox</sender>
3475+
<signal>toggled(bool)</signal>
3476+
<receiver>mLimitLabelSpinBox</receiver>
3477+
<slot>setEnabled(bool)</slot>
3478+
<hints>
3479+
<hint type="sourcelabel">
3480+
<x>57</x>
3481+
<y>493</y>
33863482
</hint>
33873483
<hint type="destinationlabel">
3388-
<x>404</x>
3389-
<y>128</y>
3484+
<x>379</x>
3485+
<y>493</y>
33903486
</hint>
33913487
</hints>
33923488
</connection>

0 commit comments

Comments
 (0)