Skip to content
Permalink
Browse files

Make the one band raster creation method return data provider

  • Loading branch information
wonder-sk committed Jan 19, 2017
1 parent 7b27079 commit ff8d91214748e0363010e263401212d0b4e0e491
@@ -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
@@ -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 )
@@ -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
@@ -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() );
@@ -201,6 +208,7 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
QCOMPARE( rlayer->extent(), extent );
QCOMPARE( rlayer->bandCount(), 1 );
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
delete rlayer;
}


0 comments on commit ff8d912

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