25 QString QgsConstantRasterAlgorithm::name()
const
27 return QStringLiteral(
"createconstantrasterlayer" );
30 QString QgsConstantRasterAlgorithm::displayName()
const
32 return QObject::tr(
"Create constant raster layer" );
35 QStringList QgsConstantRasterAlgorithm::tags()
const
37 return QObject::tr(
"raster,create,constant" ).split(
',' );
40 QString QgsConstantRasterAlgorithm::group()
const
42 return QObject::tr(
"Raster creation" );
45 QString QgsConstantRasterAlgorithm::groupId()
const
47 return QStringLiteral(
"rastercreation" );
50 QString 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." );
59 QgsConstantRasterAlgorithm *QgsConstantRasterAlgorithm::createInstance()
const
61 return new QgsConstantRasterAlgorithm();
64 void 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() );
93 const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context,
crs );
94 const double pixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
95 const double value = parameterAsDouble( parameters, QStringLiteral(
"NUMBER" ), context );
96 const int typeId = parameterAsInt( parameters, QStringLiteral(
"OUTPUT_TYPE" ), context );
101 fractpart = abs( std::modf( value, &intpart ) );
108 if ( value < std::numeric_limits<quint8>::min() || value > std::numeric_limits<quint8>::max() )
109 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" ) ) );
111 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" ) ) );
115 if ( value < std::numeric_limits<qint16>::min() || value > std::numeric_limits<qint16>::max() )
116 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" ) ) );
118 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" ) ) );
122 if ( value < std::numeric_limits<quint16>::min() || value > std::numeric_limits<quint16>::max() )
123 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" ) );
125 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" ) ) );
129 if ( value < std::numeric_limits<qint32>::min() || value > std::numeric_limits<qint32>::max() )
130 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" ) ) );
132 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" ) ) );
136 if ( value < std::numeric_limits<quint32>::min() || value > std::numeric_limits<quint32>::max() )
137 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" ) ) );
139 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" ) ) );
150 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
151 const QFileInfo fi( outputFile );
154 const int rows = std::max( std::ceil( extent.
height() / pixelSize ), 1.0 );
155 const int cols = std::max( std::ceil( extent.
width() / pixelSize ), 1.0 );
161 std::unique_ptr< QgsRasterFileWriter > writer = std::make_unique< QgsRasterFileWriter >( outputFile );
162 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
163 writer->setOutputFormat( outputFormat );
164 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster( rasterDataType, cols, rows, rasterExtent,
crs ) );
167 if ( !provider->isValid() )
180 std::vector<quint8> byteRow( cols );
181 std::fill( byteRow.begin(), byteRow.end(), value );
187 std::vector<qint16> int16Row( cols );
188 std::fill( int16Row.begin(), int16Row.end(), value );
194 std::vector<quint16> uInt16Row( cols );
195 std::fill( uInt16Row.begin(), uInt16Row.end(), value );
201 std::vector<qint32> int32Row( cols );
202 std::fill( int32Row.begin(), int32Row.end(), value );
208 std::vector<quint32> uInt32Row( cols );
209 std::fill( uInt32Row.begin(), uInt32Row.end(), value );
215 std::vector<float> float32Row( cols );
216 std::fill( float32Row.begin(), float32Row.end(), value );
222 std::vector<double> float64Row( cols );
223 std::fill( float64Row.begin(), float64Row.end(), value );
229 std::vector<float> float32Row( cols );
230 std::fill( float32Row.begin(), float32Row.end(), value );
236 const double step = rows > 0 ? 100.0 / rows : 1;
238 for (
int i = 0; i < rows ; i++ )
245 provider->writeBlock( &block, 1, 0, i );
250 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );