Skip to content

Commit ff8d912

Browse files
committed
Make the one band raster creation method return data provider
1 parent 7b27079 commit ff8d912

4 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎python/core/raster/qgsrasterfilewriter.sip‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ class QgsRasterFileWriter
3030
QgsRasterFileWriter( const QString& outputUrl );
3131

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

4244
/** Write raster file
4345
@param pipe raster pipe

‎src/core/raster/qgsrasterfilewriter.cpp‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@
2929
#include <QTextStream>
3030
#include <QMessageBox>
3131

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

3737
double pixelSize;
3838
double geoTransform[6];
3939
globalOutputParameters( extent, width, height, geoTransform, pixelSize );
4040

41-
QgsRasterDataProvider* destProvider = initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() );
42-
bool res = destProvider != nullptr;
43-
delete destProvider;
44-
return res;
41+
return initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() );
4542
}
4643

4744
QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl )

‎src/core/raster/qgsrasterfilewriter.h‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ class CORE_EXPORT QgsRasterFileWriter
5656
QgsRasterFileWriter( const QString& outputUrl );
5757

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

6870
/** Write raster file
6971
@param pipe raster pipe

‎tests/src/core/testqgsrasterfilewriter.cpp‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,15 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
191191
int width = 200, height = 100;
192192

193193
QgsRasterFileWriter writer( filename );
194-
bool res = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
195-
QVERIFY( res );
194+
QgsRasterDataProvider* dp = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
195+
QVERIFY( dp );
196+
QCOMPARE( dp->xSize(), width );
197+
QCOMPARE( dp->ySize(), height );
198+
QCOMPARE( dp->extent(), extent );
199+
QCOMPARE( dp->bandCount(), 1 );
200+
QCOMPARE( dp->dataType( 1 ), Qgis::Byte );
201+
QVERIFY( dp->isEditable() );
202+
delete dp;
196203

197204
QgsRasterLayer* rlayer = new QgsRasterLayer( filename, "tmp", "gdal" );
198205
QVERIFY( rlayer->isValid() );
@@ -201,6 +208,7 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
201208
QCOMPARE( rlayer->extent(), extent );
202209
QCOMPARE( rlayer->bandCount(), 1 );
203210
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
211+
delete rlayer;
204212
}
205213

206214

0 commit comments

Comments
 (0)