Skip to content

Commit ea86b93

Browse files
committedSep 6, 2012
more raster Python bindings, raster write test in Python
1 parent e690048 commit ea86b93

15 files changed

+432
-66
lines changed
 

‎python/core/core.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,15 @@
6363
%Include qgsproviderregistry.sip
6464
%Include qgsrasterbandstats.sip
6565
%Include qgsrasterdataprovider.sip
66+
%Include qgsrasterchecker.sip
67+
%Include qgsrasterfilewriter.sip
6668
%Include qgsrasterinterface.sip
6769
%Include qgsrasterlayer.sip
70+
%Include qgsrasterpipe.sip
6871
%Include qgsrasterpyramid.sip
72+
%Include qgsrasterprojector.sip
6973
%Include qgsrasterrenderer.sip
74+
%Include qgsrasterresamplefilter.sip
7075
%Include qgsrasterresampler.sip
7176
%Include qgsrastershader.sip
7277
%Include qgsrastershaderfunction.sip

‎python/core/qgsrasterchecker.sip

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
/** Raster checker for tests in python */
3+
4+
class QgsRasterChecker
5+
{
6+
%TypeHeaderCode
7+
#include <qgsrasterchecker.h>
8+
%End
9+
public:
10+
11+
QgsRasterChecker();
12+
13+
~QgsRasterChecker();
14+
15+
QString report();
16+
17+
bool runTest( QString theVerifiedKey, QString theVerifiedUri,
18+
QString theExpectedKey, QString theExpectedUri );
19+
};
20+

‎python/core/qgsrasterdataprovider.sip

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,63 @@ class QgsRasterDataProvider : QgsDataProvider
1818
//! If you add to this, please also add to capabilitiesString()
1919
enum Capability
2020
{
21-
NoCapabilities = 0,
22-
Identify = 1
23-
// Capability2 = 1 << 1, etc
21+
NoCapabilities,
22+
Identify,
23+
ExactMinimumMaximum,
24+
ExactResolution,
25+
EstimatedMinimumMaximum,
26+
BuildPyramids,
27+
Histogram,
28+
Size,
29+
Create,
30+
Remove
2431
};
2532

33+
// This is modified copy of GDALColorInterp
34+
enum ColorInterpretation
35+
{
36+
UndefinedColorInterpretation = 0,
37+
/*! Greyscale */ GrayIndex = 1,
38+
/*! Paletted (see associated color table) */ PaletteIndex = 2, // indexed color table
39+
/*! Red band of RGBA image */ RedBand = 3,
40+
/*! Green band of RGBA image */ GreenBand = 4,
41+
/*! Blue band of RGBA image */ BlueBand = 5,
42+
/*! Alpha (0=transparent, 255=opaque) */ AlphaBand = 6,
43+
/*! Hue band of HLS image */ HueBand = 7,
44+
/*! Saturation band of HLS image */ SaturationBand = 8,
45+
/*! Lightness band of HLS image */ LightnessBand = 9,
46+
/*! Cyan band of CMYK image */ CyanBand = 10,
47+
/*! Magenta band of CMYK image */ MagentaBand = 11,
48+
/*! Yellow band of CMYK image */ YellowBand = 12,
49+
/*! Black band of CMLY image */ BlackBand = 13,
50+
/*! Y Luminance */ YCbCr_YBand = 14,
51+
/*! Cb Chroma */ YCbCr_CbBand = 15,
52+
/*! Cr Chroma */ YCbCr_CrBand = 16,
53+
/*! Continuous palette, QGIS addition, GRASS */ ContinuousPalette = 17,
54+
/*! Max current value */ ColorInterpretationMax = 17
55+
};
56+
57+
// Progress types
58+
enum RasterProgressType
59+
{
60+
ProgressHistogram = 0,
61+
ProgressPyramids = 1,
62+
ProgressStatistics = 2
63+
};
64+
65+
enum RasterBuildPyramids
66+
{
67+
PyramidsFlagNo = 0,
68+
PyramidsFlagYes = 1,
69+
CopyExisting = 2
70+
};
71+
72+
enum RasterPyramidsFormat
73+
{
74+
PyramidsGTiff = 0,
75+
PyramidsInternal = 1,
76+
PyramidsErdas = 2
77+
};
2678

