Skip to content

Commit

Permalink
Fix generation of world file when exporting canvas
Browse files Browse the repository at this point in the history
Fixes #18491

(cherry-picked from a2ba566)
  • Loading branch information
nyalldawson committed Mar 22, 2018
1 parent af27deb commit 204e0c1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/core/qgsmapsettingsutils.cpp
Expand Up @@ -87,7 +87,7 @@ QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings
s[1] = 0;
s[2] = xOrigin;
s[3] = 0;
s[4] = ms.mapUnitsPerPixel();
s[4] = -ms.mapUnitsPerPixel();
s[5] = yOrigin;

// rotation matrix
Expand All @@ -104,7 +104,9 @@ QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings
double b = r[0] * s[1] + r[1] * s[4];
double c = r[0] * s[2] + r[1] * s[5] + r[2];
double d = r[3] * s[0] + r[4] * s[3];
double e = r[3] * s[1] + r[4] * s[4];
// Pixel YDim - almost always negative
// See https://en.wikipedia.org/wiki/World_file#cite_ref-3, https://issues.qgis.org/issues/18491
double e = r[3] * s[1] + r[4] * s[4];
double f = r[3] * s[2] + r[4] * s[5] + r[5];

QString content;
Expand All @@ -113,10 +115,9 @@ QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings
// Rotation on y axis
content += qgsDoubleToString( d ) + "\r\n";
// Rotation on x axis
content += qgsDoubleToString( -b ) + "\r\n";
// Pixel YDim - almost always negative
// See https://en.wikipedia.org/wiki/World_file#cite_ref-3
content += "-" + qgsDoubleToString( e ) + "\r\n";
content += qgsDoubleToString( b ) + "\r\n";
// Pixel YDim
content += qgsDoubleToString( e ) + "\r\n";
// Origin X (center of top left cell)
content += qgsDoubleToString( c ) + "\r\n";
// Origin Y (center of top left cell)
Expand Down
3 changes: 3 additions & 0 deletions tests/src/core/testqgsmapsettingsutils.cpp
Expand Up @@ -52,6 +52,9 @@ void TestQgsMapSettingsUtils::createWorldFileContent()

mMapSettings.setRotation( 45 );
QCOMPARE( QgsMapSettingsUtils::worldFileContent( mMapSettings ), QString( "0.70710678118654757\r\n0.70710678118654746\r\n0.70710678118654746\r\n-0.70710678118654757\r\n0.5\r\n0.49999999999999994\r\n" ) );

mMapSettings.setRotation( 145 );
QCOMPARE( QgsMapSettingsUtils::worldFileContent( mMapSettings ), QString( "-0.81915204428899191\r\n0.57357643635104594\r\n0.57357643635104594\r\n0.81915204428899191\r\n0.5\r\n0.49999999999999994\r\n" ) );
}

QGSTEST_MAIN( TestQgsMapSettingsUtils )
Expand Down

0 comments on commit 204e0c1

Please sign in to comment.