27QString QgsConstantRasterAlgorithm::name()
const
29 return QStringLiteral(
"createconstantrasterlayer" );
32QString QgsConstantRasterAlgorithm::displayName()
const
34 return QObject::tr(
"Create constant raster layer" );
37QStringList QgsConstantRasterAlgorithm::tags()
const
39 return QObject::tr(
"raster,create,constant" ).split(
',' );
42QString QgsConstantRasterAlgorithm::group()
const
44 return QObject::tr(
"Raster creation" );
47QString QgsConstantRasterAlgorithm::groupId()
const
49 return QStringLiteral(
"rastercreation" );
52QString QgsConstantRasterAlgorithm::shortHelpString()
const
54 return QObject::tr(
"This algorithm generates a raster layer for a given extent and cell "
55 "size filled with a single constant value.\n"
56 "Additionally an output data type can be specified. "
57 "The algorithm will abort if a value has been entered that "
58 "cannot be represented by the selected output raster data type." );
61QString QgsConstantRasterAlgorithm::shortDescription()
const
63 return QObject::tr(
"Generates a raster layer for a given extent and cell size filled with a single constant value." );
66QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance()
const
68 return new QgsConstantRasterAlgorithm();
71void QgsConstantRasterAlgorithm::initAlgorithm(
const QVariantMap & )
74 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"TARGET_CRS" ), QObject::tr(
"Target CRS" ), QStringLiteral(
"ProjectCrs" ) ) );
78 QStringList rasterDataTypes;
79 rasterDataTypes << QStringLiteral(
"Byte" )
80 << QStringLiteral(
"Integer16" )
81 << QStringLiteral(
"Unsigned Integer16" )
82 << QStringLiteral(
"Integer32" )
83 << QStringLiteral(
"Unsigned Integer32" )
84 << QStringLiteral(
"Float32" )
85 << QStringLiteral(
"Float64" );
88 std::unique_ptr<QgsProcessingParameterDefinition> rasterTypeParameter = std::make_unique<QgsProcessingParameterEnum>( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
90 addParameter( rasterTypeParameter.release() );
94 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
95 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
97 addParameter( createOptsParam.release() );
99 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
100 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
102 addParameter( creationOptsParam.release() );
110 const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, crs );
111 const double pixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
112 const double value = parameterAsDouble( parameters, QStringLiteral(
"NUMBER" ), context );
113 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
115 if ( pixelSize <= 0 )
123 fractpart = abs( std::modf( value, &intpart ) );
130 if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
131 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( QLatin1String(
"Byte" ) ) );
133 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( QLatin1String(
"Byte" ) ) );
137 if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
138 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( QLatin1String(
"Integer16" ) ) );
140 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( QLatin1String(
"Integer16" ) ) );
144 if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
145 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" ) );
147 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( QLatin1String(
"Unsigned Integer16" ) ) );
151 if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
152 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( QLatin1String(
"Integer32" ) ) );
154 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( QLatin1String(
"Integer32" ) ) );
158 if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
159 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( QLatin1String(
"Unsigned Integer32" ) ) );
161 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( QLatin1String(
"Unsigned Integer32" ) ) );
173 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
175 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
176 if ( !optionsString.isEmpty() )
177 creationOptions = optionsString;
179 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
180 const QFileInfo fi( outputFile );
185 const int rows =
static_cast<int>( 0.5 + extent.
height() / pixelSize );
186 const int cols =
static_cast<int>( 0.5 + extent.
width() / pixelSize );
192 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
193 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
194 if ( !creationOptions.isEmpty() )
196 writer->setCreationOptions( creationOptions.split(
'|' ) );
198 writer->setOutputFormat( outputFormat );
199 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent, crs ) );
202 if ( !provider->isValid() )
213 const double step = rows > 0 ? 100.0 / rows : 1;
215 for (
int i = 0; i < rows; i++ )
222 if ( !provider->writeBlock( &block, 1, 0, i ) )
224 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
230 outputs.insert( QStringLiteral(
"OUTPUT" ), 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 ...
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
A rectangle specified with double values.