2779
QgsRasterDataProvider();
2880

@@ -179,6 +231,14 @@ class QgsRasterDataProvider : QgsDataProvider
179231
@note: this method was added in version 1.2*/
180232
void setDpi( int dpi );
181233

234+
/** Get block size */
235+
int xBlockSize() const;
236+
int yBlockSize() const;
237+
238+
/** Get raster size */
239+
int xSize() const;
240+
int ySize() const;
241+
182242
/** read block of data using give extent and size */
183243
/*virtual void readBlock( int bandNo,
184244
QgsRectangle const & viewExtent,

‎python/core/qgsrasterfilewriter.sip

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/** Raster file writer */
3+
4+
class QgsRasterFileWriter
5+
{
6+
%TypeHeaderCode
7+
#include <qgsrasterfilewriter.h>
8+
#include <qgsrasterdataprovider.h>
9+
%End
10+
public:
11+
enum WriterError
12+
{
13+
NoError = 0,
14+
SourceProviderError = 1,
15+
DestProviderError = 2,
16+
CreateDatasourceError = 3,
17+
WriteError = 4,
18+
NoDataConflict = 5
19+
};
20+
21+
QgsRasterFileWriter( const QString& outputUrl );
22+
~QgsRasterFileWriter();
23+
WriterError writeRaster( const QgsRasterPipe* pipe, int nCols, int nRows, QgsRectangle outputExtent,
24+
const QgsCoordinateReferenceSystem& crs, QProgressDialog* p = 0 );
25+
26+
void setOutputFormat( const QString& format );
27+
QString outputFormat() const;
28+
29+
void setOutputProviderKey( const QString& key );
30+
QString outputProviderKey() const;
31+
32+
void setTiledMode( bool t );
33+
bool tiledMode() const;
34+
35+
void setMaxTileWidth( int w );
36+
int maxTileWidth() const;
37+
38+
QgsRasterDataProvider::RasterBuildPyramids buildPyramidsFlag() const;
39+
void setBuildPyramidsFlag( QgsRasterDataProvider::RasterBuildPyramids f );
40+
41+
QList< int > pyramidsList() const;
42+
void setPyramidsList( const QList< int > & list );
43+
44+
QString pyramidsResampling() const;
45+
void setPyramidsResampling( const QString & str );
46+
47+
QgsRasterDataProvider::RasterPyramidsFormat pyramidsFormat() const;
48+
void setPyramidsFormat( QgsRasterDataProvider::RasterPyramidsFormat f );
49+
50+
void setMaxTileHeight( int h );
51+
int maxTileHeight() const;
52+
53+
void setCreateOptions( const QStringList& list );
54+
QStringList createOptions() const;
55+
};
56+

‎python/core/qgsrasterpipe.sip

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/** Raster pipe */
3+
4+
class QgsRasterPipe
5+
{
6+
%TypeHeaderCode
7+
#include <qgsrasterpipe.h>
8+
#include <qgsrasterresamplefilter.h>
9+
#include <qgsrasterprojector.h>
10+
%End
11+
public:
12+
enum Role
13+
{
14+
UnknownRole = 0,
15+
ProviderRole = 1,
16+
RendererRole = 2,
17+
ResamplerRole = 3,
18+
ProjectorRole = 4
19+
};
20+
21+
QgsRasterPipe();
22+
QgsRasterPipe( const QgsRasterPipe& thePipe );
23+
24+
~QgsRasterPipe();
25+
26+
bool insert( int idx, QgsRasterInterface* theInterface );
27+
28+
bool replace( int idx, QgsRasterInterface* theInterface );
29+
30+
bool set( QgsRasterInterface * theInterface );
31+
32+
bool remove( int idx );
33+
34+
bool remove( QgsRasterInterface * theInterface );
35+
36+
int size() const;
37+
QgsRasterInterface * at( int idx ) const;
38+
QgsRasterInterface * last() const;
39+
40+
bool setOn( int idx, bool on );
41+
42+
bool canSetOn( int idx, bool on );
43+
44+
QgsRasterDataProvider * provider() const;
45+
QgsRasterRenderer * renderer() const;
46+
QgsRasterResampleFilter * resampleFilter() const;
47+
QgsRasterProjector * projector() const;
48+
49+
void setStatsOn( bool on );
50+
};
51+

‎python/core/qgsrasterprojector.sip

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/** Raster projector */
3+
4+
class QgsRasterProjector
5+
{
6+
%TypeHeaderCode
7+
#include <qgsrasterprojector.h>
8+
#include <qgscoordinatereferencesystem.h>
9+
%End
10+
public:
11+
QgsRasterProjector(
12+
QgsCoordinateReferenceSystem theSrcCRS,
13+
QgsCoordinateReferenceSystem theDestCRS,
14+
QgsRectangle theDestExtent,
15+
int theDestRows, int theDestCols,
16+
double theMaxSrcXRes, double theMaxSrcYRes,
17+
QgsRectangle theExtent
18+
);
19+
QgsRasterProjector(
20+
QgsCoordinateReferenceSystem theSrcCRS,
21+
QgsCoordinateReferenceSystem theDestCRS,
22+
double theMaxSrcXRes, double theMaxSrcYRes,
23+
QgsRectangle theExtent
24+
);
25+
QgsRasterProjector();
26+
27+
~QgsRasterProjector();
28+
29+
QgsRasterInterface * clone() const;
30+
31+
int bandCount() const;
32+
33+
QgsRasterInterface::DataType dataType( int bandNo ) const;
34+
35+
void setCRS( const QgsCoordinateReferenceSystem & theSrcCRS, const QgsCoordinateReferenceSystem & theDestCRS );
36+
37+
QgsCoordinateReferenceSystem srcCrs() const;
38+
39+
QgsCoordinateReferenceSystem destCrs() const;
40+
41+
void setMaxSrcRes( double theMaxSrcXRes, double theMaxSrcYRes );
42+
43+
QgsRectangle srcExtent();
44+
45+
int srcRows();
46+
int srcCols();
47+
void setSrcRows( int theRows );
48+
void setSrcCols( int theCols );
49+
50+
void srcRowCol( int theDestRow, int theDestCol, int *theSrcRow, int *theSrcCol );
51+
52+
int dstRows() const;
53+
int dstCols() const;
54+
55+
void * readBlock( int bandNo, const QgsRectangle & extent, int width, int height );
56+
};
57+
Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
1+
2+
/** Raster resample filter */
3+
14
class QgsRasterResampleFilter
25
{
36
%TypeHeaderCode
4-
#include "qgsrasterresamplefilter.h"
7+
#include <qgsrasterresamplefilter.h>
58
%End
9+
QgsRasterResampleFilter();
10+
QgsRasterResampleFilter( const QgsRasterResampleFilter& thePipe );
611

7-
public:
8-
QgsRasterResampleFilter( QgsRasterFace* input = 0 );
912
~QgsRasterResampleFilter();
1013

14+
QgsRasterInterface * clone() const;
15+
16+
int bandCount() const;
17+
18+
QgsRasterInterface::DataType dataType( int bandNo ) const;
19+
20+
bool setInput( QgsRasterInterface* input );
21+
1122
void * readBlock( int bandNo, const QgsRectangle & extent, int width, int height );
1223

13-
/**Set resampler for zoomed in scales. Takes ownership of the object*/
1424
void setZoomedInResampler( QgsRasterResampler* r );
15-
const QgsRasterResampler* zoomedInResampler();
25+
const QgsRasterResampler* zoomedInResampler() const;
1626

17-
/**Set resampler for zoomed out scales. Takes ownership of the object*/
1827
void setZoomedOutResampler( QgsRasterResampler* r );
1928
const QgsRasterResampler* zoomedOutResampler() const;
2029

2130
void setMaxOversampling( double os );
2231
double maxOversampling() const;
2332

24-
virtual void writeXML( QDomDocument& doc, QDomElement& parentElem ) const = 0;
33+
void writeXML( QDomDocument& doc, QDomElement& parentElem );
2534

26-
/**Sets base class members from xml. Usually called from create() methods of subclasses*/
27-
void readXML( const QDomElement& rendererElem );
35+
void readXML( const QDomElement& resamplefilterElem );
2836
};
2937

