Skip to content
Permalink
Browse files

Change computeWorldFileParameters signature and fix Python unit test

  • Loading branch information
Hugo Mercier authored and mhugent committed Sep 8, 2013
1 parent 50f86c8 commit b18f2736e1dd86df3d60a24b9c8c8925aca3abfc
@@ -133,6 +133,12 @@ class QgsComposition : QGraphicsScene
@note: added in version 1.9*/
void setUseAdvancedEffects( bool effectsEnabled );

bool generateWorldFile() const;
void setGenerateWorldFile( bool enabled );

QgsComposerMap* worldFileMap();
void setWorldFileMap( QgsComposerMap* map );

double selectionTolerance() const;
void setSelectionTolerance( double tol );

@@ -303,6 +309,9 @@ class QgsComposition : QGraphicsScene
@note added in version 1.9*/
void renderPage( QPainter* p, int page );

/** Compute world file parameters */
void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;

QgsAtlasComposition& atlasComposition();

public slots:
@@ -1000,15 +1000,15 @@ void QgsComposer::on_mActionExportAsImage_triggered()
// Write the world file if asked to
if ( mComposition->generateWorldFile() )
{
double params[6];
mComposition->computeWorldFileParameters( params );
double a, b, c, d, e, f;
mComposition->computeWorldFileParameters( a, b, c, d, e, f );

QFileInfo fi( fileNExt.first );
// build the world file name
QString worldFileName = fi.absolutePath() + "/" + fi.baseName() + "."
+ fi.suffix()[0] + fi.suffix()[fi.suffix().size()-1] + "w";

writeWorldFile( worldFileName, params );
writeWorldFile( worldFileName, a, b, c, d, e, f );
}

mView->setPaintingEnabled( true );
@@ -1158,15 +1158,15 @@ void QgsComposer::on_mActionExportAsImage_triggered()
// Write the world file if asked to
if ( mComposition->generateWorldFile() )
{
double params[6];
mComposition->computeWorldFileParameters( params );

QFileInfo fi( filename );
// build the world file name
QString worldFileName = fi.absolutePath() + "/" + fi.baseName() + "."
+ fi.suffix()[0] + fi.suffix()[fi.suffix().size()-1] + "w";

writeWorldFile( worldFileName, params );
double a, b, c, d, e, f;
mComposition->computeWorldFileParameters( a, b, c, d, e, f );
QFileInfo fi( filename );
// build the world file name
QString worldFileName = fi.absolutePath() + "/" + fi.baseName() + "."
+ fi.suffix()[0] + fi.suffix()[fi.suffix().size()-1] + "w";
writeWorldFile( worldFileName, a, b, c, d, e, f );
}
}
atlasMap->endRender();
@@ -2354,7 +2354,7 @@ void QgsComposer::createComposerView()
mViewLayout->addWidget( mView, 1, 1 );
}

void QgsComposer::writeWorldFile( QString worldFileName, double p[6] ) const
void QgsComposer::writeWorldFile( QString worldFileName, double a, double b, double c, double d, double e, double f ) const
{
QFile worldFile( worldFileName );
if ( !worldFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
@@ -2365,10 +2365,10 @@ void QgsComposer::writeWorldFile( QString worldFileName, double p[6] ) const

// QString::number does not use locale settings (for the decimal point)
// which is what we want here
fout << QString::number( p[0] ) << "\r\n";
fout << QString::number( p[3] ) << "\r\n";
fout << QString::number( p[1] ) << "\r\n";
fout << QString::number( p[4] ) << "\r\n";
fout << QString::number( p[2] ) << "\r\n";
fout << QString::number( p[5] ) << "\r\n";
fout << QString::number(a) << "\r\n";
fout << QString::number(b) << "\r\n";
fout << QString::number(c) << "\r\n";
fout << QString::number(d) << "\r\n";
fout << QString::number(e) << "\r\n";
fout << QString::number(f) << "\r\n";
}
@@ -335,7 +335,7 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
void createComposerView();

//! Write a world file
void writeWorldFile( QString fileName, double params[6] ) const;
void writeWorldFile( QString fileName, double a, double b, double c, double d, double e, double f ) const;

/**Composer title*/
QString mTitle;
@@ -2186,7 +2186,7 @@ bool QgsComposition::nearestItem( const QMap< double, const QgsComposerItem* >&
}
}

