[QGIS Commit] r13587 - in trunk/qgis: python/core src/core src/plugins/labeling

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat May 29 06:32:58 EDT 2010


Author: wonder
Date: 2010-05-29 06:32:56 -0400 (Sat, 29 May 2010)
New Revision: 13587

Modified:
   trunk/qgis/python/core/qgsmaprenderer.sip
   trunk/qgis/src/core/qgsmaprenderer.h
   trunk/qgis/src/core/qgsvectorlayer.cpp
   trunk/qgis/src/plugins/labeling/labelinggui.cpp
   trunk/qgis/src/plugins/labeling/labelingguibase.ui
   trunk/qgis/src/plugins/labeling/pallabeling.cpp
   trunk/qgis/src/plugins/labeling/pallabeling.h
Log:
Applied patch #2747 from Marco.


Modified: trunk/qgis/python/core/qgsmaprenderer.sip
===================================================================
--- trunk/qgis/python/core/qgsmaprenderer.sip	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/python/core/qgsmaprenderer.sip	2010-05-29 10:32:56 UTC (rev 13587)
@@ -19,7 +19,7 @@
   //! called when starting rendering of a layer
   virtual int prepareLayer(QgsVectorLayer* layer, int& attrIndex, QgsRenderContext& ctx ) = 0;
   //! called for every feature
-  virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
+  virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() ) = 0;
   //! called when the map is drawn and labels should be placed
   virtual void drawLabeling( QgsRenderContext& context ) = 0;
   //! called when we're done with rendering

Modified: trunk/qgis/src/core/qgsmaprenderer.h
===================================================================
--- trunk/qgis/src/core/qgsmaprenderer.h	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/src/core/qgsmaprenderer.h	2010-05-29 10:32:56 UTC (rev 13587)
@@ -53,7 +53,7 @@
     //! called when starting rendering of a layer
     virtual int prepareLayer( QgsVectorLayer* layer, int& attrIndex, QgsRenderContext& ctx ) = 0;
     //! called for every feature
-    virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat ) = 0;
+    virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() ) = 0;
     //! called when the map is drawn and labels should be placed
     virtual void drawLabeling( QgsRenderContext& context ) = 0;
     //! called when we're done with rendering

Modified: trunk/qgis/src/core/qgsvectorlayer.cpp
===================================================================
--- trunk/qgis/src/core/qgsvectorlayer.cpp	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/src/core/qgsvectorlayer.cpp	2010-05-29 10:32:56 UTC (rev 13587)
@@ -741,7 +741,7 @@
 
       // labeling - register feature
       if ( labeling && mRendererV2->symbolForFeature( fet ) != NULL )