‎src/core/qgsrasterprojector.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ QgsRasterProjector::QgsRasterProjector()
6666
QgsDebugMsg( "Entered" );
6767
}
6868

69+
QgsRasterProjector::QgsRasterProjector( const QgsRasterProjector &projector )
70+
{
71+
mSrcCRS = projector.mSrcCRS;
72+
mDestCRS = projector.mDestCRS;
73+
mMaxSrcXRes = projector.mMaxSrcXRes;
74+
mMaxSrcYRes = projector.mMaxSrcYRes;
75+
mExtent = projector.mExtent;
76+
mCoordinateTransform.setSourceCrs( mSrcCRS );
77+
mCoordinateTransform.setDestCRS( mDestCRS );
78+
}
79+
80+
QgsRasterProjector & QgsRasterProjector::operator=( const QgsRasterProjector & projector )
81+
{
82+
if ( &projector != this )
83+
{
84+
mSrcCRS = projector.mSrcCRS;
85+
mDestCRS = projector.mDestCRS;
86+
mMaxSrcXRes = projector.mMaxSrcXRes;
87+
mMaxSrcYRes = projector.mMaxSrcYRes;
88+
mExtent = projector.mExtent;
89+
mCoordinateTransform.setSourceCrs( mSrcCRS );
90+
mCoordinateTransform.setDestCRS( mDestCRS );
91+
}
92+
return *this;
93+
}
94+
6995
QgsRasterInterface * QgsRasterProjector::clone() const
7096
{
7197
QgsDebugMsg( "Entered" );
@@ -93,7 +119,7 @@ QgsRasterInterface::DataType QgsRasterProjector::dataType( int bandNo ) const
93119
return QgsRasterInterface::UnknownDataType;
94120
}
95121

96-
void QgsRasterProjector::setCRS( QgsCoordinateReferenceSystem theSrcCRS, QgsCoordinateReferenceSystem theDestCRS )
122+
void QgsRasterProjector::setCRS( const QgsCoordinateReferenceSystem & theSrcCRS, const QgsCoordinateReferenceSystem & theDestCRS )
97123
{
98124
mSrcCRS = theSrcCRS;
99125
mDestCRS = theDestCRS;

‎src/core/qgsrasterprojector.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,22 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface
5757
QgsRectangle theExtent
5858
);
5959
QgsRasterProjector();
60+
// * copy constructor to avoid synthesized which fails on copy of QgsCoordinateTransform (QObject child) in Python bindings
61+
QgsRasterProjector( const QgsRasterProjector &projector );
6062

6163
/** \brief The destructor */
6264
~QgsRasterProjector();
6365

66+
QgsRasterProjector & operator=( const QgsRasterProjector &projector );
67+
6468
QgsRasterInterface * clone() const;
6569

6670
int bandCount() const;
6771

6872
QgsRasterInterface::DataType dataType( int bandNo ) const;
6973

7074
/** \brief set source and destination CRS */
71-
void setCRS( QgsCoordinateReferenceSystem theSrcCRS, QgsCoordinateReferenceSystem theDestCRS );
75+
void setCRS( const QgsCoordinateReferenceSystem & theSrcCRS, const QgsCoordinateReferenceSystem & theDestCRS );
7276

