@@ -1723,42 +1723,6 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
1723
1723
maxcharangleout = -( qAbs ( maxcharangleout ) );
1724
1724
}
1725
1725
1726
- QgsGeometry* geom = f.geometry ();
1727
- if ( !geom )
1728
- {
1729
- return ;
1730
- }
1731
-
1732
- // reproject the geometry if necessary (but don't modify the features
1733
- // geometry so that geometry based expression keep working)
1734
- QScopedPointer<QgsGeometry> clonedGeometry;
1735
- if ( ct )
1736
- {
1737
- geom = new QgsGeometry ( *geom );
1738
- clonedGeometry.reset ( geom );
1739
-
1740
- try
1741
- {
1742
- geom->transform ( *ct );
1743
- }
1744
- catch ( QgsCsException &cse )
1745
- {
1746
- Q_UNUSED ( cse );
1747
- QgsDebugMsgLevel ( QString ( " Ignoring feature %1 due transformation exception" ).arg ( f.id () ), 4 );
1748
- return ;
1749
- }
1750
- }
1751
-
1752
- if ( !checkMinimumSizeMM ( context, geom, minFeatureSize ) )
1753
- {
1754
- return ;
1755
- }
1756
-
1757
- // whether we're going to create a centroid for polygon
1758
- bool centroidPoly = (( placement == QgsPalLayerSettings::AroundPoint
1759
- || placement == QgsPalLayerSettings::OverPoint )
1760
- && geom->type () == QGis::Polygon );
1761
-
1762
1726
// data defined centroid whole or clipped?
1763
1727
bool wholeCentroid = centroidWhole;
1764
1728
if ( dataDefinedEvaluate ( QgsPalLayerSettings::CentroidWhole, exprVal ) )
@@ -1779,51 +1743,30 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
1779
1743
}
1780
1744
}
1781
1745
1782
- if ( !geom->asGeos () )
1783
- return ; // there is something really wrong with the geometry
1784
-
1785
- // fix invalid polygons
1786
- if ( geom->type () == QGis::Polygon && !geom->isGeosValid () )
1746
+ QgsGeometry* geom = f.geometry ();
1747
+ if ( !geom )
1787
1748
{
1788
- QgsGeometry* bufferGeom = geom->buffer ( 0 , 0 );
1789
- if ( !bufferGeom )
1790
- {
1791
- return ;
1792
- }
1793
- geom = bufferGeom;
1794
- clonedGeometry.reset ( geom );
1749
+ return ;
1795
1750
}
1796
1751
1797
- // Rotate the geometry if needed, before clipping
1798
- const QgsMapToPixel& m2p = context.mapToPixel ();
1799
- if ( m2p.mapRotation () )
1800
- {
1801
- if ( geom->rotate ( m2p.mapRotation (), context.extent ().center () ) )
1802
- {
1803
- QgsDebugMsg ( QString ( " Error rotating geometry" ).arg ( geom->exportToWkt () ) );
1804
- return ; // really ?
1805
- }
1806
- }
1752
+ // whether we're going to create a centroid for polygon
1753
+ bool centroidPoly = (( placement == QgsPalLayerSettings::AroundPoint
1754
+ || placement == QgsPalLayerSettings::OverPoint )
1755
+ && geom->type () == QGis::Polygon );
1807
1756
1808
1757
// CLIP the geometry if it is bigger than the extent
1809
1758
// don't clip if centroid is requested for whole feature
1810
- bool do_clip = false ;
1759
+ bool doClip = false ;
1811
1760
if ( !centroidPoly || ( centroidPoly && !wholeCentroid ) )
1812
1761
{
1813
- do_clip = !extentGeom->contains ( geom );
1814
- if ( do_clip )
1815
- {
1816
- QgsGeometry* clipGeom = geom->intersection ( extentGeom ); // creates new geometry
1817
- if ( !clipGeom )
1818
- {
1819
- return ;
1820
- }
1821
- geom = clipGeom;
1822
- clonedGeometry.reset ( geom );
1823
- }
1762
+ doClip = true ;
1824
1763
}
1825
1764
1826
- const GEOSGeometry* geos_geom = geom->asGeos ();
1765
+ QScopedPointer<QgsGeometry> preparedGeom ( QgsPalLabeling::prepareGeometry ( geom, context, ct, minFeatureSize, doClip ? extentGeom : 0 ) );
1766
+ if ( !preparedGeom.data () )
1767
+ return ;
1768
+
1769
+ const GEOSGeometry* geos_geom = preparedGeom.data ()->asGeos ();
1827
1770
1828
1771
if ( geos_geom == NULL )
1829
1772
return ; // invalid geometry
@@ -1983,6 +1926,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
1983
1926
angle = angleOffset * M_PI / 180 ; // convert to radians
1984
1927
}
1985
1928
1929
+ const QgsMapToPixel& m2p = context.mapToPixel ();
1986
1930
// data defined rotation?
1987
1931
if ( dataDefinedEvaluate ( QgsPalLayerSettings::Rotation, exprVal ) )
1988
1932
{
@@ -3414,6 +3358,117 @@ void QgsPalLabeling::registerFeature( const QString& layerID, QgsFeature& f, con
3414
3358
lyr.registerFeature ( f, context, dxfLayer );
3415
3359
}
3416
3360
3361
+ QgsGeometry* QgsPalLabeling::prepareGeometry ( QgsGeometry* geometry, const QgsRenderContext& context, const QgsCoordinateTransform* ct, double minSize, QgsGeometry* clipGeometry )
3362
+ {
3363
+ QgsGeometry* geom = geometry;
3364
+ if ( !geom )
3365
+ {
3366
+ return 0 ;
3367
+ }
3368
+
3369
+ // reproject the geometry if necessary (but don't modify the features
3370
+ // geometry so that geometry based expression keep working)
3371
+ QScopedPointer<QgsGeometry> clonedGeometry;
3372
+ if ( ct )
3373
+ {
3374
+ geom = new QgsGeometry ( *geom );
3375
+ clonedGeometry.reset ( geom );
3376
+
3377
+ try
3378
+ {
3379
+ geom->transform ( *ct );
3380
+ }
3381
+ catch ( QgsCsException &cse )
3382
+ {
3383
+ Q_UNUSED ( cse );
3384
+ QgsDebugMsgLevel ( QString ( " Ignoring feature due to transformation exception" ), 4 );
3385
+ return 0 ;
3386
+ }
3387
+ }
3388
+
3389
+ if ( minSize > 0 && !checkMinimumSizeMM ( context, geom, minSize ) )
3390
+ {
3391
+ return 0 ;
3392
+ }
3393
+
3394
+ if ( !geom->asGeos () )
3395
+ return 0 ; // there is something really wrong with the geometry
3396
+
3397
+ // fix invalid polygons
3398
+ if ( geom->type () == QGis::Polygon && !geom->isGeosValid () )
3399
+ {
3400
+ QgsGeometry* bufferGeom = geom->buffer ( 0 , 0 );
3401
+ if ( !bufferGeom )
3402
+ {
3403
+ return 0 ;
3404
+ }
3405
+ geom = bufferGeom;
3406
+ clonedGeometry.reset ( geom );
3407
+ }
3408
+
3409
+ // Rotate the geometry if needed, before clipping
3410
+ const QgsMapToPixel& m2p = context.mapToPixel ();
3411
+ if ( m2p.mapRotation () )
3412
+ {
3413
+ if ( geom->rotate ( m2p.mapRotation (), context.extent ().center () ) )
3414
+ {
3415
+ QgsDebugMsg ( QString ( " Error rotating geometry" ).arg ( geom->exportToWkt () ) );
3416
+ return 0 ;
3417
+ }
3418
+ }
3419
+
3420
+ if ( clipGeometry && !clipGeometry->contains ( geom ) )
3421
+ {
3422
+ QgsGeometry* clipGeom = geom->intersection ( clipGeometry ); // creates new geometry
3423
+ if ( !clipGeom )
3424
+ {
3425
+ return 0 ;
3426
+ }
3427
+ geom = clipGeom;
3428
+ clonedGeometry.reset ( geom );
3429
+ }
3430
+
3431
+ return clonedGeometry.take ();
3432
+ }
3433
+
3434
+ bool QgsPalLabeling::checkMinimumSizeMM ( const QgsRenderContext& context, QgsGeometry* geom, double minSize )
3435
+ {
3436
+ if ( minSize <= 0 )
3437
+ {
3438
+ return true ;
3439
+ }
3440
+
3441
+ if ( !geom )
3442
+ {
3443
+ return false ;
3444
+ }
3445
+
3446
+ QGis::GeometryType featureType = geom->type ();
3447
+ if ( featureType == QGis::Point ) // minimum size does not apply to point features
3448
+ {
3449
+ return true ;
3450
+ }
3451
+
3452
+ double mapUnitsPerMM = context.mapToPixel ().mapUnitsPerPixel () * context.scaleFactor ();
3453
+ if ( featureType == QGis::Line )
3454
+ {
3455
+ double length = geom->length ();
3456
+ if ( length >= 0.0 )
3457
+ {
3458
+ return ( length >= ( minSize * mapUnitsPerMM ) );
3459
+ }
3460
+ }
3461
+ else if ( featureType == QGis::Polygon )
3462
+ {
3463
+ double area = geom->area ();
3464
+ if ( area >= 0.0 )
3465
+ {
3466
+ return ( sqrt ( area ) >= ( minSize * mapUnitsPerMM ) );
3467
+ }
3468
+ }
3469
+ return true ; // should never be reached. Return true in this case to label such geometries anyway.
3470
+ }
3471
+
3417
3472
void QgsPalLabeling::registerDiagramFeature ( const QString& layerID, QgsFeature& feat, const QgsRenderContext& context )
3418
3473
{
3419
3474
// get diagram layer settings, diagram renderer
@@ -3445,28 +3500,13 @@ void QgsPalLabeling::registerDiagramFeature( const QString& layerID, QgsFeature&
3445
3500
3446
3501
// convert geom to geos
3447
3502
QgsGeometry* geom = feat.geometry ();
3503
+ QScopedPointer<QgsGeometry> extentGeom ( QgsGeometry::fromRect ( mMapSettings ->visibleExtent () ) );
3448
3504
3449
- // reproject the geometry if necessary (but don't modify the features
3450
- // geometry so that geometry based expression keep working)
3451
- QScopedPointer<QgsGeometry> clonedGeometry;
3452
- if ( layerIt.value ().ct )
3453
- {
3454
- geom = new QgsGeometry ( *geom );
3455
- clonedGeometry.reset ( geom );
3456
-
3457
- try
3458
- {
3459
- geom->transform ( *( layerIt.value ().ct ) );
3460
- }
3461
- catch ( QgsCsException &cse )
3462
- {
3463
- Q_UNUSED ( cse );
3464
- QgsDebugMsgLevel ( QString ( " Ignoring feature %1 due transformation exception" ).arg ( feat.id () ), 4 );
3465
- return ;
3466
- }
3467
- }
3505
+ QScopedPointer<QgsGeometry> preparedGeom ( QgsPalLabeling::prepareGeometry ( geom, context, layerIt.value ().ct , -1 , extentGeom.data () ) );
3506
+ if ( !preparedGeom.data () )
3507
+ return ;
3468
3508
3469
- const GEOSGeometry* geos_geom = geom ->asGeos ();
3509
+ const GEOSGeometry* geos_geom = preparedGeom. data () ->asGeos ();
3470
3510
if ( geos_geom == 0 )
3471
3511
{
3472
3512
return ; // invalid geometry
0 commit comments