-        rendererContext.labelingEngine()->registerFeature( this, fet );
+        rendererContext.labelingEngine()->registerFeature( this, fet, rendererContext );
 
       if ( mEditable )
       {
@@ -805,7 +805,7 @@
     features[sym].append( fet );
 
     if ( labeling && mRendererV2->symbolForFeature( fet ) != NULL )
-      rendererContext.labelingEngine()->registerFeature( this, fet );
+      rendererContext.labelingEngine()->registerFeature( this, fet, rendererContext );
 
     if ( mEditable )
     {
@@ -1052,7 +1052,7 @@
 
         if ( labeling && mRenderer->willRenderFeature( &fet ) )
         {
-          rendererContext.labelingEngine()->registerFeature( this, fet );
+          rendererContext.labelingEngine()->registerFeature( this, fet, rendererContext );
         }
 
         ++featureCount;

Modified: trunk/qgis/src/plugins/labeling/labelinggui.cpp
===================================================================
--- trunk/qgis/src/plugins/labeling/labelinggui.cpp	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/src/plugins/labeling/labelinggui.cpp	2010-05-29 10:32:56 UTC (rev 13587)
@@ -117,6 +117,7 @@
   chkNoObstacle->setChecked( !lyr.obstacle );
   chkLabelPerFeaturePart->setChecked( lyr.labelPerPart );
   chkMergeLines->setChecked( lyr.mergeLines );
+  mMinSizeSpinBox->setValue( lyr.minFeatureSize );
 
   bool scaleBased = ( lyr.scaleMin != 0 && lyr.scaleMax != 0 );
   chkScaleBasedVisibility->setChecked( scaleBased );
@@ -231,7 +232,7 @@
   {
     lyr.bufferSize = 0;
   }
-
+  lyr.minFeatureSize = mMinSizeSpinBox->value();
   return lyr;
 }
 

Modified: trunk/qgis/src/plugins/labeling/labelingguibase.ui
===================================================================
--- trunk/qgis/src/plugins/labeling/labelingguibase.ui	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/src/plugins/labeling/labelingguibase.ui	2010-05-29 10:32:56 UTC (rev 13587)
@@ -6,7 +6,7 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>480</width>
+    <width>448</width>
     <height>610</height>
    </rect>
   </property>
@@ -18,15 +18,15 @@
     <normaloff/>
    </iconset>
   </property>
-  <layout class="QVBoxLayout" name="verticalLayout">
-   <item>
+  <layout class="QGridLayout" name="gridLayout_5">
+   <item row="0" column="0">
     <widget class="QCheckBox" name="chkEnableLabeling">
      <property name="text">
       <string>Label this layer</string>
      </property>
     </widget>
    </item>
-   <item>
+   <item row="1" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout_9">
      <item>
       <widget class="QLabel" name="label_6">
@@ -56,7 +56,7 @@
      </item>
     </layout>
    </item>
-   <item>
+   <item row="2" column="0">
     <widget class="QGroupBox" name="groupBox_4">
      <property name="title">
       <string>Placement</string>
@@ -326,7 +326,7 @@
      </layout>
     </widget>
    </item>
-   <item>
+   <item row="3" column="0">
     <layout class="QGridLayout" name="gridLayout_4">
      <item row="0" column="0" rowspan="2">
       <widget class="QGroupBox" name="groupBox">
@@ -648,21 +648,39 @@
      </item>
     </layout>
    </item>
-   <item>
+   <item row="4" column="0">
     <widget class="QCheckBox" name="chkLabelPerFeaturePart">
      <property name="text">
       <string>label every part of multi-part features</string>
      </property>
     </widget>
    </item>
-   <item>
+   <item row="5" column="0">
     <widget class="QCheckBox" name="chkMergeLines">
      <property name="text">
       <string>merge connected lines to avoid duplicate labels</string>
      </property>
     </widget>
    </item>
-   <item>
+   <item row="6" column="0">
+    <layout class="QHBoxLayout" name="horizontalLayout_10">
+     <item>
+      <widget class="QLabel" name="label_19">
+       <property name="text">
+        <string>Suppress labeling of features smaller than</string>
+       </property>
+      </widget>
+     </item>
+     <item>
+      <widget class="QDoubleSpinBox" name="mMinSizeSpinBox">
+       <property name="suffix">
+        <string> mm</string>
+       </property>
+      </widget>
+     </item>
+    </layout>
+   </item>
+   <item row="7" column="0">
     <layout class="QHBoxLayout" name="horizontalLayout_2">
      <item>
       <widget class="QCheckBox" name="chkNoObstacle">
@@ -699,7 +717,7 @@
      </item>
     </layout>
    </item>
-   <item>
+   <item row="8" column="0">
     <spacer name="verticalSpacer_2">
      <property name="orientation">
       <enum>Qt::Vertical</enum>
@@ -712,7 +730,7 @@
      </property>
     </spacer>
    </item>
-   <item>
+   <item row="9" column="0">
     <widget class="QDialogButtonBox" name="buttonBox">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>

Modified: trunk/qgis/src/plugins/labeling/pallabeling.cpp
===================================================================
--- trunk/qgis/src/plugins/labeling/pallabeling.cpp	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/src/plugins/labeling/pallabeling.cpp	2010-05-29 10:32:56 UTC (rev 13587)
@@ -105,6 +105,7 @@
   bufferColor = Qt::white;
   labelPerPart = false;
   mergeLines = false;
+  minFeatureSize = 0.0;
 }
 
 LayerSettings::LayerSettings( const LayerSettings& s )
@@ -125,6 +126,7 @@
   bufferColor = s.bufferColor;
   labelPerPart = s.labelPerPart;
   mergeLines = s.mergeLines;
+  minFeatureSize = s.minFeatureSize;
 
   fontMetrics = NULL;
   ct = NULL;
@@ -178,6 +180,7 @@
   bufferColor = _readColor( layer, "labeling/bufferColor" );
   labelPerPart = layer->customProperty( "labeling/labelPerPart" ).toBool();
   mergeLines = layer->customProperty( "labeling/mergeLines" ).toBool();
+  minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble();
 }
 
 void LayerSettings::writeToLayer( QgsVectorLayer* layer )
@@ -205,8 +208,53 @@
   _writeColor( layer, "labeling/bufferColor", bufferColor );
   layer->setCustomProperty( "labeling/labelPerPart", labelPerPart );
   layer->setCustomProperty( "labeling/mergeLines", mergeLines );
+  layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize );
 }
 