7377
/** \brief Get source CRS */
7478
QgsCoordinateReferenceSystem srcCrs() const { return mSrcCRS; }
@@ -82,6 +86,25 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface
8286
mMaxSrcXRes = theMaxSrcXRes; mMaxSrcYRes = theMaxSrcYRes;
8387
}
8488

89+
/** get source extent */
90+
QgsRectangle srcExtent() { return mSrcExtent; }
91+
92+
/** get/set source width/height */
93+
int srcRows() { return mSrcRows; }
94+
int srcCols() { return mSrcCols; }
95+
void setSrcRows( int theRows ) { mSrcRows = theRows; mSrcXRes = mSrcExtent.height() / mSrcRows; }
96+
void setSrcCols( int theCols ) { mSrcCols = theCols; mSrcYRes = mSrcExtent.width() / mSrcCols; }
97+
98+
/** \brief Get source row and column indexes for current source extent and resolution */
99+
void srcRowCol( int theDestRow, int theDestCol, int *theSrcRow, int *theSrcCol );
100+
101+
int dstRows() const { return mDestRows; }
102+
int dstCols() const { return mDestCols; }
103+
104+
void * readBlock( int bandNo, QgsRectangle const & extent, int width, int height );
105+
106+
107+
private:
85108
/** \brief get destination point for _current_ destination position */
86109
void destPointOnCPMatrix( int theRow, int theCol, double *theX, double *theY );
87110

@@ -98,9 +121,6 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface
98121
/** \brief Get approximate source row and column indexes for current source extent and resolution */
99122
inline void approximateSrcRowCol( int theDestRow, int theDestCol, int *theSrcRow, int *theSrcCol );
100123

101-
/** \brief Get source row and column indexes for current source extent and resolution */
102-
void srcRowCol( int theDestRow, int theDestCol, int *theSrcRow, int *theSrcCol );
103-
104124
/** \brief Calculate matrix */
105125
void calc();
106126

@@ -139,24 +159,9 @@ class CORE_EXPORT QgsRasterProjector : public QgsRasterInterface
139159
/** Calc / switch helper */
140160
void nextHelper();
141161

142-
/** get source extent */
143-
QgsRectangle srcExtent() { return mSrcExtent; }
144-
145-
/** get/set source width/height */
146-
int srcRows() { return mSrcRows; }
147-
int srcCols() { return mSrcCols; }
148-
void setSrcRows( int theRows ) { mSrcRows = theRows; mSrcXRes = mSrcExtent.height() / mSrcRows; }
149-
void setSrcCols( int theCols ) { mSrcCols = theCols; mSrcYRes = mSrcExtent.width() / mSrcCols; }
150-
151162
/** get mCPMatrix as string */
152163
QString cpToString();
153164

154-
int dstRows() const { return mDestRows; }
155-
int dstCols() const { return mDestCols; }
156-
157-
void * readBlock( int bandNo, QgsRectangle const & extent, int width, int height );
158-
159-
private:
160165
/** Source CRS */
161166
QgsCoordinateReferenceSystem mSrcCRS;
162167

‎src/core/raster/qgsrasterchecker.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class CORE_EXPORT QgsRasterChecker
3535
//! Destructor
3636
~QgsRasterChecker() {};
3737

38-
QString controlImagePath() const;
39-
4038
QString report() { return mReport; };
4139
/**
4240
* Test using renderer to generate the image to be compared.
@@ -45,7 +43,7 @@ class CORE_EXPORT QgsRasterChecker
4543
* @param theExpectedKey expected provider key
4644
* @param theExpectedUri URI of the expected (control) raster
4745
*/
48-
bool runTest( QString theVerifiedKey, QString theVerifiedUri,
46+
bool runTest( QString theVerifiedKey, QString theVerifiedUri,
4947
QString theExpectedKey, QString theExpectedUri );
5048
private:
5149
QString mReport;

‎src/core/raster/qgsrasterpipe.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,34 +195,34 @@ bool QgsRasterPipe::set( QgsRasterInterface* theInterface )
195195
return insert( idx, theInterface ); // insert may still fail and return false
196196
}
197197

