23QString QgsRoundRasterValuesAlgorithm::name()
const
25 return QStringLiteral(
"roundrastervalues" );
28QString QgsRoundRasterValuesAlgorithm::displayName()
const
30 return QObject::tr(
"Round raster" );
33QStringList QgsRoundRasterValuesAlgorithm::tags()
const
35 return QObject::tr(
"data,cells,round,truncate" ).split(
',' );
38QString QgsRoundRasterValuesAlgorithm::group()
const
40 return QObject::tr(
"Raster analysis" );
43QString QgsRoundRasterValuesAlgorithm::groupId()
const
45 return QStringLiteral(
"rasteranalysis" );
48void QgsRoundRasterValuesAlgorithm::initAlgorithm(
const QVariantMap & )
51 addParameter(
new QgsProcessingParameterBand( QStringLiteral(
"BAND" ), QObject::tr(
"Band number" ), 1, QStringLiteral(
"INPUT" ) ) );
52 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"ROUNDING_DIRECTION" ), QObject::tr(
"Rounding direction" ), QStringList() << QObject::tr(
"Round up" ) << QObject::tr(
"Round to nearest" ) << QObject::tr(
"Round down" ),
false, 1 ) );
54 std::unique_ptr<QgsProcessingParameterDefinition> baseParameter = std::make_unique<QgsProcessingParameterNumber>( QStringLiteral(
"BASE_N" ), QObject::tr(
"Base n for rounding to multiples of n" ),
Qgis::ProcessingNumberParameterType::Integer, 10,
true, 1 );
56 addParameter( baseParameter.release() );
60 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
61 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
63 addParameter( createOptsParam.release() );
65 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
66 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
68 addParameter( creationOptsParam.release() );
73QString QgsRoundRasterValuesAlgorithm::shortHelpString()
const
75 return QObject::tr(
"This algorithm rounds the cell values of a raster dataset according to the specified number of decimals.\n "
76 "Alternatively, a negative number of decimal places may be used to round values to powers of a base n "
77 "(specified in the advanced parameter Base n). For example, with a Base value n of 10 and Decimal places of -1 "
78 "the algorithm rounds cell values to multiples of 10, -2 rounds to multiples of 100, and so on. Arbitrary base values "
79 "may be chosen, the algorithm applies the same multiplicative principle. Rounding cell values to multiples of "
80 "a base n may be used to generalize raster layers.\n"
81 "The algorithm preserves the data type of the input raster. Therefore byte/integer rasters can only be rounded "
82 "to multiples of a base n, otherwise a warning is raised and the raster gets copied as byte/integer raster" );
85QgsRoundRasterValuesAlgorithm *QgsRoundRasterValuesAlgorithm::createInstance()
const
87 return new QgsRoundRasterValuesAlgorithm();
93 QgsRasterLayer *inputRaster = parameterAsRasterLayer( parameters, QStringLiteral(
"INPUT" ), context );
94 mDecimalPrecision = parameterAsInt( parameters, QStringLiteral(
"DECIMAL_PLACES" ), context );
95 mBaseN = parameterAsInt( parameters, QStringLiteral(
"BASE_N" ), context );
96 mMultipleOfBaseN = pow( mBaseN, abs( mDecimalPrecision ) );
97 mScaleFactor = std::pow( 10.0, mDecimalPrecision );
102 mBand = parameterAsInt( parameters, QStringLiteral(
"BAND" ), context );
103 if ( mBand < 1 || mBand > inputRaster->
bandCount() )
104 throw QgsProcessingException( QObject::tr(
"Invalid band number for BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( inputRaster->
bandCount() ) );
106 mRoundingDirection = parameterAsEnum( parameters, QStringLiteral(
"ROUNDING_DIRECTION" ), context );
109 mDataType = mInterface->dataType( mBand );
119 if ( mDecimalPrecision > -1 )
120 feedback->
reportError( QObject::tr(
"Input raster is of byte or integer type. The cell values cannot be rounded and will be output using the same data type." ),
false );
128 mExtent = inputRaster->
extent();
129 mLayerWidth = inputRaster->
width();
130 mLayerHeight = inputRaster->
height();
131 mCrs = inputRaster->
crs();
132 mNbCellsXProvider = mInterface->xSize();
133 mNbCellsYProvider = mInterface->ySize();
140 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
142 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
143 if ( !optionsString.isEmpty() )
144 creationOptions = optionsString;
146 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
147 const QFileInfo fi( outputFile );
149 auto writer = std::make_unique<QgsRasterFileWriter>( outputFile );
150 writer->setOutputProviderKey( QStringLiteral(
"gdal" ) );
151 if ( !creationOptions.isEmpty() )
153 writer->setCreationOptions( creationOptions.split(
'|' ) );
155 writer->setOutputFormat( outputFormat );
156 std::unique_ptr<QgsRasterDataProvider> provider( writer->createOneBandRaster( mInterface->dataType( mBand ), mNbCellsXProvider, mNbCellsYProvider, mExtent, mCrs ) );
159 if ( !provider->isValid() )
164 destinationRasterProvider = provider.get();
166 destinationRasterProvider->
setNoDataValue( 1, mInputNoDataValue );
170 const int nbBlocksWidth =
static_cast<int>( std::ceil( 1.0 * mLayerWidth / maxWidth ) );
171 const int nbBlocksHeight =
static_cast<int>( std::ceil( 1.0 * mLayerHeight / maxHeight ) );
172 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
175 iter.startRasterRead( mBand, mLayerWidth, mLayerHeight, mExtent );
180 std::unique_ptr<QgsRasterBlock> analysisRasterBlock;
181 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, analysisRasterBlock, iterLeft, iterTop ) )
184 feedback->
setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
185 if ( mIsInteger && mDecimalPrecision > -1 )
188 analysisRasterBlock->setNoDataValue( mInputNoDataValue );
189 if ( !destinationRasterProvider->
writeBlock( analysisRasterBlock.get(), mBand, iterLeft, iterTop ) )
196 for (
int row = 0; row < iterRows; row++ )
200 for (
int column = 0; column < iterCols; column++ )
202 bool isNoData =
false;
203 const double val = analysisRasterBlock->valueAndNoData( row, column, isNoData );
206 analysisRasterBlock->setValue( row, column, mInputNoDataValue );
210 double roundedVal = mInputNoDataValue;
211 if ( mRoundingDirection == 0 && mDecimalPrecision < 0 )
213 roundedVal = roundUpBaseN( val );
215 else if ( mRoundingDirection == 0 && mDecimalPrecision > -1 )
217 const double m = ( val < 0.0 ) ? -1.0 : 1.0;
218 roundedVal = roundUp( val, m );
220 else if ( mRoundingDirection == 1 && mDecimalPrecision < 0 )
222 roundedVal = roundNearestBaseN( val );
224 else if ( mRoundingDirection == 1 && mDecimalPrecision > -1 )
226 const double m = ( val < 0.0 ) ? -1.0 : 1.0;
227 roundedVal = roundNearest( val, m );
229 else if ( mRoundingDirection == 2 && mDecimalPrecision < 0 )
231 roundedVal = roundDownBaseN( val );
235 const double m = ( val < 0.0 ) ? -1.0 : 1.0;
236 roundedVal = roundDown( val, m );
239 analysisRasterBlock->setValue( row, column, roundedVal );
243 if ( !destinationRasterProvider->
writeBlock( analysisRasterBlock.get(), mBand, iterLeft, iterTop ) )
252 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
256double QgsRoundRasterValuesAlgorithm::roundNearest(
double value,
double m )
258 return ( std::round( value * m * mScaleFactor ) / mScaleFactor ) * m;
261double QgsRoundRasterValuesAlgorithm::roundUp(
double value,
double m )
263 return ( std::ceil( value * m * mScaleFactor ) / mScaleFactor ) * m;
266double QgsRoundRasterValuesAlgorithm::roundDown(
double value,
double m )
268 return ( std::floor( value * m * mScaleFactor ) / mScaleFactor ) * m;
271double QgsRoundRasterValuesAlgorithm::roundNearestBaseN(
double value )
273 return static_cast<double>( mMultipleOfBaseN * round( value / mMultipleOfBaseN ) );
276double QgsRoundRasterValuesAlgorithm::roundUpBaseN(
double value )
278 return static_cast<double>( mMultipleOfBaseN * ceil( value / mMultipleOfBaseN ) );
281double QgsRoundRasterValuesAlgorithm::roundDownBaseN(
double value )
283 return static_cast<double>( mMultipleOfBaseN * floor( value / mMultipleOfBaseN ) );
@ Int16
Sixteen bit signed integer (qint16)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ UInt32
Thirty two bit unsigned integer (quint32)
@ 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.
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 reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A raster band parameter for Processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
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.
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.
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.