void QgsComposition::computeWorldFileParameters( double p[6] ) 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
@@ -2240,10 +2240,10 @@ void QgsComposition::computeWorldFileParameters( double p[6] ) const
r[5] = - XC * sin( alpha ) + YC * ( 1 - cos( alpha ) );

// result = rotation x scaling = rotation(scaling(X))
p[0] = r[0] * s[0] + r[1] * s[3];
p[1] = r[0] * s[1] + r[1] * s[4];
p[2] = r[0] * s[2] + r[1] * s[5] + r[2];
p[3] = r[3] * s[0] + r[4] * s[3];
p[4] = r[3] * s[1] + r[4] * s[4];
p[5] = r[3] * s[2] + r[4] * s[5] + r[5];
a = r[0] * s[0] + r[1] * s[3];
b = r[0] * s[1] + r[1] * s[4];
c = r[0] * s[2] + r[1] * s[5] + r[2];
d = r[3] * s[0] + r[4] * s[3];
e = r[3] * s[1] + r[4] * s[4];
f = r[3] * s[2] + r[4] * s[5] + r[5];
}
@@ -364,7 +364,7 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
void renderPage( QPainter* p, int page );

/** Compute world file parameters */
void computeWorldFileParameters( double p[6] ) const;
void computeWorldFileParameters( double& a, double& b, double& c, double& d, double& e, double& f ) const;

QgsAtlasComposition& atlasComposition() { return mAtlasComposition; }

@@ -223,7 +223,7 @@ void TestQgsComposerMap::overviewMapCenter()
QgsComposerMap* overviewMapCenter = new QgsComposerMap( mComposition, 20, 130, 70, 70 );
overviewMapCenter->setFrameEnabled( true );
mComposition->addComposerMap( overviewMapCenter );
mComposerMap->setNewExtent( QgsRectangle( 785462.375+5000, 3341423.125, 789262.375+5000, 3343323.125 ) ); //zoom in
mComposerMap->setNewExtent( QgsRectangle( 785462.375 + 5000, 3341423.125, 789262.375 + 5000, 3343323.125 ) ); //zoom in
mComposerMap->setGridEnabled( false );
overviewMapCenter->setNewExtent( QgsRectangle( 781662.375, 3339523.125, 793062.375, 3350923.125 ) );
overviewMapCenter->setOverviewFrameMap( mComposerMap->id() );
@@ -244,15 +244,15 @@ void TestQgsComposerMap::worldFileGeneration()
mComposition->setGenerateWorldFile( true );
mComposition->setWorldFileMap( mComposerMap );

double params[6];
mComposition->computeWorldFileParameters( params );
double a, b, c, d, e, f;
mComposition->computeWorldFileParameters( a, b, c, d, e, f );

QVERIFY( fabs(params[0] - 4.18048) < 0.001 );
QVERIFY( fabs(params[1] - 2.41331) < 0.001 );
QVERIFY( fabs(params[2] - 779444) < 1 );
QVERIFY( fabs(params[3] - 2.4136) < 0.001 );
QVERIFY( fabs(params[4] + 4.17997) < 0.001 );
QVERIFY( fabs(params[5] - 3.34241e+06) < 1e+03 );
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 );
}

QTEST_MAIN( TestQgsComposerMap )
@@ -249,6 +249,21 @@ def testZebraStyle(self):
myPngPath)
assert testResult == True, myMessage

def testWorldFileGeneration( self ):
myRectangle = QgsRectangle(781662.375, 3339523.125, 793062.375, 3345223.125)
self.mComposerMap.setNewExtent( myRectangle )
self.mComposerMap.setRotation( 30.0 )

self.mComposition.setGenerateWorldFile( True )
self.mComposition.setWorldFileMap( self.mComposerMap )

p = self.mComposition.computeWorldFileParameters()
pexpected = (4.180480199790922, 2.4133064516129026, 779443.7612381146,
2.4136013686911886, -4.179969388427311, 3342408.5663611)
ptolerance = (0.001, 0.001, 1, 0.001, 0.001, 1e+03)
for i in range(0,6):
assert abs(p[i]-pexpected[i]) < ptolerance[i]

if __name__ == '__main__':
unittest.main()

0 comments on commit b18f273

Please sign in to comment.
You can’t perform that action at this time.