+bool LayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry* geom, double minSize ) const
+{
+  if ( minSize <= 0 )
+  {
+    return true;
+  }
+
+  if ( !geom )
+  {
+    return false;
+  }
+
+  QGis::GeometryType featureType = geom->type();
+  if ( featureType == QGis::Point ) //minimum size does not apply to point features
+  {
+    return true;
+  }
+
+  GEOSGeometry* geosGeom = geom->asGeos();
+  if ( !geosGeom )
+  {
+    return true;
+  }
+
+  double mapUnitsPerMM = ct.mapToPixel().mapUnitsPerPixel() * ct.scaleFactor();
+  if ( featureType == QGis::Line )
+  {
+    double length;
+    if ( GEOSLength( geosGeom, &length ) )
+    {
+      return ( length >= ( minSize * mapUnitsPerMM ) );
+    }
+  }
+  else if ( featureType == QGis::Polygon )
+  {
+    double area;
+    if ( GEOSArea( geosGeom, &area ) )
+    {
+      return ( sqrt( area ) >= ( minSize * mapUnitsPerMM ) );
+    }
+  }
+  return true; //should never be reached. Return true in this case to label such geometries anyway.
+}
+
 void LayerSettings::calculateLabelSize( QString text, double& labelX, double& labelY )
 {
   //QFontMetrics fontMetrics(textFont);
@@ -219,7 +267,7 @@
 }
 
 
-void LayerSettings::registerFeature( QgsFeature& f )
+void LayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& context )
 {
   QString labelText = f.attributeMap()[fieldIndex].toString();
   double labelX, labelY; // will receive label size
@@ -229,6 +277,11 @@
   if ( ct != NULL ) // reproject the geometry if necessary
     geom->transform( *ct );
 
+  if ( !checkMinimumSizeMM( context, geom, minFeatureSize ) )
+  {
+    return;
+  }
+
   MyLabel* lbl = new MyLabel( f.id(), labelText, GEOSGeom_clone( geom->asGeos() ) );
 
   // record the created geometry - it will be deleted at the end.
@@ -375,9 +428,10 @@
 }
 
 
-void PalLabeling::registerFeature( QgsVectorLayer* layer, QgsFeature& f )
+void PalLabeling::registerFeature( QgsVectorLayer* layer, QgsFeature& f, const QgsRenderContext& context )
 {
-  mActiveLayers[layer].registerFeature( f );
+  LayerSettings& lyr = mActiveLayers[layer];
+  lyr.registerFeature( f, context );
 }
 
 

Modified: trunk/qgis/src/plugins/labeling/pallabeling.h
===================================================================
--- trunk/qgis/src/plugins/labeling/pallabeling.h	2010-05-29 08:23:10 UTC (rev 13586)
+++ trunk/qgis/src/plugins/labeling/pallabeling.h	2010-05-29 10:32:56 UTC (rev 13587)
@@ -66,12 +66,13 @@
     QColor bufferColor;
     bool labelPerPart; // whether to label every feature's part or only the biggest one
     bool mergeLines;
+    double minFeatureSize; // minimum feature size to be labelled (in mm)
 
     // called from register feature hook
     void calculateLabelSize( QString text, double& labelX, double& labelY );
 
     // implementation of register feature hook
-    void registerFeature( QgsFeature& f );
+    void registerFeature( QgsFeature& f, const QgsRenderContext& context );
 
     void readFromLayer( QgsVectorLayer* layer );
     void writeToLayer( QgsVectorLayer* layer );
@@ -85,6 +86,11 @@
     const QgsCoordinateTransform* ct;
     QgsPoint ptZero, ptOne;
     QList<MyLabel*> geometries;
+
+  private:
+    /**Checks if a feature is larger than a minimum size (in mm)
+    @return true if above size, false if below*/
+    bool checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry* geom, double minSize ) const;
 };
 
 class LabelCandidate
@@ -128,7 +134,7 @@
     //! hook called when drawing layer before issuing select()
     virtual int prepareLayer( QgsVectorLayer* layer, int& attrIndex, QgsRenderContext& ctx );
     //! hook called when drawing for every feature in a layer
-    virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat );
+    virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() );
     //! called when the map is drawn and labels should be placed
     virtual void drawLabeling( QgsRenderContext& context );
     //! called when we're done with rendering
@@ -145,6 +151,7 @@
 
     void initPal();
 
+
   protected:
     // temporary hashtable of layer settings, being filled during labeling, cleared once labeling's done
     QHash<QgsVectorLayer*, LayerSettings> mActiveLayers;



More information about the QGIS-commit mailing list