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
59 "This algorithm generates a raster layer for a given extent and cell "
60 "size filled with a single constant value.\n"
61 "Additionally an output data type can be specified. "
62 "The algorithm will abort if a value has been entered that "
63 "cannot be represented by the selected output raster data type."
67QString QgsConstantRasterAlgorithm::shortDescription()
const
69 return QObject::tr(
"Generates a raster layer for a given extent and cell size filled with a single constant value." );
72QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance()
const
74 return new QgsConstantRasterAlgorithm();
77void QgsConstantRasterAlgorithm::initAlgorithm(
const QVariantMap & )
84 QStringList rasterDataTypes;
85 rasterDataTypes << u
"Byte"_s << u
"Integer16"_s << u
"Unsigned Integer16"_s << u
"Integer32"_s << u
"Unsigned Integer32"_s << u
"Float32"_s << u
"Float64"_s;
88 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter
89 = std::make_unique<QgsProcessingParameterEnum>( u
"OUTPUT_TYPE"_s, QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
91 addParameter( rasterTypeParameter.release() );
95 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATE_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
96 createOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
98 addParameter( createOptsParam.release() );
100 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
101 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
103 addParameter( creationOptsParam.release() );
111 const QgsRectangle extent = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
112 const double pixelSize = parameterAsDouble( parameters, u
"PIXEL_SIZE"_s, context );
113 const double value = parameterAsDouble( parameters, u
"NUMBER"_s, context );
114 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
116 if ( pixelSize <= 0 )
124 fractpart = abs( std::modf( value, &intpart ) );
131 if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
133 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 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 )
142 if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
144 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 )
148 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 )
153 if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
155 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" )
159 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 )
164 if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
166 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 )
170 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 )
175 if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
177 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 )
181 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 )
194 QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
196 const QString optionsString = parameterAsString( parameters, u
"CREATE_OPTIONS"_s, context );
197 if ( !optionsString.isEmpty() )
198 creationOptions = optionsString;
200 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
201 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
205 const int rows =
static_cast<int>( 0.5 + extent.
height() / pixelSize );
206 const int cols =
static_cast<int>( 0.5 + extent.
width() / pixelSize );
212 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
213 writer->setOutputProviderKey( u
"gdal"_s );
214 if ( !creationOptions.isEmpty() )
216 writer->setCreationOptions( creationOptions.split(
'|' ) );
218 writer->setOutputFormat( outputFormat );
219 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent, crs ) );
222 if ( !provider->isValid() )
233 const double step = rows > 0 ? 100.0 / rows : 1;
235 for (
int i = 0; i < rows; i++ )
242 if ( !provider->writeBlock( &block, 1, 0, i ) )
244 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
250 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.