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() );
110 QGS_MARK_ALGORITHM_SOURCE
113 const QgsRectangle extent = parameterAsExtent( parameters, u
"EXTENT"_s, context, crs );
114 const double pixelSize = parameterAsDouble( parameters, u
"PIXEL_SIZE"_s, context );
115 const double value = parameterAsDouble( parameters, u
"NUMBER"_s, context );
116 const int typeId = parameterAsInt( parameters, u
"OUTPUT_TYPE"_s, context );
118 if ( pixelSize <= 0 )
126 fractpart = abs( std::modf( value, &intpart ) );
133 if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
135 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 )
139 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 )
144 if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
146 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 )
150 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 )
155 if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
157 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" )
161 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 )
166 if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
168 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 )
172 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 )
177 if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
179 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 )
183 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 )
196 QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
198 const QString optionsString = parameterAsString( parameters, u
"CREATE_OPTIONS"_s, context );
199 if ( !optionsString.isEmpty() )
200 creationOptions = optionsString;
202 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
203 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
207 const int rows =
static_cast<int>( 0.5 + extent.
height() / pixelSize );
208 const int cols =
static_cast<int>( 0.5 + extent.
width() / pixelSize );
214 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
215 writer->setOutputProviderKey( u
"gdal"_s );
216 if ( !creationOptions.isEmpty() )
218 writer->setCreationOptions( creationOptions.split(
'|' ) );
220 writer->setOutputFormat( outputFormat );
221 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent, crs ) );
224 if ( !provider->isValid() )
235 const double step = rows > 0 ? 100.0 / rows : 1;
237 for (
int i = 0; i < rows; i++ )
244 if ( !provider->writeBlock( &block, 1, 0, i ) )
246 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
252 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.