Skip to content

Commit 7b27079

Browse files
committed
Convenience API call to create empty raster file
1 parent 15cd833 commit 7b27079

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

‎python/core/raster/qgsrasterfilewriter.sip‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class QgsRasterFileWriter
2929

3030
QgsRasterFileWriter( const QString& outputUrl );
3131

32+
/** Create a raster file with one band without initializing the pixel data.
33+
* @note Does not work with tiled mode enabled.
34+
* @returns true when the raster has been created successfully
35+
* @note added in QGIS 3.0
36+
*/
37+
bool createOneBandRaster( Qgis::DataType dataType,
38+
int width, int height,
39+
const QgsRectangle& extent,
40+
const QgsCoordinateReferenceSystem& crs );
41+
3242
/** Write raster file
3343
@param pipe raster pipe
3444
@param nCols number of output columns

‎src/core/raster/qgsrasterfilewriter.cpp‎

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

32+
bool QgsRasterFileWriter::createOneBandRaster( Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs )
33+
{
34+
if ( mTiledMode )
35+
return false; // does not make sense with tiled mode
36+
37+
double pixelSize;
38+
double geoTransform[6];
39+
globalOutputParameters( extent, width, height, geoTransform, pixelSize );
40+
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;
45+
}
46+
3247
QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl )
3348
: mMode( Raw )
3449
, mOutputUrl( outputUrl )

‎src/core/raster/qgsrasterfilewriter.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ class CORE_EXPORT QgsRasterFileWriter
5555

5656
QgsRasterFileWriter( const QString& outputUrl );
5757

58+
/** Create a raster file with one band without initializing the pixel data.
59+
* @note Does not work with tiled mode enabled.
60+
* @returns true when the raster has been created successfully
61+
* @note added in QGIS 3.0
62+
*/
63+
bool createOneBandRaster( Qgis::DataType dataType,
64+
int width, int height,
65+
const QgsRectangle& extent,
66+
const QgsCoordinateReferenceSystem& crs );
67+
5868
/** Write raster file
5969
@param pipe raster pipe
6070
@param nCols number of output columns

‎tests/src/core/testqgsrasterfilewriter.cpp‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TestQgsRasterFileWriter: public QObject
4747
void cleanup() {} // will be called after every testfunction.
4848

4949
void writeTest();
50+
void testCreateOneBandRaster();
5051
private:
5152
bool writeTest( const QString& rasterName );
5253
void log( const QString& msg );
@@ -178,6 +179,31 @@ bool TestQgsRasterFileWriter::writeTest( const QString& theRasterName )
178179
return ok;
179180
}
180181

182+
void TestQgsRasterFileWriter::testCreateOneBandRaster()
183+
{
184+
// generate unique filename (need to open the file first to generate it)
185+
QTemporaryFile tmpFile;
186+
tmpFile.open();
187+
tmpFile.close();
188+
QString filename = tmpFile.fileName();
189+
190+
QgsRectangle extent( 106.7, -6.2, 106.9, -6.1 );
191+
int width = 200, height = 100;
192+
193+
QgsRasterFileWriter writer( filename );
194+
bool res = writer.createOneBandRaster( Qgis::Byte, width, height, extent, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
195+
QVERIFY( res );
196+
197+
QgsRasterLayer* rlayer = new QgsRasterLayer( filename, "tmp", "gdal" );
198+
QVERIFY( rlayer->isValid() );
199+
QCOMPARE( rlayer->width(), width );
200+
QCOMPARE( rlayer->height(), height );
201+
QCOMPARE( rlayer->extent(), extent );
202+
QCOMPARE( rlayer->bandCount(), 1 );
203+
QCOMPARE( rlayer->dataProvider()->dataType( 1 ), Qgis::Byte );
204+
}
205+
206+
181207
void TestQgsRasterFileWriter::log( const QString& msg )
182208
{
183209
mReport += msg + "<br>";

0 commit comments

Comments
 (0)