Skip to content

Commit a2ba566

Browse files
committed
Fix generation of world file when exporting canvas
Fixes #18491
1 parent 8a5924d commit a2ba566

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/core/qgsmapsettingsutils.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings
8787
s[1] = 0;
8888
s[2] = xOrigin;
8989
s[3] = 0;
90-
s[4] = ms.mapUnitsPerPixel();
90+
s[4] = -ms.mapUnitsPerPixel();
9191
s[5] = yOrigin;
9292

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

110112
QString content;
@@ -113,10 +115,9 @@ QString QgsMapSettingsUtils::worldFileContent( const QgsMapSettings &mapSettings
113115
// Rotation on y axis
114116
content += qgsDoubleToString( d ) + "\r\n";
115117
// Rotation on x axis
116-
content += qgsDoubleToString( -b ) + "\r\n";
117-
// Pixel YDim - almost always negative
118-
// See https://en.wikipedia.org/wiki/World_file#cite_ref-3
119-
content += "-" + qgsDoubleToString( e ) + "\r\n";
118+
content += qgsDoubleToString( b ) + "\r\n";
119+
// Pixel YDim
120+
content += qgsDoubleToString( e ) + "\r\n";
120121
// Origin X (center of top left cell)
121122
content += qgsDoubleToString( c ) + "\r\n";
122123
// Origin Y (center of top left cell)

tests/src/core/testqgsmapsettingsutils.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ void TestQgsMapSettingsUtils::createWorldFileContent()
5252

5353
mMapSettings.setRotation( 45 );
5454
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" ) );
55+
56+
mMapSettings.setRotation( 145 );
57+
QCOMPARE( QgsMapSettingsUtils::worldFileContent( mMapSettings ), QString( "-0.81915204428899191\r\n0.57357643635104594\r\n0.57357643635104594\r\n0.81915204428899191\r\n0.5\r\n0.49999999999999994\r\n" ) );
5558
}
5659

5760
QGSTEST_MAIN( TestQgsMapSettingsUtils )

0 commit comments

Comments
 (0)