Skip to content

Commit

Permalink
Fix offset labels from reprojected layers when map is rotated
Browse files Browse the repository at this point in the history
Fixes #24796
  • Loading branch information
nyalldawson committed May 28, 2019
1 parent 4cb1213 commit 7f8d43e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
17 changes: 1 addition & 16 deletions src/core/qgspallabeling.cpp
Expand Up @@ -3092,22 +3092,7 @@ QgsGeometry QgsPalLabeling::prepareGeometry( const QgsGeometry &geometry, QgsRen
const QgsMapToPixel &m2p = context.mapToPixel();
if ( !qgsDoubleNear( m2p.mapRotation(), 0 ) )
{
QgsPointXY center = context.extent().center();

if ( ct.isValid() && !ct.isShortCircuited() )
{
try
{
center = ct.transform( center );
}
catch ( QgsCsException &cse )
{
Q_UNUSED( cse )
QgsDebugMsgLevel( QStringLiteral( "Ignoring feature due to transformation exception" ), 4 );
return QgsGeometry();
}
}

QgsPointXY center = context.mapExtent().center();
if ( geom.rotate( m2p.mapRotation(), center ) )
{
QgsDebugMsg( QStringLiteral( "Error rotating geometry" ).arg( geom.asWkt() ) );
Expand Down
59 changes: 59 additions & 0 deletions tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -55,6 +55,7 @@ class TestQgsLabelingEngine : public QObject
void testAdjacentParts();
void testLabelBoundary();
void testLabelBlockingRegion();
void testLabelRotationWithReprojection();

private:
QgsVectorLayer *vl = nullptr;
Expand Down Expand Up @@ -1010,5 +1011,63 @@ void TestQgsLabelingEngine::testLabelBlockingRegion()
QVERIFY( imageCheck( QStringLiteral( "label_blocking_boundary_geometry" ), img, 20 ) );
}

void TestQgsLabelingEngine::testLabelRotationWithReprojection()
{
// test combination of map rotation with reprojected layer
QgsPalLayerSettings settings;
setDefaultLabelParams( settings );

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

settings.fieldName = QStringLiteral( "'X'" );
settings.isExpression = true;
settings.placement = QgsPalLayerSettings::OverPoint;

std::unique_ptr< QgsVectorLayer> vl2( new QgsVectorLayer( QStringLiteral( "Point?crs=epsg:4326&field=id:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) ) );
vl2->setRenderer( new QgsNullSymbolRenderer() );

QgsFeature f;
f.setAttributes( QgsAttributes() << 1 );
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -6.250851540391068, 53.335006994584944 ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );
f.setAttributes( QgsAttributes() << 2 );
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -21.950014487179544, 64.150023619739216 ) ) );
QVERIFY( vl2->dataProvider()->addFeature( f ) );
f.setAttributes( QgsAttributes() << 3 );
f.setGeometry( QgsGeometry::fromPointXY( QgsPointXY( -0.118667702475932, 51.5019405883275 ) ) );
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;
QgsCoordinateReferenceSystem tgtCrs( QStringLiteral( "EPSG:3857" ) );
mapSettings.setDestinationCrs( tgtCrs );

mapSettings.setOutputSize( size );
mapSettings.setExtent( QgsRectangle( -4348530.5, 5618594.3, 2516176.1, 12412237.9 ) );
mapSettings.setRotation( 60 );
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_rotate_with_reproject" ), img, 20 ) );
}

QGSTEST_MAIN( TestQgsLabelingEngine )
#include "testqgslabelingengine.moc"
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 7f8d43e

Please sign in to comment.