Skip to content

Commit

Permalink
Address review, add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn authored and nyalldawson committed Apr 7, 2021
1 parent 7dd97e6 commit e58a8c6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/core/pal/layer.cpp
Expand Up @@ -342,7 +342,12 @@ void Layer::joinConnectedFeatures()
// 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();
if ( part->feature()->minimumSize() != 0.0 && part->length() < part->feature()->minimumSize() )
{
delete part;
return true;
}
return false;
} ), mFeatureParts.end() );
}

Expand Down
68 changes: 68 additions & 0 deletions tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -62,6 +62,7 @@ class TestQgsLabelingEngine : public QObject
void testAdjacentParts();
void testTouchingParts();
void testMergingLinesWithForks();
void testMergingLinesWithMinimumSize();
void testCurvedLabelsWithTinySegments();
void testCurvedLabelCorrectLinePlacement();
void testCurvedLabelNegativeDistance();
Expand Down Expand Up @@ -1198,6 +1199,73 @@ void TestQgsLabelingEngine::testMergingLinesWithForks()
QVERIFY( imageCheck( QStringLiteral( "label_multipart_touching_branches" ), img, 20 ) );
}

void TestQgsLabelingEngine::testMergingLinesWithMinimumSize()
{
// test that the "merge connected features" setting works well with
// a non-zero minimum feature size value
QgsPalLayerSettings settings;
setDefaultLabelParams( settings );

QgsTextFormat format = settings.format();
format.setSize( 20 );
format.setColor( QColor( 0, 0, 0 ) );
settings.setFormat( format );

settings.fieldName = QStringLiteral( "'XX'" );
settings.isExpression = true;
settings.placement = QgsPalLayerSettings::Curved;
settings.labelPerPart = false;
settings.lineSettings().setMergeLines( true );
settings.thinningSettings().setMinimumFeatureSize( 90.0 );

// if treated individually, none of these parts are long enough for the label to fit -- but the label should be rendered if the mergeLines setting is true
std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( QStringLiteral( "LineString?crs=epsg:3946&field=id:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
vl2->setRenderer( new QgsNullSymbolRenderer() );

QgsFeature f;
f.setAttributes( QgsAttributes() << 1 );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString (190000 5000010, 190100 5000000)" ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );
// side branch
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString (190100 5000000, 190100 5000010)" ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );
// side branch
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString (190100 5000000, 190100 4999995)" ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );
// main road continues, note that we deliberately split this up into non-consecutive sections, just for extra checks!
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString (190120 5000000, 190200 5000000)" ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "LineString (190120 5000000, 190100 5000000)" ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );

vl2->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) ); // TODO: this should not be necessary!
vl2->setLabelsEnabled( true );

// make a fake render context
QSize size( 640, 480 );
QgsMapSettings mapSettings;
mapSettings.setLabelingEngineSettings( createLabelEngineSettings() );
mapSettings.setDestinationCrs( vl2->crs() );

mapSettings.setOutputSize( size );
mapSettings.setExtent( vl2->extent() );
mapSettings.setLayers( QList<QgsMapLayer *>() << vl2.get() );
mapSettings.setOutputDpi( 96 );

QgsLabelingEngineSettings engineSettings = mapSettings.labelingEngineSettings();
engineSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, false );
engineSettings.setFlag( QgsLabelingEngineSettings::DrawLabelRectOnly, true );
//engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, true );
mapSettings.setLabelingEngineSettings( engineSettings );

QgsMapRendererSequentialJob job( mapSettings );
job.start();
job.waitForFinished();

QImage img = job.renderedImage();
QVERIFY( imageCheck( QStringLiteral( "label_merged_minimum_size" ), img, 20 ) );
}

void TestQgsLabelingEngine::testCurvedLabelsWithTinySegments()
{
// test drawing curved labels when input linestring has many small segments
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e58a8c6

Please sign in to comment.