25QString QgsRescaleRasterAlgorithm::name()
const
27 return QStringLiteral(
"rescaleraster" );
30QString QgsRescaleRasterAlgorithm::displayName()
const
32 return QObject::tr(
"Rescale raster" );
35QStringList QgsRescaleRasterAlgorithm::tags()
const
37 return QObject::tr(
"raster,rescale,minimum,maximum,range" ).split(
',' );
40QString QgsRescaleRasterAlgorithm::group()
const
42 return QObject::tr(
"Raster analysis" );
45QString QgsRescaleRasterAlgorithm::groupId()
const
47 return QStringLiteral(
"rasteranalysis" );
50QString 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 the original NoData value, but there is "
57 "an option to override it." );
60QgsRescaleRasterAlgorithm *QgsRescaleRasterAlgorithm::createInstance()
const
62 return new QgsRescaleRasterAlgorithm();
65void QgsRescaleRasterAlgorithm::initAlgorithm(
const QVariantMap & )
68 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"BAND" ), QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
75 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
76 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
78 addParameter( createOptsParam.release() );
80 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
81 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
83 addParameter( creationOptsParam.release() );
92 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
96 mBand = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
97 if ( mBand < 1 || mBand > layer->
bandCount() )
98 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" )
102 mMinimum = parameterAsDouble( parameters, QStringLiteral(
"MINIMUM" ), context );
103 mMaximum = parameterAsDouble( parameters, QStringLiteral(
"MAXIMUM" ), context );
108 mLayerWidth = layer->
width();
109 mLayerHeight = layer->
height();
110 mExtent = layer->
extent();
111 if ( parameters.value( QStringLiteral(
"NODATA" ) ).isValid() )
113 mNoData = parameterAsDouble( parameters, QStringLiteral(
"NODATA" ), context );
120 if ( std::isfinite( mNoData ) )
123 if ( mNoData < std::numeric_limits<float>::lowest() )
124 mNoData = std::numeric_limits<float>::lowest();
125 else if ( mNoData > std::numeric_limits<float>::max() )
126 mNoData = std::numeric_limits<float>::max();
129 mXSize = mInterface->xSize();
130 mYSize = mInterface->ySize();
137 feedback->
pushInfo( QObject::tr(
"Calculating raster minimum and maximum values…" ) );
140 feedback->
pushInfo( QObject::tr(
"Rescaling values…" ) );
142 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
144 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
145 if ( !optionsString.isEmpty() )
146 creationOptions = optionsString;
148 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
149 const QFileInfo fi( outputFile );
151 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
152 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
153 if ( !creationOptions.isEmpty() )
155 writer->setCreationOptions( creationOptions.split(
'|' ) );
158 writer->setOutputFormat( outputFormat );
159 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster(
Qgis::DataType::Float32, mXSize, mYSize, mExtent, mCrs ) );
162 if ( !provider->isValid() )
171 const int numBlocksX =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / blockWidth ) );
172 const int numBlocksY =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / blockHeight ) );
173 const int numBlocks = numBlocksX * numBlocksY;
176 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
181 std::unique_ptr<QgsRasterBlock> inputBlock;
182 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, inputBlock, iterLeft, iterTop ) )
184 auto outputBlock = std::make_unique<QgsRasterBlock>( destProvider->
dataType( 1 ), iterCols, iterRows );
185 feedback->
setProgress( 100 * ( ( iterTop / blockHeight * numBlocksX ) + iterLeft / blockWidth ) / numBlocks );
187 for (
int row = 0; row < iterRows; row++ )
192 for (
int col = 0; col < iterCols; col++ )
194 bool isNoData =
false;
195 const double val = inputBlock->valueAndNoData( row, col, isNoData );
198 outputBlock->setValue( row, col, mNoData );
203 outputBlock->setValue( row, col, newValue );
207 if ( !destProvider->
writeBlock( outputBlock.get(), mBand, iterLeft, iterTop ) )
215 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
@ Float32
Thirty two bit floating point (float)
@ 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.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
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 pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
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.
The RasterBandStats struct is a container for statistics about a single raster band.
double minimumValue
The minimum cell value in the raster band.
double maximumValue
The maximum cell value in the raster band.
Base class for raster data providers.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
virtual bool setNoDataValue(int bandNo, double noDataValue)
Set no data value on created dataset.
virtual double sourceNoDataValue(int bandNo) const
Value representing no data value.
bool writeBlock(QgsRasterBlock *block, int band, int xOffset=0, int yOffset=0)
Writes pixel data from a raster block into the provider data source.
Qgis::DataType dataType(int bandNo) const override=0
Returns data type for the band specified by number.
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.
Represents a raster layer.
int height() const
Returns the height of the (unclipped) raster.
int bandCount() const
Returns the number of bands in this layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
int width() const
Returns the width of the (unclipped) raster.
A rectangle specified with double values.