23QString QgsFillNoDataAlgorithm::name()
const
25 return QStringLiteral(
"fillnodata" );
28QString QgsFillNoDataAlgorithm::displayName()
const
30 return QObject::tr(
"Fill NoData cells" );
33QStringList QgsFillNoDataAlgorithm::tags()
const
35 return QObject::tr(
"data,cells,fill,set" ).split(
',' );
38QString QgsFillNoDataAlgorithm::group()
const
40 return QObject::tr(
"Raster tools" );
43QString QgsFillNoDataAlgorithm::groupId()
const
45 return QStringLiteral(
"rastertools" );
48void QgsFillNoDataAlgorithm::initAlgorithm(
const QVariantMap & )
51 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"BAND" ), QObject::tr(
"Band Number" ), 1, QStringLiteral(
"INPUT" ) ) );
56 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
57 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
59 addParameter( createOptsParam.release() );
61 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
62 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
64 addParameter( creationOptsParam.release() );
69QString QgsFillNoDataAlgorithm::shortHelpString()
const
71 return QObject::tr(
"This algorithm resets the NoData values in the input raster "
72 "to a chosen value, resulting in a raster dataset with no NoData pixels. "
73 "This value can be set by the user using the Fill value parameter. "
74 "The algorithm respects the input raster data type (eg. a floating point fill value will be truncated "
75 "when applied to an integer raster)." );
78QString QgsFillNoDataAlgorithm::shortDescription()
const
80 return QObject::tr(
"Generates a raster dataset with the NoData values in the input raster filled with a given value." );
83QgsFillNoDataAlgorithm *QgsFillNoDataAlgorithm::createInstance()
const
85 return new QgsFillNoDataAlgorithm();
91 mInputRaster = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
92 mFillValue = parameterAsDouble( parameters, QStringLiteral(
"FILL_VALUE" ), context );
97 mBand = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
98 if ( mBand < 1 || mBand > mInputRaster->bandCount() )
99 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( mInputRaster->bandCount() ) );
101 mInterface.reset( mInputRaster->dataProvider()->clone() );
102 mInputNoDataValue = mInputRaster->dataProvider()->sourceNoDataValue( mBand );
103 mExtent = mInputRaster->extent();
104 mLayerWidth = mInputRaster->width();
105 mLayerHeight = mInputRaster->height();
106 mCrs = mInputRaster->crs();
107 mNbCellsXProvider = mInterface->xSize();
108 mNbCellsYProvider = mInterface->ySize();
115 if ( !mInputRaster->dataProvider()->sourceHasNoDataValue( mBand ) )
116 feedback->
reportError( QObject::tr(
"Input raster has no NoData values. There exist no NoData cells to fill." ),
false );
119 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
121 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
122 if ( !optionsString.isEmpty() )
123 creationOptions = optionsString;
125 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
126 const QFileInfo fi( outputFile );
128 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
129 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
130 if ( !creationOptions.isEmpty() )
132 writer->setCreationOptions( creationOptions.split(
'|' ) );
134 writer->setOutputFormat( outputFormat );
135 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mInterface->dataType( mBand ), mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
138 if ( !provider->isValid() )
143 destinationRasterProvider = provider.get();
148 const int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
149 const int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
150 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
153 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
158 std::unique_ptr<QgsRasterBlock> filledRasterBlock;
159 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, filledRasterBlock, iterLeft, iterTop ) )
162 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
167 if ( !filledRasterBlock->hasNoDataValue() )
169 if ( !destinationRasterProvider->
writeBlock( filledRasterBlock.get(), mBand, iterLeft, iterTop ) )
176 for (
int row = 0; row < iterRows; row++ )
180 for (
int column = 0; column < iterCols; column++ )
182 if ( filledRasterBlock->isNoData( row, column ) )
183 filledRasterBlock->setValue( row, column, mFillValue );
186 if ( !destinationRasterProvider->
writeBlock( filledRasterBlock.get(), mBand, iterLeft, iterTop ) )
194 outputs.insert( QStringLiteral(
"OUTPUT" ), 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.
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 setEditable(bool enabled)
Turns on/off editing mode of the provider.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Iterator for sequentially processing raster cells.
static const int DEFAULT_MAXIMUM_TILE_WIDTH
Default maximum tile width.
static const int DEFAULT_MAXIMUM_TILE_HEIGHT
Default maximum tile height.