Skip to content

Commit

Permalink
[labeling] Fix minimum feature size settings not taking merged lines …
Browse files Browse the repository at this point in the history
…into account
  • Loading branch information
nirvn authored and nyalldawson committed Apr 7, 2021
1 parent ad1b06d commit 7dd97e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
21 changes: 21 additions & 0 deletions src/core/labeling/qgslabelfeature.h
Expand Up @@ -530,6 +530,24 @@ class CORE_EXPORT QgsLabelFeature
*/
void setOriginalFeatureCrs( const QgsCoordinateReferenceSystem &crs );

/**
* Returns the minimum size (in map unit) for a feature to be labelled.
*
* \note At the moment this is only used when labeling merged lines
* \see minimumSize()
* \since QGIS 3.20
*/
double minimumSize() const { return mMinimumSize; }

/**
* Sets the minimum \a size (in map unit) for a feature to be labelled.
*
* \note At the moment this is only used when labeling merged lines
* \see setMinimumSize()
* \since QGIS 3.20
*/
void setMinimumSize( double size ) { mMinimumSize = size; }

protected:
//! Pointer to PAL layer (assigned when registered to PAL)
pal::Layer *mLayer = nullptr;
Expand Down Expand Up @@ -611,6 +629,9 @@ class CORE_EXPORT QgsLabelFeature
QgsLabelLineSettings::AnchorType mLineAnchorType = QgsLabelLineSettings::AnchorType::HintOnly;

QgsCoordinateReferenceSystem mOriginalFeatureCrs;

double mMinimumSize = 0.0;

};

#endif // QGSLABELFEATURE_H
17 changes: 15 additions & 2 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -2104,8 +2104,20 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
QgsLabelThinningSettings featureThinningSettings = mThinningSettings;
featureThinningSettings.updateDataDefinedProperties( mDataDefinedProperties, context.expressionContext() );

if ( featureThinningSettings.minimumFeatureSize() > 0 && !checkMinimumSizeMM( context, geom, featureThinningSettings.minimumFeatureSize() ) )
return;
double minimumSize = 0.0;
if ( featureThinningSettings.minimumFeatureSize() > 0 )
{
// for minimum feature size on merged lines, we need to delay the filtering after the merging occurred in PAL
if ( geom.type() == QgsWkbTypes::LineGeometry && mLineSettings.mergeLines() )
{
minimumSize = context.convertToMapUnits( featureThinningSettings.minimumFeatureSize(), QgsUnitTypes::RenderMillimeters );
}
else
{
if ( !checkMinimumSizeMM( context, geom, featureThinningSettings.minimumFeatureSize() ) )
return;
}
}

if ( !geos_geom_clone )
return; // invalid geometry
Expand Down Expand Up @@ -2493,6 +2505,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
( *labelFeature )->setLineAnchorType( lineSettings.anchorType() );
( *labelFeature )->setLabelAllParts( labelAll );
( *labelFeature )->setOriginalFeatureCrs( context.coordinateTransform().sourceCrs() );
( *labelFeature )->setMinimumSize( minimumSize );
if ( geom.type() == QgsWkbTypes::PointGeometry && !obstacleGeometry.isNull() )
{
//register symbol size
Expand Down
6 changes: 6 additions & 0 deletions src/core/pal/layer.cpp
Expand Up @@ -338,6 +338,12 @@ void Layer::joinConnectedFeatures()
}
}
mConnectedHashtable.clear();

// Expunge feature parts that are smaller than the minimum size required
mFeatureParts.erase( std::remove_if( mFeatureParts.begin(), mFeatureParts.end(), []( FeaturePart * part )
{
return part->feature()->minimumSize() != 0.0 && part->length() < part->feature()->minimumSize();
} ), mFeatureParts.end() );
}

int Layer::connectedFeatureId( QgsFeatureId featureId ) const
Expand Down

0 comments on commit 7dd97e6

Please sign in to comment.