198-
QgsRasterInterface * QgsRasterPipe::iface( Role role ) const
199-
{
200-
QgsDebugMsg( QString( "role = %1" ).arg( role ) );
201-
if ( mRoleMap.contains( role ) )
198+
QgsRasterInterface * QgsRasterPipe::interface( Role role ) const
202199
{
203-
return mInterfaces.value( mRoleMap.value( role ) );
200+
QgsDebugMsg( QString( "role = %1" ).arg( role ) );
201+
if ( mRoleMap.contains( role ) )
202+
{
203+
return mInterfaces.value( mRoleMap.value( role ) );
204+
}
205+
return 0;
204206
}
205-
return 0;
206-
}
207207

208208
QgsRasterDataProvider * QgsRasterPipe::provider() const
209209
{
210-
return dynamic_cast<QgsRasterDataProvider *>( iface( ProviderRole ) );
210+
return dynamic_cast<QgsRasterDataProvider *>( interface( ProviderRole ) );
211211
}
212212

213213
QgsRasterRenderer * QgsRasterPipe::renderer() const
214214
{
215-
return dynamic_cast<QgsRasterRenderer *>( iface( RendererRole ) );
215+
return dynamic_cast<QgsRasterRenderer *>( interface( RendererRole ) );
216216
}
217217

218218
QgsRasterResampleFilter * QgsRasterPipe::resampleFilter() const
219219
{
220-
return dynamic_cast<QgsRasterResampleFilter *>( iface( ResamplerRole ) );
220+
return dynamic_cast<QgsRasterResampleFilter *>( interface( ResamplerRole ) );
221221
}
222222

223223
QgsRasterProjector * QgsRasterPipe::projector() const
224224
{
225-
return dynamic_cast<QgsRasterProjector*>( iface( ProjectorRole ) );
225+
return dynamic_cast<QgsRasterProjector*>( interface( ProjectorRole ) );
226226
}
227227

228228
bool QgsRasterPipe::remove( int idx )

‎src/core/raster/qgsrasterpipe.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ class CORE_EXPORT QgsRasterPipe
4747
QgsRasterPipe( );
4848
QgsRasterPipe( const QgsRasterPipe& thePipe );
4949

50-
virtual ~QgsRasterPipe();
51-
52-
/** \brief Try to connect interfaces in pipe and to the provider at beginning.
53-
Returns true if connected or false if connection failed */
54-
bool connect( QVector<QgsRasterInterface*> theInterfaces );
50+
~QgsRasterPipe();
5551

5652
/** Try to insert interface at specified index and connect
5753
* if connection would fail, the interface is not inserted and false is returned */
@@ -69,9 +65,6 @@ class CORE_EXPORT QgsRasterPipe
6965
*/
7066
bool set( QgsRasterInterface * theInterface );
7167

72-
/** Get known interface by role */
73-
QgsRasterInterface * iface( Role role ) const;
74-
7568
/** Remove and delete interface at given index if possible */
7669
bool remove( int idx );
7770

@@ -115,6 +108,13 @@ class CORE_EXPORT QgsRasterPipe
115108

116109
// Check if index is in bounds
117110
bool checkBounds( int idx ) const;
111+
112+
/** Get known interface by role */
113+
QgsRasterInterface * interface( Role role ) const;
114+
115+
/** \brief Try to connect interfaces in pipe and to the provider at beginning.
116+
Returns true if connected or false if connection failed */
117+
bool connect( QVector<QgsRasterInterface*> theInterfaces );
118118
};
119119

120120
#endif

