Skip to content

Commit cfe397e

Browse files
committed
Better fix for ffd81f4, avoid unnecessary copies of geometry
1 parent cc9f2a6 commit cfe397e

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

python/core/qgspallabeling.sip

+11
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,17 @@ class QgsPalLabeling : QgsLabelingEngineInterface
754754
*/
755755
static QgsGeometry* prepareGeometry( QgsGeometry *geometry, const QgsRenderContext &context, const QgsCoordinateTransform *ct, double minSize = 0, QgsGeometry *clipGeometry = 0 ) /Factory/;
756756

757+
/** Checks whether a geometry requires preparation before registration with PAL
758+
* @param geometry geometry to prepare
759+
* @param context render context
760+
* @param ct coordinate transform
761+
* @param clipGeometry geometry to clip features to, if applicable
762+
* @returns true if geometry requires preparation
763+
* @note added in QGIS 2.9
764+
*/
765+
static bool geometryRequiresPreparation( QgsGeometry *geometry, const QgsRenderContext &context, const QgsCoordinateTransform *ct, QgsGeometry *clipGeometry = 0 );
766+
767+
757768
protected:
758769
// update temporary QgsPalLayerSettings with any data defined text style values
759770
void dataDefinedTextStyle( QgsPalLayerSettings& tmpLyr,

src/core/qgspallabeling.cpp

+54-10
Original file line numberDiff line numberDiff line change
@@ -1762,11 +1762,19 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
17621762
doClip = true;
17631763
}
17641764

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();
1765+
const GEOSGeometry* geos_geom = 0;
1766+
QScopedPointer<QgsGeometry> preparedGeom;
1767+
if ( QgsPalLabeling::geometryRequiresPreparation( geom, context, ct, doClip ? extentGeom : 0 ) )
1768+
{
1769+
preparedGeom.reset( QgsPalLabeling::prepareGeometry( geom, context, ct, minFeatureSize, doClip ? extentGeom : 0 ) );
1770+
if ( !preparedGeom.data() )
1771+
return;
1772+
geos_geom = preparedGeom.data()->asGeos();
1773+
}
1774+
else
1775+
{
1776+
geos_geom = geom->asGeos();
1777+
}
17701778

17711779
if ( geos_geom == NULL )
17721780
return; // invalid geometry
@@ -3358,6 +3366,33 @@ void QgsPalLabeling::registerFeature( const QString& layerID, QgsFeature& f, con
33583366
lyr.registerFeature( f, context, dxfLayer );
33593367
}
33603368

3369+
bool QgsPalLabeling::geometryRequiresPreparation( QgsGeometry* geometry, const QgsRenderContext& context, const QgsCoordinateTransform* ct, QgsGeometry* clipGeometry )
3370+
{
3371+
if ( !geometry )
3372+
{
3373+
return false;
3374+
}
3375+
3376+
//requires reprojection
3377+
if ( ct )
3378+
return true;
3379+
3380+
//requires fixing
3381+
if ( geometry->type() == QGis::Polygon && !geometry->isGeosValid() )
3382+
return true;
3383+
3384+
//requires rotation
3385+
const QgsMapToPixel& m2p = context.mapToPixel();
3386+
if ( !qgsDoubleNear( m2p.mapRotation(), 0 ) )
3387+
return true;
3388+
3389+
//requires clip
3390+
if ( clipGeometry && !clipGeometry->contains( geometry ) )
3391+
return true;
3392+
3393+
return false;
3394+
}
3395+
33613396
QgsGeometry* QgsPalLabeling::prepareGeometry( QgsGeometry* geometry, const QgsRenderContext& context, const QgsCoordinateTransform* ct, double minSize, QgsGeometry* clipGeometry )
33623397
{
33633398
if ( !geometry )
@@ -3406,7 +3441,7 @@ QgsGeometry* QgsPalLabeling::prepareGeometry( QgsGeometry* geometry, const QgsRe
34063441

34073442
// Rotate the geometry if needed, before clipping
34083443
const QgsMapToPixel& m2p = context.mapToPixel();
3409-
if ( m2p.mapRotation() )
3444+
if ( !qgsDoubleNear( m2p.mapRotation(), 0 ) )
34103445
{
34113446
if ( geom->rotate( m2p.mapRotation(), context.extent().center() ) )
34123447
{
@@ -3500,11 +3535,20 @@ void QgsPalLabeling::registerDiagramFeature( const QString& layerID, QgsFeature&
35003535
QgsGeometry* geom = feat.geometry();
35013536
QScopedPointer<QgsGeometry> extentGeom( QgsGeometry::fromRect( mMapSettings->visibleExtent() ) );
35023537

3503-
QScopedPointer<QgsGeometry> preparedGeom( QgsPalLabeling::prepareGeometry( geom, context, layerIt.value().ct, -1, extentGeom.data() ) );
3504-
if ( !preparedGeom.data() )
3505-
return;
3538+
const GEOSGeometry* geos_geom = 0;
3539+
QScopedPointer<QgsGeometry> preparedGeom;
3540+
if ( QgsPalLabeling::geometryRequiresPreparation( geom, context, layerIt.value().ct, extentGeom.data() ) )
3541+
{
3542+
preparedGeom.reset( QgsPalLabeling::prepareGeometry( geom, context, layerIt.value().ct, 0, extentGeom.data() ) );
3543+
if ( !preparedGeom.data() )
3544+
return;
3545+
geos_geom = preparedGeom.data()->asGeos();
3546+
}
3547+
else
3548+
{
3549+
geos_geom = geom->asGeos();
3550+
}
35063551

3507-
const GEOSGeometry* geos_geom = preparedGeom.data()->asGeos();
35083552
if ( geos_geom == 0 )
35093553
{
35103554
return; // invalid geometry

src/core/qgspallabeling.h

+10
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,16 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
823823
*/
824824
static QgsGeometry* prepareGeometry( QgsGeometry *geometry, const QgsRenderContext &context, const QgsCoordinateTransform *ct, double minSize = 0, QgsGeometry *clipGeometry = 0 );
825825

826+
/** Checks whether a geometry requires preparation before registration with PAL
827+
* @param geometry geometry to prepare
828+
* @param context render context
829+
* @param ct coordinate transform
830+
* @param clipGeometry geometry to clip features to, if applicable
831+
* @returns true if geometry requires preparation
832+
* @note added in QGIS 2.9
833+
*/
834+
static bool geometryRequiresPreparation( QgsGeometry *geometry, const QgsRenderContext &context, const QgsCoordinateTransform *ct, QgsGeometry *clipGeometry = 0 );
835+
826836
protected:
827837
// update temporary QgsPalLayerSettings with any data defined text style values
828838
void dataDefinedTextStyle( QgsPalLayerSettings& tmpLyr,

0 commit comments

Comments
 (0)