Skip to content

Commit 7f8d43e

Browse files
committedMay 28, 2019
Fix offset labels from reprojected layers when map is rotated
Fixes #24796
1 parent 4cb1213 commit 7f8d43e

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed
 

‎src/core/qgspallabeling.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,22 +3092,7 @@ QgsGeometry QgsPalLabeling::prepareGeometry( const QgsGeometry &geometry, QgsRen
30923092
const QgsMapToPixel &m2p = context.mapToPixel();
30933093
if ( !qgsDoubleNear( m2p.mapRotation(), 0 ) )
30943094
{
3095-
QgsPointXY center = context.extent().center();
3096-
3097-
if ( ct.isValid() && !ct.isShortCircuited() )
3098-
{
3099-
try
3100-
{
3101-
center = ct.transform( center );
3102-
}
3103-
catch ( QgsCsException &cse )
3104-
{
3105-
Q_UNUSED( cse )
3106-
QgsDebugMsgLevel( QStringLiteral( "Ignoring feature due to transformation exception" ), 4 );
3107-
return QgsGeometry();
3108-
}
3109-
}
3110-
3095+
QgsPointXY center = context.mapExtent().center();
31113096
if ( geom.rotate( m2p.mapRotation(), center ) )
31123097
{
31133098
QgsDebugMsg( QStringLiteral( "Error rotating geometry" ).arg( geom.asWkt() ) );

‎tests/src/core/testqgslabelingengine.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestQgsLabelingEngine : public QObject
5555
void testAdjacentParts();
5656
void testLabelBoundary();
5757
void testLabelBlockingRegion();
58+
void testLabelRotationWithReprojection();
5859

5960
private:
6061
QgsVectorLayer *vl = nullptr;
@@ -1010,5 +1011,63 @@ void TestQgsLabelingEngine::testLabelBlockingRegion()
10101011
QVERIFY( imageCheck( QStringLiteral( "label_blocking_boundary_geometry" ), img, 20 ) );
10111012
}
10121013

1014+
void TestQgsLabelingEngine::testLabelRotationWithReprojection()
1015+
{
1016+
// test combination of map rotation with reprojected layer
1017+
QgsPalLayerSettings settings;
1018+
setDefaultLabelParams( settings );
1019+
1020+
QgsTextFormat format = settings.format();
1021+
format.setSize( 20 );
1022+
format.setColor( QColor( 0, 0, 0 ) );
1023+
settings.setFormat( format );
1024+
1025+
settings.fieldName = QStringLiteral( "'X'" );
1026+
settings.isExpression = true;
1027+
settings.placement = QgsPalLayerSettings::OverPoint;
1028+
1029+
std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326&field=id:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
1030+
vl2->setRenderer( new QgsNullSymbolRenderer() );
1031+
1032+
QgsFeature f;
1033+
f.setAttributes( QgsAttributes() << 1 );
1034+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -6.250851540391068, 53.335006994584944 ) ) );
1035+
QVERIFY( vl2->dataProvider()->addFeature( f ) );
1036+
f.setAttributes( QgsAttributes() << 2 );
1037+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -21.950014487179544, 64.150023619739216 ) ) );
1038+
QVERIFY( vl2->dataProvider()->addFeature( f ) );
1039+
f.setAttributes( QgsAttributes() << 3 );
1040+
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -0.118667702475932, 51.5019405883275 ) ) );
1041+
QVERIFY( vl2->dataProvider()->addFeature( f ) );
1042+
1043+
vl2->setLabeling( new QgsVectorLayerSimpleLabeling( settings ) ); // TODO: this should not be necessary!
1044+
vl2->setLabelsEnabled( true );
1045+
1046+
// make a fake render context
1047+
QSize size( 640, 480 );
1048+
QgsMapSettings mapSettings;
1049+
QgsCoordinateReferenceSystem tgtCrs( QStringLiteral( "EPSG:3857" ) );
1050+
mapSettings.setDestinationCrs( tgtCrs );
1051+
1052+
mapSettings.setOutputSize( size );
1053+
mapSettings.setExtent( QgsRectangle( -4348530.5, 5618594.3, 2516176.1, 12412237.9 ) );
1054+
mapSettings.setRotation( 60 );
1055+
mapSettings.setLayers( QList<QgsMapLayer *>() << vl2.get() );
1056+
mapSettings.setOutputDpi( 96 );
1057+
1058+
QgsLabelingEngineSettings engineSettings = mapSettings.labelingEngineSettings();
1059+
engineSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, false );
1060+
engineSettings.setFlag( QgsLabelingEngineSettings::DrawLabelRectOnly, true );
1061+
//engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, true );
1062+
mapSettings.setLabelingEngineSettings( engineSettings );
1063+
1064+
QgsMapRendererSequentialJob job( mapSettings );
1065+
job.start();
1066+
job.waitForFinished();
1067+
1068+
QImage img = job.renderedImage();
1069+
QVERIFY( imageCheck( QStringLiteral( "label_rotate_with_reproject" ), img, 20 ) );
1070+
}
1071+
10131072
QGSTEST_MAIN( TestQgsLabelingEngine )
10141073
#include "testqgslabelingengine.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.