‎tests/src/core/testqgsrasterfilewriter.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class TestQgsRasterFileWriter: public QObject
4747
void writeTest();
4848
private:
4949
bool writeTest( QString rasterName );
50-
void log ( QString msg );
51-
void logError ( QString msg );
50+
void log( QString msg );
51+
void logError( QString msg );
5252
QString mTestDataDir;
5353
QString mReport;
5454
};
@@ -85,7 +85,7 @@ void TestQgsRasterFileWriter::cleanupTestCase()
8585
void TestQgsRasterFileWriter::writeTest()
8686
{
8787
QDir dir( mTestDataDir + "/raster" );
88-
88+
8989
QStringList filters;
9090
filters << "*.tif";
9191
QStringList rasterNames = dir.entryList( filters, QDir::Files );
@@ -108,7 +108,7 @@ bool TestQgsRasterFileWriter::writeTest( QString theRasterName )
108108
QFileInfo myRasterFileInfo( myFileName );
109109

110110
QgsRasterLayer * mpRasterLayer = new QgsRasterLayer( myRasterFileInfo.filePath(),
111-
myRasterFileInfo.completeBaseName() );
111+
myRasterFileInfo.completeBaseName() );
112112
qDebug() << theRasterName << " metadata: " << mpRasterLayer->dataProvider()->metadata();
113113

114114
if ( !mpRasterLayer->isValid() ) return false;
@@ -122,15 +122,15 @@ bool TestQgsRasterFileWriter::writeTest( QString theRasterName )
122122
QString tmpName = tmpFile.fileName();
123123
tmpFile.close();
124124
// do not remove when class is destroyd so that we can read the file and see difference
125-
tmpFile.setAutoRemove ( false );
125+
tmpFile.setAutoRemove( false );
126126
qDebug() << "temporary output file: " << tmpName;
127127
mReport += "temporary output file: " + tmpName + "<br>";
128128

129129
QgsRasterFileWriter fileWriter( tmpName );
130130
QgsRasterPipe* pipe = new QgsRasterPipe();
131131
if ( !pipe->set( provider->clone() ) )
132132
{
133-
logError ( "Cannot set pipe provider" );
133+
logError( "Cannot set pipe provider" );
134134
return false;
135135
}
136136
qDebug() << "provider set";
@@ -145,7 +145,7 @@ bool TestQgsRasterFileWriter::writeTest( QString theRasterName )
145145
}
146146
qDebug() << "nuller set";
147147

148-
// Reprojection not really done
148+
// Reprojection not really done
149149
QgsRasterProjector *projector = new QgsRasterProjector;
150150
projector->setCRS( provider->crs(), provider->crs() );
151151
if ( !pipe->insert( 2, projector ) )
@@ -155,26 +155,26 @@ bool TestQgsRasterFileWriter::writeTest( QString theRasterName )
155155
}
156156
qDebug() << "projector set";
157157

158-
fileWriter.writeRaster( pipe, provider->xSize() + 1, provider->ySize(), provider->extent(), provider->crs() );
158+
fileWriter.writeRaster( pipe, provider->xSize(), provider->ySize(), provider->extent(), provider->crs() );
159159

160160
delete pipe;
161161

162162
QgsRasterChecker checker;
163-
bool ok = checker.runTest( "gdal", tmpName, "gdal", myRasterFileInfo.filePath() );
163+
bool ok = checker.runTest( "gdal", tmpName, "gdal", myRasterFileInfo.filePath() );
164164
mReport += checker.report();
165165

166166
// All OK, we can delete the file
167-
tmpFile.setAutoRemove ( ok );
167+
tmpFile.setAutoRemove( ok );
168168

169169
return true;
170170
}
171171

172-
void TestQgsRasterFileWriter::log ( QString msg )
172+
void TestQgsRasterFileWriter::log( QString msg )
173173
{
174174
mReport += msg + "<br>";
175175
}
176176

