Skip to content

Commit 8538be6

Browse files
committedSep 16, 2015
[composer] Fix generation of world file when map is not on first page
fix #13262 (cherry-picked from d897624)
1 parent fc8f30e commit 8538be6

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed
 

‎src/core/composer/qgscomposition.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,35 +2827,38 @@ QGraphicsView *QgsComposition::graphicsView() const
28272827

28282828
void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const
28292829
{
2830-
//
2831-
// Word file parameters : affine transformation parameters from pixel coordinates to map coordinates
2830+
// World file parameters : affine transformation parameters from pixel coordinates to map coordinates
28322831

28332832
if ( !mWorldFileMap )
28342833
{
28352834
return;
28362835
}
28372836

2838-
QRectF brect = mWorldFileMap->mapRectToScene( mWorldFileMap->rect() );
2839-
QgsRectangle extent = *mWorldFileMap->currentMapExtent();
2837+
double destinationHeight = paperHeight();
2838+
double destinationWidth = paperWidth();
2839+
2840+
QRectF mapItemSceneRect = mWorldFileMap->mapRectToScene( mWorldFileMap->rect() );
2841+
QgsRectangle mapExtent = *mWorldFileMap->currentMapExtent();
28402842

28412843
double alpha = mWorldFileMap->mapRotation() / 180 * M_PI;
28422844

2843-
double xr = extent.width() / brect.width();
2844-
double yr = extent.height() / brect.height();
2845+
double xRatio = mapExtent.width() / mapItemSceneRect.width();
2846+
double yRatio = mapExtent.height() / mapItemSceneRect.height();
28452847

2846-
double XC = extent.center().x();
2847-
double YC = extent.center().y();
2848+
double xCenter = mapExtent.center().x();
2849+
double yCenter = mapExtent.center().y();
28482850

2849-
// get the extent for the page
2850-
double xmin = extent.xMinimum() - mWorldFileMap->pos().x() * xr;
2851-
double ymax = extent.yMaximum() + mWorldFileMap->pos().y() * yr;
2852-
QgsRectangle paperExtent( xmin, ymax - paperHeight() * yr, xmin + paperWidth() * xr, ymax );
2851+
// get the extent (in map units) for the page
2852+
QPointF mapItemPosOnPage = mWorldFileMap->pagePos();
2853+
double xmin = mapExtent.xMinimum() - mapItemPosOnPage.x() * xRatio;
2854+
double ymax = mapExtent.yMaximum() + mapItemPosOnPage.y() * yRatio;
2855+
QgsRectangle paperExtent( xmin, ymax - destinationHeight * yRatio, xmin + destinationWidth * xRatio, ymax );
28532856

28542857
double X0 = paperExtent.xMinimum();
28552858
double Y0 = paperExtent.yMinimum();
28562859

2857-
int widthPx = ( int )( printResolution() * paperWidth() / 25.4 );
2858-
int heightPx = ( int )( printResolution() * paperHeight() / 25.4 );
2860+
int widthPx = ( int )( printResolution() * destinationWidth / 25.4 );
2861+
int heightPx = ( int )( printResolution() * destinationHeight / 25.4 );
28592862

28602863
double Ww = paperExtent.width() / widthPx;
28612864
double Hh = paperExtent.height() / heightPx;
@@ -2873,10 +2876,10 @@ void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c
28732876
double r[6];
28742877
r[0] = cos( alpha );
28752878
r[1] = -sin( alpha );
2876-
r[2] = XC * ( 1 - cos( alpha ) ) + YC * sin( alpha );
2879+
r[2] = xCenter * ( 1 - cos( alpha ) ) + yCenter * sin( alpha );
28772880
r[3] = sin( alpha );
28782881
r[4] = cos( alpha );
2879-
r[5] = - XC * sin( alpha ) + YC * ( 1 - cos( alpha ) );
2882+
r[5] = - xCenter * sin( alpha ) + yCenter * ( 1 - cos( alpha ) );
28802883

28812884
// result = rotation x scaling = rotation(scaling(X))
28822885
a = r[0] * s[0] + r[1] * s[3];

‎tests/src/core/testqgscomposermap.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,23 @@ void TestQgsComposerMap::worldFileGeneration()
155155
double a, b, c, d, e, f;
156156
mComposition->computeWorldFileParameters( a, b, c, d, e, f );
157157

158-
QVERIFY( fabs( a - 4.18048 ) < 0.001 );
159-
QVERIFY( fabs( b - 2.41331 ) < 0.001 );
160-
QVERIFY( fabs( c - 779444 ) < 1 );
161-
QVERIFY( fabs( d - 2.4136 ) < 0.001 );
162-
QVERIFY( fabs( e + 4.17997 ) < 0.001 );
163-
QVERIFY( fabs( f - 3.34241e+06 ) < 1e+03 );
158+
QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
159+
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
160+
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
161+
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
162+
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
163+
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );
164+
165+
//test with map on second page. Parameters should be the same
166+
mComposerMap->setItemPosition( 20, 20, QgsComposerItem::UpperLeft, 2 );
167+
mComposition->computeWorldFileParameters( a, b, c, d, e, f );
168+
169+
QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
170+
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
171+
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
172+
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
173+
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
174+
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );
164175

165176
mComposition->setGenerateWorldFile( false );
166177
mComposerMap->setMapRotation( 0.0 );

0 commit comments

Comments
 (0)
Please sign in to comment.