Skip to content

Commit 70a07f9

Browse files
committedJun 3, 2019
Add some unit tests for labeling results
1 parent 357328a commit 70a07f9

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
 

‎tests/src/core/testqgslabelingengine.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class TestQgsLabelingEngine : public QObject
6161
void testLabelBoundary();
6262
void testLabelBlockingRegion();
6363
void testLabelRotationWithReprojection();
64+
void labelingResults();
6465

6566
private:
6667
QgsVectorLayer *vl = nullptr;
@@ -1366,5 +1367,110 @@ void TestQgsLabelingEngine::testLabelRotationWithReprojection()
13661367
QVERIFY( imageCheck( QStringLiteral( "label_rotate_with_reproject" ), img, 20 ) );
13671368
}
13681369

1370+
void TestQgsLabelingEngine::labelingResults()
1371+
{
1372+
// test retrieval of labeling results
1373+
QgsPalLayerSettings settings;
1374+
setDefaultLabelParams( settings );
1375+
1376+
QgsTextFormat format = settings.format();
1377+
format.setSize( 20 );
1378+
format.setColor( QColor( 0, 0, 0 ) );
1379+
settings.setFormat( format );
1380+
1381+
settings.fieldName = QStringLiteral( "\"id\"" );
1382+
settings.isExpression = true;
1383+
settings.placement = QgsPalLayerSettings::OverPoint;
1384+
1385+
1386+
std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326&field=id:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
1387+
vl2->setRenderer( new QgsNullSymbolRenderer() );
1388+
1389+
QgsFeature f;
1390+
f.setAttributes( QgsAttributes() << 1 );
1391+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -6.250851540391068, 53.335006994584944 ) ) );
1392+
QVERIFY( vl2->dataProvider()->addFeature( f ) );
1393+
f.setAttributes( QgsAttributes() << 8888 );
1394+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -21.950014487179544, 64.150023619739216 ) ) );
1395+
QVERIFY( vl2->dataProvider()->addFeature( f ) );
1396+
f.setAttributes( QgsAttributes() << 33333 );
1397+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -0.118667702475932, 51.5019405883275 ) ) );
1398+
QVERIFY( vl2->dataProvider()->addFeature( f ) );
1399+
vl2->updateExtents();
1400+
1401+
vl2->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) ); // TODO: this should not be necessary!
1402+
vl2->setLabelsEnabled( true );
1403+
1404+
// make a fake render context
1405+
QSize size( 640, 480 );
1406+
QgsMapSettings mapSettings;
1407+
QgsCoordinateReferenceSystem tgtCrs( QStringLiteral( "EPSG:3857" ) );
1408+
mapSettings.setDestinationCrs( tgtCrs );
1409+
1410+
mapSettings.setOutputSize( size );
1411+
mapSettings.setExtent( QgsRectangle( -4137976.6, 6557092.6, 1585557.4, 9656515.0 ) );
1412+
// mapSettings.setRotation( 60 );
1413+
mapSettings.setLayers( QList<QgsMapLayer *>() << vl2.get() );
1414+
mapSettings.setOutputDpi( 96 );
1415+
1416+
QgsLabelingEngineSettings engineSettings = mapSettings.labelingEngineSettings();
1417+
engineSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, false );
1418+
engineSettings.setFlag( QgsLabelingEngineSettings::DrawLabelRectOnly, true );
1419+
//engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, true );
1420+
mapSettings.setLabelingEngineSettings( engineSettings );
1421+
1422+
QgsMapRendererSequentialJob job( mapSettings );
1423+
job.start();
1424+
job.waitForFinished();
1425+
1426+
std::unique_ptr< QgsLabelingResults > results( job.takeLabelingResults() );
1427+
QVERIFY( results );
1428+
1429+
// retrieve some labels
1430+
QList<QgsLabelPosition> labels = results->labelsAtPosition( QgsPointXY( -654732, 7003282 ) );
1431+
QCOMPARE( labels.count(), 1 );
1432+
QCOMPARE( labels.at( 0 ).featureId, 1 );
1433+
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "1" ) );
1434+
QGSCOMPARENEAR( labels.at( 0 ).width, 167961, 500 ); // tolerance will probably need tweaking, to account for cross-platform font diffs
1435+
QGSCOMPARENEAR( labels.at( 0 ).height, 295119, 500 );
1436+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.xMinimum(), -779822, 500 );
1437+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.xMaximum(), -611861, 500 );
1438+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.yMinimum(), 6897647, 500 );
1439+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.yMaximum(), 7192767, 500 );
1440+
QCOMPARE( labels.at( 0 ).rotation, 0 );
1441+
1442+
labels = results->labelsAtPosition( QgsPointXY( -769822, 6927647 ) );
1443+
QCOMPARE( labels.count(), 1 );
1444+
QCOMPARE( labels.at( 0 ).featureId, 1 );
1445+
labels = results->labelsAtPosition( QgsPointXY( -615861, 7132767 ) );
1446+
QCOMPARE( labels.count(), 1 );
1447+
QCOMPARE( labels.at( 0 ).featureId, 1 );
1448+
1449+
labels = results->labelsAtPosition( QgsPointXY( -2463392, 9361711 ) );
1450+
QCOMPARE( labels.count(), 1 );
1451+
QCOMPARE( labels.at( 0 ).featureId, 2 );
1452+
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "8888" ) );
1453+
QGSCOMPARENEAR( labels.at( 0 ).width, 671844, 500 ); // tolerance will probably need tweaking, to account for cross-platform font diffs
1454+
QGSCOMPARENEAR( labels.at( 0 ).height, 295119, 500 );
1455+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.xMinimum(), -2779386, 500 );
1456+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.xMaximum(), -2107542, 500 );
1457+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.yMinimum(), 9240403, 500 );
1458+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.yMaximum(), 9535523, 500 );
1459+
QCOMPARE( labels.at( 0 ).rotation, 0 );
1460+
labels = results->labelsAtPosition( QgsPointXY( -1383, 6708478 ) );
1461+
QCOMPARE( labels.count(), 1 );
1462+
QCOMPARE( labels.at( 0 ).featureId, 3 );
1463+
QCOMPARE( labels.at( 0 ).labelText, QStringLiteral( "33333" ) );
1464+
QGSCOMPARENEAR( labels.at( 0 ).width, 839805, 500 ); // tolerance will probably need tweaking, to account for cross-platform font diffs
1465+
QGSCOMPARENEAR( labels.at( 0 ).height, 295119, 500 );
1466+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.xMinimum(), -433112, 500 );
1467+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.xMaximum(), 406692, 500 );
1468+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.yMinimum(), 6563006, 500 );
1469+
QGSCOMPARENEAR( labels.at( 0 ).labelRect.yMaximum(), 6858125, 500 );
1470+
QCOMPARE( labels.at( 0 ).rotation, 0 );
1471+
labels = results->labelsAtPosition( QgsPointXY( -2463392, 6708478 ) );
1472+
QCOMPARE( labels.count(), 0 );
1473+
}
1474+
13691475
QGSTEST_MAIN( TestQgsLabelingEngine )
13701476
#include "testqgslabelingengine.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.