177-
void TestQgsRasterFileWriter::logError ( QString msg )
177+
void TestQgsRasterFileWriter::logError( QString msg )
178178
{
179179
mReport += "Error:<font color='red'>" + msg + "</font><br>";
180180
qDebug() << msg;

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ADD_PYTHON_TEST(PyQgsComposerHtml test_qgscomposerhtml.py)
44
ADD_PYTHON_TEST(PyQgsComposerMap test_qgscomposermap.py)
55
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)
66
ADD_PYTHON_TEST(PyQgsRasterLayer test_qgsrasterlayer.py)
7+
ADD_PYTHON_TEST(PyQgsRasterFileWriter test_qgsrasterfilewriter.py)
78
ADD_PYTHON_TEST(PyQgsMemoryProvider test_qgsmemoryprovider.py)
89
ADD_PYTHON_TEST(PyQgsLogger test_qgslogger.py)
910
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import os, glob
2+
import unittest
3+
4+
from qgis.core import QgsRasterLayer, QgsRasterChecker, QgsRasterPipe, QgsRasterFileWriter, QgsRasterProjector
5+
from PyQt4.QtCore import QFileInfo, QString, QStringList, QTemporaryFile, QDir
6+
7+
# Convenience instances in case you may need them
8+
# not used in this test
9+
from utilities import getQgisTestApp
10+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
11+
12+
class TestQgsRasterFileWriter(unittest.TestCase):
13+
def __init__(self,methodName):
14+
unittest.TestCase.__init__(self,methodName)
15+
self.testDataDir = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'testdata'))
16+
self.report = "<h1>Python Raster File Writer Tests</h1>\n"
17+
18+
def write(self, theRasterName):
19+
print theRasterName
20+
21+
path = "%s/%s" % ( self.testDataDir, theRasterName )
22+
#myFileInfo = QFileInfo( path )
23+
#myBaseName = myFileInfo.baseName()
24+
rasterLayer = QgsRasterLayer(path, "test")
25+
if not rasterLayer.isValid(): return False
26+
provider = rasterLayer.dataProvider()
27+
28+
tmpFile = QTemporaryFile()
29+
tmpFile.open() # fileName is no avialable until open
30+
tmpName = tmpFile.fileName()
31+
tmpFile.close();
32+
# do not remove when class is destroyd so that we can read the file and see difference
33+
tmpFile.setAutoRemove ( False )
34+
35+
fileWriter = QgsRasterFileWriter ( tmpName )
36+
pipe = QgsRasterPipe()
37+
if not pipe.set( provider.clone() ):
38+
print "Cannot set pipe provider"
39+
return False
40+
41+
#nuller = QgsRasterNuller()
42+
#nuller.setNoData( ... )
43+
#if not pipe.insert( 1, nuller ):
44+
# print "Cannot set pipe nuller"
45+
# return False
46+
47+
projector = QgsRasterProjector()
48+
projector.setCRS( provider.crs(), provider.crs() )
49+
if not pipe.insert( 2, projector.clone() ):
50+
print "Cannot set pipe projector"
51+
return False
52+
53+
fileWriter.writeRaster( pipe, provider.xSize(), provider.ySize(), provider.extent(), provider.crs() )
54+
55+
checker = QgsRasterChecker()
56+
ok = checker.runTest( "gdal", tmpName, "gdal", path );
57+
self.report += checker.report();
58+
59+
# All OK, we can delete the file
60+
tmpFile.setAutoRemove ( ok );
61+
62+
return ok
63+
64+
def testWrite(self):
65+
for name in glob.glob( "%s/raster/*.tif" % self.testDataDir ):
66+
baseName = os.path.basename ( name )
67+
allOk = True
68+
ok = self.write( "raster/%s" % baseName )
69+
if not ok: allOk = False
70+
71+
reportFilePath = "%s/qgistest.html" % QDir.tempPath()
72+
reportFile = open(reportFilePath,'a')
73+
reportFile.write( self.report )
74+
reportFile.close()
75+
76+
assert allOk, "Raster file writer test failed"
77+
78+
if __name__ == '__main__':
79+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.