Skip to content

Commit

Permalink
Convenience API call to create empty raster file
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Jan 19, 2017
1 parent 15cd833 commit 7b27079
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/core/raster/qgsrasterfilewriter.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/core/raster/qgsrasterfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
10 changes: 10 additions & 0 deletions src/core/raster/qgsrasterfilewriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions tests/src/core/testqgsrasterfilewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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>";
Expand Down

0 comments on commit 7b27079

Please sign in to comment.