Skip to content

Commit

Permalink
Fix suppress labelling of short lines
Browse files Browse the repository at this point in the history
And deduplicate code
Followup cfe397e
  • Loading branch information
m-kuhn committed Jun 3, 2015
1 parent 8ab005f commit 2b096e0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 45 deletions.
2 changes: 1 addition & 1 deletion python/core/qgspallabeling.sip
Expand Up @@ -812,5 +812,5 @@ class QgsPalLabeling : QgsLabelingEngineInterface
* @returns true if geometry exceeds minimum size
* @note added in QGIS 2.9
*/
static bool checkMinimumSizeMM( const QgsRenderContext &context, QgsGeometry *geom, double minSize );
static bool checkMinimumSizeMM( const QgsRenderContext &context, const QgsGeometry *geom, double minSize );
};
51 changes: 9 additions & 42 deletions src/core/qgspallabeling.cpp
Expand Up @@ -1240,42 +1240,9 @@ bool QgsPalLayerSettings::dataDefinedUseExpression( DataDefinedProperties p ) co
return useExpression;
}

bool QgsPalLayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry* geom, double minSize ) const
bool QgsPalLayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, const 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;
}

double mapUnitsPerMM = ct.mapToPixel().mapUnitsPerPixel() * ct.scaleFactor();
if ( featureType == QGis::Line )
{
double length = geom->length();
if ( length >= 0.0 )
{
return ( length >= ( minSize * mapUnitsPerMM ) );
}
}
else if ( featureType == QGis::Polygon )
{
double area = geom->area();
if ( area >= 0.0 )
{
return ( sqrt( area ) >= ( minSize * mapUnitsPerMM ) );
}
}
return true; //should never be reached. Return true in this case to label such geometries anyway.
return QgsPalLabeling::checkMinimumSizeMM( ct, geom, minSize );
}

void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY, QgsFeature* f )
Expand Down Expand Up @@ -1764,7 +1731,12 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext

const GEOSGeometry* geos_geom = 0;
QScopedPointer<QgsGeometry> preparedGeom;
if ( QgsPalLabeling::geometryRequiresPreparation( geom, context, ct, doClip ? extentGeom : 0 ) )

if ( minFeatureSize > 0 && !checkMinimumSizeMM( context, geom, minFeatureSize ) )
{
return;
}
else if ( QgsPalLabeling::geometryRequiresPreparation( geom, context, ct, doClip ? extentGeom : 0 ) )
{
preparedGeom.reset( QgsPalLabeling::prepareGeometry( geom, context, ct, minFeatureSize, doClip ? extentGeom : 0 ) );
if ( !preparedGeom.data() )
Expand Down Expand Up @@ -3452,11 +3424,6 @@ QgsGeometry* QgsPalLabeling::prepareGeometry( const QgsGeometry* geometry, const
}
}

if ( minSize > 0 && !checkMinimumSizeMM( context, geom, minSize ) )
{
return 0;
}

// Rotate the geometry if needed, before clipping
const QgsMapToPixel& m2p = context.mapToPixel();
if ( !qgsDoubleNear( m2p.mapRotation(), 0 ) )
Expand Down Expand Up @@ -3513,7 +3480,7 @@ QgsGeometry* QgsPalLabeling::prepareGeometry( const QgsGeometry* geometry, const
return clonedGeometry.take();
}

bool QgsPalLabeling::checkMinimumSizeMM( const QgsRenderContext& context, QgsGeometry* geom, double minSize )
bool QgsPalLabeling::checkMinimumSizeMM( const QgsRenderContext& context, const QgsGeometry* geom, double minSize )
{
if ( minSize <= 0 )
{
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgspallabeling.h
Expand Up @@ -558,7 +558,7 @@ class CORE_EXPORT QgsPalLayerSettings

/**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;
bool checkMinimumSizeMM( const QgsRenderContext& ct, const QgsGeometry* geom, double minSize ) const;


QMap<DataDefinedProperties, QVariant> dataDefinedValues;
Expand Down Expand Up @@ -882,7 +882,7 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
* @returns true if geometry exceeds minimum size
* @note added in QGIS 2.9
*/
static bool checkMinimumSizeMM( const QgsRenderContext &context, QgsGeometry *geom, double minSize );
static bool checkMinimumSizeMM( const QgsRenderContext &context, const QgsGeometry *geom, double minSize );

// hashtable of layer settings, being filled during labeling (key = layer ID)
QHash<QString, QgsPalLayerSettings> mActiveLayers;
Expand All @@ -906,6 +906,7 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface

QgsLabelingResults* mResults;

friend class QgsPalLayerSettings;
};
Q_NOWARN_DEPRECATED_POP

Expand Down

2 comments on commit 2b096e0

@nyalldawson
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@m-kuhn I can't test right now, but wouldn't checkMinimumSizeMM need to be called after reprojecting the feature?

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on 2b096e0 Jun 3, 2015 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.