@@ -147,6 +147,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
147147 xOffset = 0 ;
148148 yOffset = 0 ;
149149 angleOffset = 0 ;
150+ centroidWhole = false ;
150151 // textFont = QFont();
151152 textNamedStyle = QString ( " " );
152153 textColor = Qt::black;
@@ -193,6 +194,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
193194 xOffset = s.xOffset ;
194195 yOffset = s.yOffset ;
195196 angleOffset = s.angleOffset ;
197+ centroidWhole = s.centroidWhole ;
196198 textFont = s.textFont ;
197199 textNamedStyle = s.textNamedStyle ;
198200 textColor = s.textColor ;
@@ -363,6 +365,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
363365 xOffset = layer->customProperty ( " labeling/xOffset" , QVariant ( 0.0 ) ).toDouble ();
364366 yOffset = layer->customProperty ( " labeling/yOffset" , QVariant ( 0.0 ) ).toDouble ();
365367 angleOffset = layer->customProperty ( " labeling/angleOffset" , QVariant ( 0.0 ) ).toDouble ();
368+ centroidWhole = layer->customProperty ( " labeling/centroidWhole" , QVariant ( false ) ).toBool ();
366369 QString fontFamily = layer->customProperty ( " labeling/fontFamily" ).toString ();
367370 double fontSize = layer->customProperty ( " labeling/fontSize" ).toDouble ();
368371 int fontWeight = layer->customProperty ( " labeling/fontWeight" ).toInt ();
@@ -421,6 +424,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
421424 layer->setCustomProperty ( " labeling/xOffset" , xOffset );
422425 layer->setCustomProperty ( " labeling/yOffset" , yOffset );
423426 layer->setCustomProperty ( " labeling/angleOffset" , angleOffset );
427+ layer->setCustomProperty ( " labeling/centroidWhole" , centroidWhole );
424428
425429 layer->setCustomProperty ( " labeling/fontFamily" , textFont.family () );
426430 layer->setCustomProperty ( " labeling/namedStyle" , textNamedStyle );
@@ -676,10 +680,29 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
676680 return ;
677681 }
678682
683+ // whether we're going to create a centroid for polygon
684+ bool centroidPoly = (( placement == QgsPalLayerSettings::AroundPoint
685+ || placement == QgsPalLayerSettings::OverPoint )
686+ && geom->type () == QGis::Polygon );
687+
688+ // CLIP the geometry if it is bigger than the extent
689+ // don't clip if centroid is requested for whole feature
690+ bool do_clip = false ;
691+ if ( !centroidPoly || ( centroidPoly && !centroidWhole ) )
692+ {
693+ do_clip = !extentGeom->contains ( geom );
694+ if ( do_clip )
695+ {
696+ geom = geom->intersection ( extentGeom ); // creates new geometry
697+ if ( !geom )
698+ {
699+ return ;
700+ }
701+ }
702+ }
703+
679704 // convert centroids to points before processing to use GEOS instead of PAL calculation
680- if (( placement == QgsPalLayerSettings::AroundPoint
681- || placement == QgsPalLayerSettings::OverPoint )
682- && geom->type () == QGis::Polygon )
705+ if ( centroidPoly )
683706 {
684707 QgsGeometry* centroidpt = geom->centroid ();
685708 if ( centroidpt->isGeosValid () && extentGeom->contains ( centroidpt ) )
@@ -692,34 +715,17 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
692715 }
693716 else
694717 {
695- // invalid geom type, skip registering feature with PAL
718+ // invalid geom type or outside extents
696719 return ;
697720 }
698721 }
699722
700- // CLIP the geometry if it is bigger than the extent
701- QgsGeometry* geomClipped = NULL ;
702- GEOSGeometry* geos_geom;
703- bool do_clip = !extentGeom->contains ( geom );
704- if ( do_clip )
705- {
706- geomClipped = geom->intersection ( extentGeom ); // creates new geometry
707- if ( !geomClipped )
708- {
709- return ;
710- }
711- geos_geom = geomClipped->asGeos ();
712- }
713- else
714- {
715- geos_geom = geom->asGeos ();
716- }
723+ GEOSGeometry* geos_geom = geom->asGeos ();
717724
718725 if ( geos_geom == NULL )
719726 return ; // invalid geometry
720727 GEOSGeometry* geos_geom_clone = GEOSGeom_clone ( geos_geom );
721- if ( do_clip )
722- delete geomClipped;
728+
723729
724730 // data defined position / alignment / rotation?
725731 bool dataDefinedPosition = false ;
@@ -921,12 +927,11 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
921927 pal::LabelPosition* lp = new LabelPosition ( 1 , xPos, yPos, labelX, labelY,
922928 ( angleOffset * M_PI / 180 ), 0.0 , fpart );
923929
930+ // lp->getWidth or lp->getHeight doesn't account for rotation, get bbox instead
924931 double amin[2 ], amax[2 ];
925932 lp->getBoundingBox ( amin, amax );
926933 QgsRectangle lblrect = QgsRectangle ( amin[0 ], amin[1 ], amax[0 ], amax[1 ] );
927934
928- // labelW = lp->getWidth();
929- // labelH = lp->getHeight();
930935 labelW = lblrect.width ();
931936 labelH = lblrect.height ();
932937 delete fpart;
0 commit comments