Skip to content
Permalink
Browse files

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
  • Loading branch information
dakcarto committed Nov 13, 2012
1 parent a6033f0 commit f210668cb2134144f50f06bc366a3ce620e6c6f2
@@ -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
@@ -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();
@@ -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() )
@@ -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
@@ -1921,7 +1921,7 @@
<x>0</x>
<y>0</y>
<width>686</width>
<height>532</height>
<height>574</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_13">
@@ -2620,17 +2620,29 @@
</property>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_19">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Suppress labeling of features smaller than</string>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="mMinSizeSpinBox">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
@@ -2639,9 +2651,22 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_11">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="5" column="0">
<item row="6" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_20">
<item>
<widget class="QCheckBox" name="chkNoObstacle">
@@ -2744,6 +2769,61 @@
</item>
</layout>
</item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_22">
<item>
<widget class="QCheckBox" name="mLimitLabelChkBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Limit number of labels drawn to</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="mLimitLabelSpinBox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>999999999</number>
</property>
<property name="value">
<number>2000</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_10">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
@@ -3301,12 +3381,12 @@
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>336</x>
<y>120</y>
<x>319</x>
<y>408</y>
</hint>
<hint type="destinationlabel">
<x>410</x>
<y>122</y>
<x>393</x>
<y>410</y>
</hint>
</hints>
</connection>
@@ -3318,11 +3398,11 @@
<hints>
<hint type="sourcelabel">
<x>319</x>
<y>259</y>
<y>547</y>
</hint>
<hint type="destinationlabel">
<x>391</x>
<y>261</y>
<y>549</y>
</hint>
</hints>
</connection>
@@ -3334,11 +3414,11 @@
<hints>
<hint type="sourcelabel">
<x>391</x>
<y>261</y>
<y>549</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>259</y>
<y>547</y>
</hint>
</hints>
</connection>
@@ -3349,12 +3429,12 @@
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
<x>410</x>
<y>122</y>
<x>393</x>
<y>410</y>
</hint>
<hint type="destinationlabel">
<x>336</x>
<y>120</y>
<x>319</x>
<y>408</y>
</hint>
</hints>
</connection>
@@ -3365,12 +3445,12 @@
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>117</x>
<y>127</y>
<x>128</x>
<y>103</y>
</hint>
<hint type="destinationlabel">
<x>362</x>
<y>131</y>
<x>601</x>
<y>106</y>
</hint>
</hints>
</connection>
@@ -3381,12 +3461,28 @@
<slot>setVisible(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>117</x>
<y>130</y>
<x>128</x>
<y>103</y>
</hint>
<hint type="destinationlabel">
<x>643</x>
<y>106</y>
</hint>
</hints>
</connection>
<connection>
<sender>mLimitLabelChkBox</sender>
<signal>toggled(bool)</signal>
<receiver>mLimitLabelSpinBox</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>57</x>
<y>493</y>
</hint>
<hint type="destinationlabel">
<x>404</x>
<y>128</y>
<x>379</x>
<y>493</y>
</hint>
</hints>
</connection>

0 comments on commit f210668

Please sign in to comment.
You can’t perform that action at this time.