24using namespace Qt::StringLiterals;
28QString QgsFillNoDataAlgorithm::name()
const
30 return u
"fillnodata"_s;
33QString QgsFillNoDataAlgorithm::displayName()
const
35 return QObject::tr(
"Fill NoData cells" );
38QStringList QgsFillNoDataAlgorithm::tags()
const
40 return QObject::tr(
"data,cells,fill,set" ).split(
',' );
43QString QgsFillNoDataAlgorithm::group()
const
45 return QObject::tr(
"Raster tools" );
48QString QgsFillNoDataAlgorithm::groupId()
const
50 return u
"rastertools"_s;
53void QgsFillNoDataAlgorithm::initAlgorithm(
const QVariantMap & )
61 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATE_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
62 createOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
64 addParameter( createOptsParam.release() );
66 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u
"CREATION_OPTIONS"_s, QObject::tr(
"Creation options" ), QVariant(),
false,
true );
67 creationOptsParam->setMetadata( QVariantMap( { { u
"widget_wrapper"_s, QVariantMap( { { u
"widget_type"_s, u
"rasteroptions"_s } } ) } } ) );
69 addParameter( creationOptsParam.release() );
74QString QgsFillNoDataAlgorithm::shortHelpString()
const
77 "This algorithm resets the NoData values in the input raster "
78 "to a chosen value, resulting in a raster dataset with no NoData pixels. "
79 "This value can be set by the user using the Fill value parameter. "
80 "The algorithm respects the input raster data type (eg. a floating point fill value will be truncated "
81 "when applied to an integer raster)."
85QString QgsFillNoDataAlgorithm::shortDescription()
const
87 return QObject::tr(
"Generates a raster dataset with the NoData values in the input raster filled with a given value." );
90QgsFillNoDataAlgorithm *QgsFillNoDataAlgorithm::createInstance()
const
92 return new QgsFillNoDataAlgorithm();
98 mInputRaster = parameterAsRasterLayer( parameters, u
"INPUT"_s, context );
99 mFillValue = parameterAsDouble( parameters, u
"FILL_VALUE"_s, context );
104 mBand = parameterAsInt( parameters, u
"BAND"_s, context );
105 if ( mBand < 1 || mBand > mInputRaster->bandCount() )
106 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( mInputRaster->bandCount() ) );
108 mInterface.reset( mInputRaster->dataProvider()->clone() );
109 mInputNoDataValue = mInputRaster->dataProvider()->sourceNoDataValue( mBand );
110 mExtent = mInputRaster->extent();
111 mLayerWidth = mInputRaster->width();
112 mLayerHeight = mInputRaster->height();
113 mCrs = mInputRaster->crs();
114 mNbCellsXProvider = mInterface->xSize();
115 mNbCellsYProvider = mInterface->ySize();
121 QGS_MARK_ALGORITHM_SOURCE
124 if ( !mInputRaster->dataProvider()->sourceHasNoDataValue( mBand ) )
125 feedback->
reportError( QObject::tr(
"Input raster has no NoData values. There exist no NoData cells to fill." ),
false );
128 QString creationOptions = parameterAsString( parameters, u
"CREATION_OPTIONS"_s, context ).trimmed();
130 const QString optionsString = parameterAsString( parameters, u
"CREATE_OPTIONS"_s, context );
131 if ( !optionsString.isEmpty() )
132 creationOptions = optionsString;
134 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
135 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u
"OUTPUT"_s, context );
136 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
137 writer->setOutputProviderKey( u
"gdal"_s );
138 if ( !creationOptions.isEmpty() )
140 writer->setCreationOptions( creationOptions.split(
'|' ) );
142 writer->setOutputFormat( outputFormat );
143 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mInterface->dataType( mBand ), mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
146 if ( !provider->isValid() )
151 destinationRasterProvider = provider.get();
155 const double maxProgressDuringBlockWriting = hasReportsDuringClose ? 50.0 : 100.0;
158 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
163 std::unique_ptr<QgsRasterBlock> filledRasterBlock;
164 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, filledRasterBlock, iterLeft, iterTop ) )
167 feedback->
setProgress( maxProgressDuringBlockWriting * iter.progress( mBand ) );
172 if ( !filledRasterBlock->hasNoDataValue() )
174 if ( !destinationRasterProvider->
writeBlock( filledRasterBlock.get(), mBand, iterLeft, iterTop ) )
181 for (
int row = 0; row < iterRows; row++ )
185 for (
int column = 0; column < iterCols; column++ )
187 if ( filledRasterBlock->isNoData( row, column ) )
188 filledRasterBlock->setValue( row, column, mFillValue );
191 if ( !destinationRasterProvider->
writeBlock( filledRasterBlock.get(), mBand, iterLeft, iterTop ) )
198 if ( feedback && hasReportsDuringClose )
210 outputs.insert( u
"OUTPUT"_s, outputFile );
@ 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.
virtual QgsError error() const
Gets current status error.
QString summary() const
Short error description, usually the first error in chain, the real error.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
static std::unique_ptr< QgsFeedback > createScaledFeedback(QgsFeedback *parentFeedback, double startPercentage, double endPercentage)
Returns a feedback object whose [0, 100] progression range will be mapped to parentFeedback [startPer...
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 raster band 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 raster layer parameter for processing algorithms.
Base class for raster data providers.
bool writeBlock(QgsRasterBlock *block, int band, int xOffset=0, int yOffset=0)
Writes pixel data from a raster block into the provider data source.
virtual bool closeWithProgress(QgsFeedback *feedback)
Close the provider with feedback.
virtual bool hasReportsDuringClose() const
Returns whether closeWithProgress() will actually report closing progress.
virtual bool setEditable(bool enabled)
Turns on/off editing mode of the provider.
Iterator for sequentially processing raster cells.