23 QString QgsConstantRasterAlgorithm::name()
const 25 return QStringLiteral(
"createconstantrasterlayer" );
28 QString QgsConstantRasterAlgorithm::displayName()
const 30 return QObject::tr(
"Create constant raster layer" );
33 QStringList QgsConstantRasterAlgorithm::tags()
const 35 return QObject::tr(
"raster,create,constant" ).split(
',' );
38 QString QgsConstantRasterAlgorithm::group()
const 40 return QObject::tr(
"Raster tools" );
43 QString QgsConstantRasterAlgorithm::groupId()
const 45 return QStringLiteral(
"rastertools" );
48 QString QgsConstantRasterAlgorithm::shortHelpString()
const 50 return QObject::tr(
"Generates raster layer for given extent and cell " 51 "size filled with the specified value." );
54 QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance()
const 56 return new QgsConstantRasterAlgorithm();
59 void QgsConstantRasterAlgorithm::initAlgorithm(
const QVariantMap & )
62 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"TARGET_CRS" ), QObject::tr(
"Target CRS" ), QStringLiteral(
"ProjectCrs" ) ) );
74 QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, crs );
75 double pixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
76 double value = parameterAsDouble( parameters, QStringLiteral(
"NUMBER" ), context );
78 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
79 QFileInfo fi( outputFile );
82 int rows = std::max( std::ceil( extent.
height() / pixelSize ), 1.0 );
83 int cols = std::max( std::ceil( extent.
width() / pixelSize ), 1.0 );
89 std::unique_ptr< QgsRasterFileWriter > writer = qgis::make_unique< QgsRasterFileWriter >( outputFile );
90 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
91 writer->setOutputFormat( outputFormat );
92 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster(
Qgis::Float32, cols, rows, rasterExtent, crs ) );
95 if ( !provider->isValid() )
98 provider->setNoDataValue( 1, -9999 );
100 std::vector<float> line( cols );
101 std::fill( line.begin(), line.end(), value );
105 double step = rows > 0 ? 100.0 / rows : 1;
107 for (
int i = 0; i < rows ; i++ )
114 provider->writeBlock( &block, 1, 0, i );
119 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
A rectangle specified with double values.
Base class for providing feedback from a processing algorithm.
void setProgress(double progress)
Sets the current progress for the feedback object.
Thirty two bit floating point (float)
const QgsCoordinateReferenceSystem & crs
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
double width() const
Returns the width of the rectangle.
Custom exception class for processing related exceptions.
static int typeSize(int dataType)
A coordinate reference system parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
bool isCanceled() const
Tells whether the operation has been canceled already.
This class represents a coordinate reference system (CRS).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Contains information about the context in which a processing algorithm is executed.
double height() const
Returns the height of the rectangle.