Skip to content

Commit

Permalink
Merge pull request #4593 from alexbruy/create-raster-api
Browse files Browse the repository at this point in the history
[API] convenience API call to create empty raster file with given number of bands
  • Loading branch information
alexbruy committed May 30, 2017
2 parents b2ba450 + ca7cd63 commit 9e11082
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
17 changes: 17 additions & 0 deletions python/core/raster/qgsrasterfilewriter.sip
Expand Up @@ -53,6 +53,23 @@ class QgsRasterFileWriter
:rtype: QgsRasterDataProvider
%End

QgsRasterDataProvider *createMultiBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle &extent,
const QgsCoordinateReferenceSystem &crs,
int nBands ) /Factory/;
%Docstring
Create a raster file with given number of bands 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.
:return: Instance of data provider in editing mode (on success) or None on error.
.. versionadded:: 3.0
:rtype: QgsRasterDataProvider
%End

WriterError writeRaster( const QgsRasterPipe *pipe, int nCols, int nRows, const QgsRectangle &outputExtent,
const QgsCoordinateReferenceSystem &crs, QgsRasterBlockFeedback *feedback = 0 );
%Docstring
Expand Down
12 changes: 12 additions & 0 deletions src/core/raster/qgsrasterfilewriter.cpp
Expand Up @@ -41,6 +41,18 @@ QgsRasterDataProvider *QgsRasterFileWriter::createOneBandRaster( Qgis::DataType
return initOutput( width, height, crs, geoTransform, 1, dataType, QList<bool>(), QList<double>() );
}

QgsRasterDataProvider *QgsRasterFileWriter::createMultiBandRaster( Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs, int nBands )
{
if ( mTiledMode )
return nullptr; // does not make sense with tiled mode

double pixelSize;
double geoTransform[6];
globalOutputParameters( extent, width, height, geoTransform, pixelSize );

return initOutput( width, height, crs, geoTransform, nBands, dataType, QList<bool>(), QList<double>() );
}

QgsRasterFileWriter::QgsRasterFileWriter( const QString &outputUrl )
: mMode( Raw )
, mOutputUrl( outputUrl )
Expand Down
13 changes: 13 additions & 0 deletions src/core/raster/qgsrasterfilewriter.h
Expand Up @@ -67,6 +67,19 @@ class CORE_EXPORT QgsRasterFileWriter
const QgsRectangle &extent,
const QgsCoordinateReferenceSystem &crs ) SIP_FACTORY;

/** Create a raster file with given number of bands 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 Instance of data provider in editing mode (on success) or nullptr on error.
* \since QGIS 3.0
*/
QgsRasterDataProvider *createMultiBandRaster( Qgis::DataType dataType,
int width, int height,
const QgsRectangle &extent,
const QgsCoordinateReferenceSystem &crs,
int nBands ) SIP_FACTORY;

/** Write raster file
\param pipe raster pipe
\param nCols number of output columns
Expand Down
58 changes: 58 additions & 0 deletions tests/src/core/testqgsrasterfilewriter.cpp
Expand Up @@ -48,6 +48,7 @@ class TestQgsRasterFileWriter: public QObject

void writeTest();
void testCreateOneBandRaster();
void testCreateMultiBandRaster();
private:
bool writeTest( const QString &rasterName );
void log( const QString &msg );
Expand Down Expand Up @@ -211,6 +212,63 @@ void TestQgsRasterFileWriter::testCreateOneBandRaster()
delete rlayer;
}

void TestQgsRasterFileWriter::testCreateMultiBandRaster()
{
// 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, nBands = 1;

QgsRasterFileWriter writer( filename );
QgsRasterDataProvider *dp = writer.createMultiBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ), nBands );
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() );
QCOMPARE( rlayer->width(), width );
QCOMPARE( rlayer->height(), height );
QCOMPARE( rlayer->extent(), extent );
QCOMPARE( rlayer->bandCount(), 1 );
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
delete rlayer;

nBands = 3;
dp = writer.createMultiBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ), nBands );
QVERIFY( dp );
QCOMPARE( dp->xSize(), width );
QCOMPARE( dp->ySize(), height );
QCOMPARE( dp->extent(), extent );
QCOMPARE( dp->bandCount(), nBands );
for ( int i = 1; i <= nBands; i++ )
{
QCOMPARE( dp->dataType( i ), Qgis::Byte );
}
QVERIFY( dp->isEditable() );
delete dp;

rlayer = new QgsRasterLayer( filename, "tmp", "gdal" );
QVERIFY( rlayer->isValid() );
QCOMPARE( rlayer->width(), width );
QCOMPARE( rlayer->height(), height );
QCOMPARE( rlayer->extent(), extent );
QCOMPARE( rlayer->bandCount(), nBands );
for ( int i = 1; i <= nBands; i++ )
{
QCOMPARE( rlayer->dataProvider()->dataType( i ), Qgis::Byte );
}
delete rlayer;
}

void TestQgsRasterFileWriter::log( const QString &msg )
{
Expand Down

0 comments on commit 9e11082

Please sign in to comment.