Skip to content

Commit

Permalink
[composer] Fix generation of world file when map is not on first page
Browse files Browse the repository at this point in the history
fix #13262
  • Loading branch information
nyalldawson committed Sep 14, 2015
1 parent d081341 commit d897624
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
35 changes: 19 additions & 16 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2835,35 +2835,38 @@ QGraphicsView *QgsComposition::graphicsView() const

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

if ( !mWorldFileMap )
{
return;
}

QRectF brect = mWorldFileMap->mapRectToScene( mWorldFileMap->rect() );
QgsRectangle extent = *mWorldFileMap->currentMapExtent();
double destinationHeight = paperHeight();
double destinationWidth = paperWidth();

QRectF mapItemSceneRect = mWorldFileMap->mapRectToScene( mWorldFileMap->rect() );
QgsRectangle mapExtent = *mWorldFileMap->currentMapExtent();

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

double xr = extent.width() / brect.width();
double yr = extent.height() / brect.height();
double xRatio = mapExtent.width() / mapItemSceneRect.width();
double yRatio = mapExtent.height() / mapItemSceneRect.height();

double XC = extent.center().x();
double YC = extent.center().y();
double xCenter = mapExtent.center().x();
double yCenter = mapExtent.center().y();

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

double X0 = paperExtent.xMinimum();
double Y0 = paperExtent.yMinimum();

int widthPx = ( int )( printResolution() * paperWidth() / 25.4 );
int heightPx = ( int )( printResolution() * paperHeight() / 25.4 );
int widthPx = ( int )( printResolution() * destinationWidth / 25.4 );
int heightPx = ( int )( printResolution() * destinationHeight / 25.4 );

double Ww = paperExtent.width() / widthPx;
double Hh = paperExtent.height() / heightPx;
Expand All @@ -2881,10 +2884,10 @@ void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c
double r[6];
r[0] = cos( alpha );
r[1] = -sin( alpha );
r[2] = XC * ( 1 - cos( alpha ) ) + YC * sin( alpha );
r[2] = xCenter * ( 1 - cos( alpha ) ) + yCenter * sin( alpha );
r[3] = sin( alpha );
r[4] = cos( alpha );
r[5] = - XC * sin( alpha ) + YC * ( 1 - cos( alpha ) );
r[5] = - xCenter * sin( alpha ) + yCenter * ( 1 - cos( alpha ) );

// result = rotation x scaling = rotation(scaling(X))
a = r[0] * s[0] + r[1] * s[3];
Expand Down
23 changes: 17 additions & 6 deletions tests/src/core/testqgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,23 @@ void TestQgsComposerMap::worldFileGeneration()
double a, b, c, d, e, f;
mComposition->computeWorldFileParameters( a, b, c, d, e, f );

QVERIFY( fabs( a - 4.18048 ) < 0.001 );
QVERIFY( fabs( b - 2.41331 ) < 0.001 );
QVERIFY( fabs( c - 779444 ) < 1 );
QVERIFY( fabs( d - 2.4136 ) < 0.001 );
QVERIFY( fabs( e + 4.17997 ) < 0.001 );
QVERIFY( fabs( f - 3.34241e+06 ) < 1e+03 );
QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );

//test with map on second page. Parameters should be the same
mComposerMap->setItemPosition( 20, 20, QgsComposerItem::UpperLeft, 2 );
mComposition->computeWorldFileParameters( a, b, c, d, e, f );

QVERIFY( qgsDoubleNear( a, 4.18048, 0.001 ) );
QVERIFY( qgsDoubleNear( b, 2.41331, 0.001 ) );
QVERIFY( qgsDoubleNear( c, 779444, 1 ) );
QVERIFY( qgsDoubleNear( d, 2.4136, 0.001 ) );
QVERIFY( qgsDoubleNear( e, -4.17997, 0.001 ) );
QVERIFY( qgsDoubleNear( f, 3.34241e+06, 1e+03 ) );

mComposition->setGenerateWorldFile( false );
mComposerMap->setMapRotation( 0.0 );
Expand Down

0 comments on commit d897624

Please sign in to comment.