Skip to content
Permalink
Browse files

Address review, add test case

  • Loading branch information
nirvn authored and nyalldawson committed Apr 7, 2021
1 parent 7dd97e6 commit e58a8c6a13fa52956fa3a50978c0f96b657887af
@@ -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() );
}

@@ -62,6 +62,7 @@ class TestQgsLabelingEngine : public QObject
void testAdjacentParts();
void testTouchingParts();
void testMergingLinesWithForks();
void testMergingLinesWithMinimumSize();
void testCurvedLabelsWithTinySegments();
void testCurvedLabelCorrectLinePlacement();
void testCurvedLabelNegativeDistance();
@@ -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
Binary file not shown.

0 comments on commit e58a8c6

Please sign in to comment.