Skip to content

Commit

Permalink
Make the one band raster creation method return data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 19, 2017
1 parent 7b27079 commit ff8d912
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
12 changes: 7 additions & 5 deletions python/core/raster/qgsrasterfilewriter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ class QgsRasterFileWriter
QgsRasterFileWriter( const QString& outputUrl );

/** Create a raster file with one band without initializing the pixel data.
* Returned provider may be used to initialize the raster using writeBlock() calls.
* Ownership of the returned provider is passed to the caller.
* @note Does not work with tiled mode enabled.
* @returns true when the raster has been created successfully
* @returns Instance of data provider in editing mode (on success) or null on error.
* @note added in QGIS 3.0
*/
bool createOneBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs );
QgsRasterDataProvider* createOneBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs ) /Factory/;

/** Write raster file
@param pipe raster pipe
Expand Down
9 changes: 3 additions & 6 deletions src/core/raster/qgsrasterfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@
#include <QTextStream>
#include <QMessageBox>

bool QgsRasterFileWriter::createOneBandRaster( Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
QgsRasterDataProvider* QgsRasterFileWriter::createOneBandRaster( Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
{
if ( mTiledMode )
return false; // does not make sense with tiled mode
return nullptr; // does not make sense with tiled mode

double pixelSize;
double geoTransform[6];
globalOutputParameters( extent, width, height, geoTransform, pixelSize );

QgsRasterDataProvider* destProvider = initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() );
bool res = destProvider != nullptr;
delete destProvider;
return res;
return initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() );
}

QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl )
Expand Down
12 changes: 7 additions & 5 deletions src/core/raster/qgsrasterfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ class CORE_EXPORT QgsRasterFileWriter
QgsRasterFileWriter( const QString& outputUrl );

/** Create a raster file with one band without initializing the pixel data.
* Returned provider may be used to initialize the raster using writeBlock() calls.
* Ownership of the returned provider is passed to the caller.
* @note Does not work with tiled mode enabled.
* @returns true when the raster has been created successfully
* @returns Instance of data provider in editing mode (on success) or null on error.
* @note added in QGIS 3.0
*/
bool createOneBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs );
QgsRasterDataProvider* createOneBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs );

/** Write raster file
@param pipe raster pipe
Expand Down
12 changes: 10 additions & 2 deletions tests/src/core/testqgsrasterfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,15 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
int width = 200, height = 100;

QgsRasterFileWriter writer( filename );
bool res = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( res );
QgsRasterDataProvider* dp = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( dp );
QCOMPARE( dp->xSize(), width );
QCOMPARE( dp->ySize(), height );
QCOMPARE( dp->extent(), extent );
QCOMPARE( dp->bandCount(), 1 );
QCOMPARE( dp->dataType( 1 ), Qgis::Byte );
QVERIFY( dp->isEditable() );
delete dp;

QgsRasterLayer* rlayer = new QgsRasterLayer( filename, "tmp", "gdal" );
QVERIFY( rlayer->isValid() );
Expand All @@ -201,6 +208,7 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
QCOMPARE( rlayer->extent(), extent );
QCOMPARE( rlayer->bandCount(), 1 );
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
delete rlayer;
}


Expand Down

0 comments on commit ff8d912

Please sign in to comment.