27using namespace Qt::StringLiterals;
31QString QgsConstantRasterAlgorithm::name()
const
33 return u
"createconstantrasterlayer"_s;
36QString QgsConstantRasterAlgorithm::displayName()
const
38 return QObject::tr(
"Create constant raster layer" );
41QStringList QgsConstantRasterAlgorithm::tags()
const
43 return QObject::tr(
"raster,create,constant" ).split(
',' );
46QString QgsConstantRasterAlgorithm::group()
const
48 return QObject::tr(
"Raster creation" );
51QString QgsConstantRasterAlgorithm::groupId()
const
53 return u
"rastercreation"_s;
56QString QgsConstantRasterAlgorithm::shortHelpString()
const
58 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell "
59 "size filled with a single constant value.\n"
60 "Additionally an output data type can be specified. "
61 "The algorithm will abort if a value has been entered that "
62 "cannot be represented by the selected output raster data type." );
65QString QgsConstantRasterAlgorithm::shortDescription()
const
67 return QObject::tr(
"Generates a raster layer for a given extent and cell size filled with a single constant value." );
70QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance()
const
72 return new QgsConstantRasterAlgorithm();
75void QgsConstantRasterAlgorithm::initAlgorithm(
const QVariantMap & )
82 QStringList rasterDataTypes;
83 rasterDataTypes << u
"Byte"_s
85 << u
"Unsigned Integer16"_s
87 << u
"Unsigned Integer32"_s
92 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
94 addParameter( rasterTypeParameter.release() );
98 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATE_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
99 createOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
101 addParameter( createOptsParam.release() );
103 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
104 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
106 addParameter( creationOptsParam.release() );
114 const QgsRectangle extent = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
115 const double pixelSize = parameterAsDouble( parameters, u
"PIXEL_SIZE"_s, context );
116 const double value = parameterAsDouble( parameters, u
"NUMBER"_s, context );
117 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
119 if ( pixelSize <= 0 )
127 fractpart = abs( std::modf( value, &intpart ) );
134 if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
135 throw QgsProcessingException( QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2" ).arg( std::numeric_limits<quint8>::min() ).arg( std::numeric_limits<quint8>::max() ).arg(
"Byte"_L1 ) );
137 feedback->
reportError( QObject::tr(
"The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg(
"Byte"_L1 ) );
141 if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
142 throw QgsProcessingException( QObject::tr(
"Raster datasets of type %3 only accept values between %1 and %2" ).arg( std::numeric_limits<qint16>::min() ).arg( std::numeric_limits<qint16>::max() ).arg(
"Integer16"_L1 ) );
144 feedback->
reportError( QObject::tr(
"The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg(
"Integer16"_L1 ) );
148 if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
149 throw QgsProcessingException( QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2" ).arg( std::numeric_limits<quint16>::min() ).arg( std::numeric_limits<quint16>::max() ).arg(
"Unsigned Integer16" ) );
151 feedback->
reportError( QObject::tr(
"The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg(
"Unsigned Integer16"_L1 ) );
155 if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
156 throw QgsProcessingException( QObject::tr(
"Raster datasets of type %3 only accept values between %1 and %2" ).arg( std::numeric_limits<qint32>::min() ).arg( std::numeric_limits<qint32>::max() ).arg(
"Integer32"_L1 ) );
158 feedback->
reportError( QObject::tr(
"The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg(
"Integer32"_L1 ) );
162 if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
163 throw QgsProcessingException( QObject::tr(
"Raster datasets of type %3 only accept positive values between %1 and %2" ).arg( std::numeric_limits<quint32>::min() ).arg( std::numeric_limits<quint32>::max() ).arg(
"Unsigned Integer32"_L1 ) );
165 feedback->
reportError( QObject::tr(
"The entered constant value has decimals but will be written to a raster dataset of type %1. The decimals of the constant value will be omitted." ).arg(
"Unsigned Integer32"_L1 ) );
177 QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
179 const QString optionsString = parameterAsString( parameters, u
"CREATE_OPTIONS"_s, context );
180 if ( !optionsString.isEmpty() )
181 creationOptions = optionsString;
183 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
184 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
188 const int rows =
static_cast<int>( 0.5 + extent.
height() / pixelSize );
189 const int cols =
static_cast<int>( 0.5 + extent.
width() / pixelSize );
195 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
196 writer->setOutputProviderKey( u
"gdal"_s );
197 if ( !creationOptions.isEmpty() )
199 writer->setCreationOptions( creationOptions.split(
'|' ) );
201 writer->setOutputFormat( outputFormat );
202 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent, crs ) );
205 if ( !provider->isValid() )
216 const double step = rows > 0 ? 100.0 / rows : 1;
218 for (
int i = 0; i < rows; i++ )
225 if ( !provider->writeBlock( &block, 1, 0, i ) )
227 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
233 outputs.insert( u
"OUTPUT"_s, outputFile );
DataType
Raster data types.
@ Float32
Thirty two bit floating point (float).
@ Int16
Sixteen bit signed integer (qint16).
@ UInt16
Sixteen bit unsigned integer (quint16).
@ Byte
Eight bit unsigned integer (quint8).
@ Int32
Thirty two bit signed integer (qint32).
@ Float64
Sixty four bit floating point (double).
@ UInt32
Thirty two bit unsigned integer (quint32).
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
Represents a coordinate reference system (CRS).
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A coordinate reference system parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
A rectangle specified with double values.