Skip to content
Permalink
Browse files

Convenience API call to create empty raster file

  • Loading branch information
wonder-sk committed Jan 19, 2017
1 parent 15cd833 commit 7b27079de98466c9b07520f29c129f1599385c95
@@ -29,6 +29,16 @@ class QgsRasterFileWriter

QgsRasterFileWriter( const QString& outputUrl );

/** Create a raster file with one band without initializing the pixel data.
* @note Does not work with tiled mode enabled.
* @returns true when the raster has been created successfully
* @note added in QGIS 3.0
*/
bool createOneBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs );

/** Write raster file
@param pipe raster pipe
@param nCols number of output columns
@@ -29,6 +29,21 @@
#include <QTextStream>
#include <QMessageBox>

bool 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

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;
}

QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl )
: mMode( Raw )
, mOutputUrl( outputUrl )
@@ -55,6 +55,16 @@ class CORE_EXPORT QgsRasterFileWriter

QgsRasterFileWriter( const QString& outputUrl );

/** Create a raster file with one band without initializing the pixel data.
* @note Does not work with tiled mode enabled.
* @returns true when the raster has been created successfully
* @note added in QGIS 3.0
*/
bool createOneBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle& extent,
const QgsCoordinateReferenceSystem& crs );

/** Write raster file
@param pipe raster pipe
@param nCols number of output columns
@@ -47,6 +47,7 @@ class TestQgsRasterFileWriter: public QObject
void cleanup() {} // will be called after every testfunction.

void writeTest();
void testCreateOneBandRaster();
private:
bool writeTest( const QString& rasterName );
void log( const QString& msg );
@@ -178,6 +179,31 @@ bool TestQgsRasterFileWriter::writeTest( const QString& theRasterName )
return ok;
}

void TestQgsRasterFileWriter::testCreateOneBandRaster()
{
// generate unique filename (need to open the file first to generate it)
QTemporaryFile tmpFile;
tmpFile.open();
tmpFile.close();
QString filename = tmpFile.fileName();

QgsRectangle extent( 106.7, -6.2, 106.9, -6.1 );
int width = 200, height = 100;

QgsRasterFileWriter writer( filename );
bool res = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( res );

QgsRasterLayer* rlayer = new QgsRasterLayer( filename, "tmp", "gdal" );
QVERIFY( rlayer->isValid() );
QCOMPARE( rlayer->width(), width );
QCOMPARE( rlayer->height(), height );
QCOMPARE( rlayer->extent(), extent );
QCOMPARE( rlayer->bandCount(), 1 );
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
}


void TestQgsRasterFileWriter::log( const QString& msg )
{
mReport += msg + "<br>";

0 comments on commit 7b27079

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