25QString QgsConstantRasterAlgorithm::name()
const
27 return QStringLiteral(
"createconstantrasterlayer" );
30QString QgsConstantRasterAlgorithm::displayName()
const
32 return QObject::tr(
"Create constant raster layer" );
35QStringList QgsConstantRasterAlgorithm::tags()
const
37 return QObject::tr(
"raster,create,constant" ).split(
',' );
40QString QgsConstantRasterAlgorithm::group()
const
42 return QObject::tr(
"Raster creation" );
45QString QgsConstantRasterAlgorithm::groupId()
const
47 return QStringLiteral(
"rastercreation" );
50QString QgsConstantRasterAlgorithm::shortHelpString()
const
52 return QObject::tr(
"Generates raster layer for given extent and cell "
53 "size filled with the specified value.\n"
54 "Additionally an output data type can be specified. "
55 "The algorithm will abort if a value has been entered that "
56 "cannot be represented by the selected output raster data type." );
59QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance()
const
61 return new QgsConstantRasterAlgorithm();
64void QgsConstantRasterAlgorithm::initAlgorithm(
const QVariantMap & )
67 addParameter(
new QgsProcessingParameterCrs( QStringLiteral(
"TARGET_CRS" ), QObject::tr(
"Target CRS" ), QStringLiteral(
"ProjectCrs" ) ) );
73 QStringList rasterDataTypes;
74 rasterDataTypes << QStringLiteral(
"Byte" )
75 << QStringLiteral(
"Integer16" )
76 << QStringLiteral(
"Unsigned Integer16" )
77 << QStringLiteral(
"Integer32" )
78 << QStringLiteral(
"Unsigned Integer32" )
79 << QStringLiteral(
"Float32" )
80 << QStringLiteral(
"Float64" );
83 std::unique_ptr< QgsProcessingParameterDefinition > rasterTypeParameter = std::make_unique< QgsProcessingParameterEnum >( QStringLiteral(
"OUTPUT_TYPE" ), QObject::tr(
"Output raster data type" ), rasterDataTypes,
false, 5,
false );
85 addParameter( rasterTypeParameter.release() );
87 std::unique_ptr< QgsProcessingParameterString > createOptsParam = std::make_unique< QgsProcessingParameterString >( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
88 createOptsParam->setMetadata( QVariantMap( {{QStringLiteral(
"widget_wrapper" ), QVariantMap( {{QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) }} ) }} ) );
90 addParameter( createOptsParam.release() );
98 const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
99 const double pixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
100 const double value = parameterAsDouble( parameters, QStringLiteral(
"NUMBER" ), context );
101 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
103 if ( pixelSize <= 0 )
111 fractpart = abs( std::modf( value, &intpart ) );
118 if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
119 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" ) ) );
121 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" ) ) );
125 if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
126 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" ) ) );
128 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" ) ) );
132 if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
133 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" ) );
135 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" ) ) );
139 if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
140 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" ) ) );
142 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" ) ) );
146 if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
147 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" ) ) );
149 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" ) ) );
161 const QString createOptions = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context ).trimmed();
162 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
163 const QFileInfo fi( outputFile );
166 const int rows = std::max( std::ceil( extent.
height() / pixelSize ), 1.0 );
167 const int cols = std::max( std::ceil( extent.
width() / pixelSize ), 1.0 );
173 std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
174 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
175 if ( !createOptions.isEmpty() )
177 writer->setCreateOptions( createOptions.split(
'|' ) );
179 writer->setOutputFormat( outputFormat );
180 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent,
crs ) );
183 if ( !provider->isValid() )
196 std::vector<quint8> byteRow( cols );
197 std::fill( byteRow.begin(), byteRow.end(), value );
203 std::vector<qint16> int16Row( cols );
204 std::fill( int16Row.begin(), int16Row.end(), value );
210 std::vector<quint16> uInt16Row( cols );
211 std::fill( uInt16Row.begin(), uInt16Row.end(), value );
217 std::vector<qint32> int32Row( cols );
218 std::fill( int32Row.begin(), int32Row.end(), value );
224 std::vector<quint32> uInt32Row( cols );
225 std::fill( uInt32Row.begin(), uInt32Row.end(), value );
231 std::vector<float> float32Row( cols );
232 std::fill( float32Row.begin(), float32Row.end(), value );
238 std::vector<double> float64Row( cols );
239 std::fill( float64Row.begin(), float64Row.end(), value );
245 std::vector<float> float32Row( cols );
246 std::fill( float32Row.begin(), float32Row.end(), value );
252 const double step = rows > 0 ? 100.0 / rows : 1;
254 for (
int i = 0; i < rows ; i++ )
261 provider->writeBlock( &block, 1, 0, i );
266 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)
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Double
Double/float values.
This class 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 int typeSize(Qgis::DataType dataType)
Returns the size in bytes for the specified dataType.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double width() const
Returns the width of the rectangle.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
double height() const
Returns the height of the rectangle.
const QgsCoordinateReferenceSystem & crs