Make the one band raster creation method return data provider

This commit is contained in:
Martin Dobias 2017-01-19 13:37:31 +08:00
parent 7b27079de9
commit ff8d912147
4 changed files with 27 additions and 18 deletions

View File

@ -30,14 +30,16 @@ class QgsRasterFileWriter
QgsRasterFileWriter( const QString& outputUrl ); QgsRasterFileWriter( const QString& outputUrl );
/** Create a raster file with one band without initializing the pixel data. /** 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. * @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 * @note added in QGIS 3.0
*/ */
bool createOneBandRaster( Qgis::DataType dataType, QgsRasterDataProvider* createOneBandRaster( Qgis::DataType dataType,
int width, int height, int width, int height,
const QgsRectangle& extent, const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs ); const QgsCoordinateReferenceSystem& crs ) /Factory/;
/** Write raster file /** Write raster file
@param pipe raster pipe @param pipe raster pipe

View File

@ -29,19 +29,16 @@
#include <QTextStream> #include <QTextStream>
#include <QMessageBox> #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 ) if ( mTiledMode )
return false; // does not make sense with tiled mode return nullptr; // does not make sense with tiled mode
double pixelSize; double pixelSize;
double geoTransform[6]; double geoTransform[6];
globalOutputParameters( extent, width, height, geoTransform, pixelSize ); globalOutputParameters( extent, width, height, geoTransform, pixelSize );
QgsRasterDataProvider* destProvider = initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() ); return initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() );
bool res = destProvider != nullptr;
delete destProvider;
return res;
} }
QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl ) QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl )

View File

@ -56,11 +56,13 @@ class CORE_EXPORT QgsRasterFileWriter
QgsRasterFileWriter( const QString& outputUrl ); QgsRasterFileWriter( const QString& outputUrl );
/** Create a raster file with one band without initializing the pixel data. /** 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. * @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 * @note added in QGIS 3.0
*/ */
bool createOneBandRaster( Qgis::DataType dataType, QgsRasterDataProvider* createOneBandRaster( Qgis::DataType dataType,
int width, int height, int width, int height,
const QgsRectangle& extent, const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs ); const QgsCoordinateReferenceSystem& crs );

View File

@ -191,8 +191,15 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
int width = 200, height = 100; int width = 200, height = 100;
QgsRasterFileWriter writer( filename ); QgsRasterFileWriter writer( filename );
bool res = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) ); QgsRasterDataProvider* dp = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( res ); 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" ); QgsRasterLayer* rlayer = new QgsRasterLayer( filename, "tmp", "gdal" );
QVERIFY( rlayer->isValid() ); QVERIFY( rlayer->isValid() );
@ -201,6 +208,7 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
QCOMPARE( rlayer->extent(), extent ); QCOMPARE( rlayer->extent(), extent );
QCOMPARE( rlayer->bandCount(), 1 ); QCOMPARE( rlayer->bandCount(), 1 );
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte ); QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
delete rlayer;
} }