QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
Loading...
Searching...
No Matches
qgsalgorithmrescaleraster.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrescaleraster.cpp
3 ---------------------
4 begin : July 2020
5 copyright : (C) 2020 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include <limits>
21#include <math.h>
22
23#include "qgsrasterfilewriter.h"
24
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30
31QString QgsRescaleRasterAlgorithm::name() const
32{
33 return u"rescaleraster"_s;
34}
35
36QString QgsRescaleRasterAlgorithm::displayName() const
37{
38 return QObject::tr( "Rescale raster" );
39}
40
41QStringList QgsRescaleRasterAlgorithm::tags() const
42{
43 return QObject::tr( "raster,rescale,minimum,maximum,range" ).split( ',' );
44}
45
46QString QgsRescaleRasterAlgorithm::group() const
47{
48 return QObject::tr( "Raster analysis" );
49}
50
51QString QgsRescaleRasterAlgorithm::groupId() const
52{
53 return u"rasteranalysis"_s;
54}
55
56QString QgsRescaleRasterAlgorithm::shortHelpString() const
57{
58 return QObject::tr(
59 "This algorithm rescales a raster layer to a new value range, while preserving the shape "
60 "(distribution) of the raster's histogram (pixel values). Input values "
61 "are mapped using a linear interpolation from the source raster's minimum "
62 "and maximum pixel values to the destination minimum and maximum pixel range.\n\n"
63 "By default the algorithm preserves the original NoData value, but there is "
64 "an option to override it."
65 );
66}
67
68QString QgsRescaleRasterAlgorithm::shortDescription() const
69{
70 return QObject::tr(
71 "Rescales a raster layer to a new value range, while preserving the shape "
72 "(distribution) of the raster's histogram (pixel values)."
73 );
74}
75
76QgsRescaleRasterAlgorithm *QgsRescaleRasterAlgorithm::createInstance() const
77{
78 return new QgsRescaleRasterAlgorithm();
79}
80
81void QgsRescaleRasterAlgorithm::initAlgorithm( const QVariantMap & )
82{
83 addParameter( new QgsProcessingParameterRasterLayer( u"INPUT"_s, u"Input raster"_s ) );
84 addParameter( new QgsProcessingParameterBand( u"BAND"_s, QObject::tr( "Band number" ), 1, u"INPUT"_s ) );
85 addParameter( new QgsProcessingParameterNumber( u"MINIMUM"_s, QObject::tr( "New minimum value" ), Qgis::ProcessingNumberParameterType::Double, 0 ) );
86 addParameter( new QgsProcessingParameterNumber( u"MAXIMUM"_s, QObject::tr( "New maximum value" ), Qgis::ProcessingNumberParameterType::Double, 255 ) );
87 addParameter( new QgsProcessingParameterNumber( u"NODATA"_s, QObject::tr( "New NoData value" ), Qgis::ProcessingNumberParameterType::Double, QVariant(), true ) );
88
89 // backwards compatibility parameter
90 // TODO QGIS 5: remove parameter and related logic
91 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATE_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
92 createOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
93 createOptsParam->setFlags( createOptsParam->flags() | Qgis::ProcessingParameterFlag::Hidden );
94 addParameter( createOptsParam.release() );
95
96 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( u"CREATION_OPTIONS"_s, QObject::tr( "Creation options" ), QVariant(), false, true );
97 creationOptsParam->setMetadata( QVariantMap( { { u"widget_wrapper"_s, QVariantMap( { { u"widget_type"_s, u"rasteroptions"_s } } ) } } ) );
98 creationOptsParam->setFlags( creationOptsParam->flags() | Qgis::ProcessingParameterFlag::Advanced );
99 addParameter( creationOptsParam.release() );
100
101 addParameter( new QgsProcessingParameterRasterDestination( u"OUTPUT"_s, QObject::tr( "Rescaled" ) ) );
102}
103
104bool QgsRescaleRasterAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
105{
106 Q_UNUSED( feedback );
107
108 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u"INPUT"_s, context );
109 if ( !layer )
110 throw QgsProcessingException( invalidRasterError( parameters, u"INPUT"_s ) );
111
112 mBand = parameterAsInt( parameters, u"BAND"_s, context );
113 if ( mBand < 1 || mBand > layer->bandCount() )
114 throw QgsProcessingException( QObject::tr( "Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->bandCount() ) );
115
116 mMinimum = parameterAsDouble( parameters, u"MINIMUM"_s, context );
117 mMaximum = parameterAsDouble( parameters, u"MAXIMUM"_s, context );
118
119 mInterface.reset( layer->dataProvider()->clone() );
120
121 mCrs = layer->crs();
122 mLayerWidth = layer->width();
123 mLayerHeight = layer->height();
124 mExtent = layer->extent();
125 if ( parameters.value( u"NODATA"_s ).isValid() )
126 {
127 mNoData = parameterAsDouble( parameters, u"NODATA"_s, context );
128 }
129 else
130 {
131 mNoData = layer->dataProvider()->sourceNoDataValue( mBand );
132 }
133
134 if ( std::isfinite( mNoData ) )
135 {
136 // Clamp nodata to float32 range, since that's the type of the raster
137 if ( mNoData < std::numeric_limits<float>::lowest() )
138 mNoData = std::numeric_limits<float>::lowest();
139 else if ( mNoData > std::numeric_limits<float>::max() )
140 mNoData = std::numeric_limits<float>::max();
141 }
142
143 mXSize = mInterface->xSize();
144 mYSize = mInterface->ySize();
145
146 return true;
147}
148
149QVariantMap QgsRescaleRasterAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
150{
151 feedback->pushInfo( QObject::tr( "Calculating raster minimum and maximum values…" ) );
152 const QgsRasterBandStats stats = mInterface->bandStatistics( mBand, Qgis::RasterBandStatistic::Min | Qgis::RasterBandStatistic::Max, QgsRectangle(), 0 );
153
154 feedback->pushInfo( QObject::tr( "Rescaling values…" ) );
155
156 QString creationOptions = parameterAsString( parameters, u"CREATION_OPTIONS"_s, context ).trimmed();
157 // handle backwards compatibility parameter CREATE_OPTIONS
158 const QString optionsString = parameterAsString( parameters, u"CREATE_OPTIONS"_s, context );
159 if ( !optionsString.isEmpty() )
160 creationOptions = optionsString;
161
162 const QString outputFile = parameterAsOutputLayer( parameters, u"OUTPUT"_s, context );
163 const QString outputFormat = parameterAsOutputRasterFormat( parameters, u"OUTPUT"_s, context );
164 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
165 writer->setOutputProviderKey( u"gdal"_s );
166 if ( !creationOptions.isEmpty() )
167 {
168 writer->setCreationOptions( creationOptions.split( '|' ) );
169 }
170
171 writer->setOutputFormat( outputFormat );
172 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( Qgis::DataType::Float32, mXSize, mYSize, mExtent, mCrs ) );
173 if ( !provider )
174 throw QgsProcessingException( QObject::tr( "Could not create raster output: %1" ).arg( outputFile ) );
175 if ( !provider->isValid() )
176 throw QgsProcessingException( QObject::tr( "Could not create raster output %1: %2" ).arg( outputFile, provider->error().message( QgsErrorMessage::Text ) ) );
177
178 QgsRasterDataProvider *destProvider = provider.get();
179 destProvider->setEditable( true );
180 destProvider->setNoDataValue( 1, mNoData );
181
182 const bool hasReportsDuringClose = provider->hasReportsDuringClose();
183 const double maxProgressDuringBlockWriting = hasReportsDuringClose ? 50.0 : 100.0;
184
185 QgsRasterIterator iter( mInterface.get() );
186 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
187 int iterLeft = 0;
188 int iterTop = 0;
189 int iterCols = 0;
190 int iterRows = 0;
191 std::unique_ptr<QgsRasterBlock> inputBlock;
192 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, inputBlock, iterLeft, iterTop ) )
193 {
194 auto outputBlock = std::make_unique<QgsRasterBlock>( destProvider->dataType( 1 ), iterCols, iterRows );
195 feedback->setProgress( maxProgressDuringBlockWriting * iter.progress( mBand ) );
196
197 for ( int row = 0; row < iterRows; row++ )
198 {
199 if ( feedback->isCanceled() )
200 break;
201
202 for ( int col = 0; col < iterCols; col++ )
203 {
204 bool isNoData = false;
205 const double val = inputBlock->valueAndNoData( row, col, isNoData );
206 if ( isNoData )
207 {
208 outputBlock->setValue( row, col, mNoData );
209 }
210 else
211 {
212 const double newValue = ( ( val - stats.minimumValue ) * ( mMaximum - mMinimum ) / ( stats.maximumValue - stats.minimumValue ) ) + mMinimum;
213 outputBlock->setValue( row, col, newValue );
214 }
215 }
216 }
217 if ( !destProvider->writeBlock( outputBlock.get(), mBand, iterLeft, iterTop ) )
218 {
219 throw QgsProcessingException( QObject::tr( "Could not write raster block: %1" ).arg( destProvider->error().summary() ) );
220 }
221 }
222 destProvider->setEditable( false );
223
224 if ( hasReportsDuringClose )
225 {
226 std::unique_ptr<QgsFeedback> scaledFeedback( QgsFeedback::createScaledFeedback( feedback, maxProgressDuringBlockWriting, 100.0 ) );
227 if ( !provider->closeWithProgress( scaledFeedback.get() ) )
228 {
229 if ( feedback->isCanceled() )
230 return {};
231 throw QgsProcessingException( QObject::tr( "Could not write raster dataset" ) );
232 }
233 }
234
235 QVariantMap outputs;
236 outputs.insert( u"OUTPUT"_s, outputFile );
237 return outputs;
238}
239
@ Float32
Thirty two bit floating point (float).
Definition qgis.h:401
@ Hidden
Parameter is hidden and should not be shown to users.
Definition qgis.h:3881
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3880
@ Double
Double/float values.
Definition qgis.h:3921
virtual QgsError error() const
Gets current status error.
QString summary() const
Short error description, usually the first error in chain, the real error.
Definition qgserror.cpp:132
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
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...
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Definition qgsmaplayer.h:90
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.
Iterator for sequentially processing raster cells.
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.