mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Convenience API call to create empty raster file
This commit is contained in:
parent
15cd8331ac
commit
7b27079de9
@ -29,6 +29,16 @@ class QgsRasterFileWriter
|
|||||||
|
|
||||||
QgsRasterFileWriter( const QString& outputUrl );
|
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
|
/** Write raster file
|
||||||
@param pipe raster pipe
|
@param pipe raster pipe
|
||||||
@param nCols number of output columns
|
@param nCols number of output columns
|
||||||
|
@ -29,6 +29,21 @@
|
|||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QMessageBox>
|
#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 )
|
QgsRasterFileWriter::QgsRasterFileWriter( const QString& outputUrl )
|
||||||
: mMode( Raw )
|
: mMode( Raw )
|
||||||
, mOutputUrl( outputUrl )
|
, mOutputUrl( outputUrl )
|
||||||
|
@ -55,6 +55,16 @@ class CORE_EXPORT QgsRasterFileWriter
|
|||||||
|
|
||||||
QgsRasterFileWriter( const QString& outputUrl );
|
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
|
/** Write raster file
|
||||||
@param pipe raster pipe
|
@param pipe raster pipe
|
||||||
@param nCols number of output columns
|
@param nCols number of output columns
|
||||||
|
@ -47,6 +47,7 @@ class TestQgsRasterFileWriter: public QObject
|
|||||||
void cleanup() {} // will be called after every testfunction.
|
void cleanup() {} // will be called after every testfunction.
|
||||||
|
|
||||||
void writeTest();
|
void writeTest();
|
||||||
|
void testCreateOneBandRaster();
|
||||||
private:
|
private:
|
||||||
bool writeTest( const QString& rasterName );
|
bool writeTest( const QString& rasterName );
|
||||||
void log( const QString& msg );
|
void log( const QString& msg );
|
||||||
@ -178,6 +179,31 @@ bool TestQgsRasterFileWriter::writeTest( const QString& theRasterName )
|
|||||||
return ok;
|
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 )
|
void TestQgsRasterFileWriter::log( const QString& msg )
|
||||||
{
|
{
|
||||||
mReport += msg + "<br>";
|
mReport += msg + "<br>";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user