25 QString QgsRescaleRasterAlgorithm::name()
const
27 return QStringLiteral(
"rescaleraster" );
30 QString QgsRescaleRasterAlgorithm::displayName()
const
32 return QObject::tr(
"Rescale raster" );
35 QStringList QgsRescaleRasterAlgorithm::tags()
const
37 return QObject::tr(
"raster,rescale,minimum,maximum,range" ).split(
',' );
40 QString QgsRescaleRasterAlgorithm::group()
const
42 return QObject::tr(
"Raster analysis" );
45 QString QgsRescaleRasterAlgorithm::groupId()
const
47 return QStringLiteral(
"rasteranalysis" );
50 QString QgsRescaleRasterAlgorithm::shortHelpString()
const
52 return QObject::tr(
"Rescales raster layer to a new value range, while preserving the shape "
53 "(distribution) of the raster's histogram (pixel values). Input values "
54 "are mapped using a linear interpolation from the source raster's minimum "
55 "and maximum pixel values to the destination minimum and maximum pixel range.\n\n"
56 "By default the algorithm preserves original the NODATA value, but there is "
57 "an option to override it." );
60 QgsRescaleRasterAlgorithm *QgsRescaleRasterAlgorithm::createInstance()
const
62 return new QgsRescaleRasterAlgorithm();
65 void QgsRescaleRasterAlgorithm::initAlgorithm(
const QVariantMap & )
68 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"BAND" ), QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
79 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
83 mBand = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
84 if ( mBand < 1 || mBand > layer->
bandCount() )
85 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" )
89 mMinimum = parameterAsDouble( parameters, QStringLiteral(
"MINIMUM" ), context );
90 mMaximum = parameterAsDouble( parameters, QStringLiteral(
"MAXIMUM" ), context );
95 mLayerWidth = layer->
width();
96 mLayerHeight = layer->
height();
98 if ( parameters.value( QStringLiteral(
"NODATA" ) ).isValid() )
100 mNoData = parameterAsDouble( parameters, QStringLiteral(
"NODATA" ), context );
106 mXSize = mInterface->xSize();
107 mYSize = mInterface->ySize();
114 feedback->
pushInfo( QObject::tr(
"Calculating raster minimum and maximum values…" ) );
117 feedback->
pushInfo( QObject::tr(
"Rescaling values…" ) );
118 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
119 QFileInfo fi( outputFile );
121 std::unique_ptr< QgsRasterFileWriter > writer = qgis::make_unique< QgsRasterFileWriter >( outputFile );
122 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
123 writer->setOutputFormat( outputFormat );
124 std::unique_ptr<QgsRasterDataProvider > provider( writer->createOneBandRaster(
Qgis::Float32, mXSize, mYSize, mExtent, mCrs ) );
127 if ( !provider->isValid() )
136 int numBlocksX =
static_cast< int >( std::ceil( 1.0 * mLayerWidth / blockWidth ) );
137 int numBlocksY =
static_cast< int >( std::ceil( 1.0 * mLayerHeight / blockHeight ) );
138 int numBlocks = numBlocksX * numBlocksY;
141 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
146 std::unique_ptr< QgsRasterBlock > block;
147 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, block, iterLeft, iterTop ) )
149 feedback->
setProgress( 100 * ( ( iterTop / blockHeight * numBlocksX ) + iterLeft / blockWidth ) / numBlocks );
151 for (
int row = 0; row < iterRows; row++ )
156 for (
int col = 0; col < iterCols; col++ )
158 bool isNoData =
false;
159 double val = block->valueAndNoData( row, col, isNoData );
162 block->setValue( row, col, mNoData );
167 block->setValue( row, col, newValue );
171 destProvider->
writeBlock( block.get(), mBand, iterLeft, iterTop );
176 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );