29using namespace Qt::StringLiterals;
33void QgsReclassifyUtils::reportClasses(
const QVector<QgsReclassifyUtils::RasterClass> &classes,
QgsProcessingFeedback *feedback )
36 feedback->
pushInfo( QObject::tr(
"Using classes:" ) );
37 for (
const RasterClass &
c : classes )
39 feedback->
pushInfo( u
" %1) %2 %3 %4"_s.arg( i ).arg(
c.asText() ).arg( QChar( 0x2192 ) ).arg(
c.value ) );
44void QgsReclassifyUtils::checkForOverlaps(
const QVector<QgsReclassifyUtils::RasterClass> &classes,
QgsProcessingFeedback *feedback )
47 for (
int i = 0; i < classes.count() - 1; i++ )
49 for (
int j = i + 1; j < classes.count(); j++ )
51 const QgsReclassifyUtils::RasterClass &class1 = classes.at( i );
52 const QgsReclassifyUtils::RasterClass &class2 = classes.at( j );
53 if ( class1.overlaps( class2 ) )
55 feedback->
reportError( QObject::tr(
"Warning: Class %1 (%2) overlaps with class %3 (%4)" ).arg( i + 1 ).arg( class1.asText() ).arg( j + 1 ).arg( class2.asText() ) );
61void QgsReclassifyUtils::reclassify(
const QVector<QgsReclassifyUtils::RasterClass> &classes,
QgsRasterInterface *sourceRaster,
int band,
const QgsRectangle &extent,
int sourceWidthPixels,
int sourceHeightPixels, std::unique_ptr<QgsRasterDataProvider> destinationRaster,
double destNoDataValue,
bool useNoDataForMissingValues,
QgsProcessingFeedback *feedback )
64 iter.startRasterRead( band, sourceWidthPixels, sourceHeightPixels, extent );
66 const bool hasReportsDuringClose = destinationRaster->hasReportsDuringClose();
67 const double maxProgressDuringBlockWriting = hasReportsDuringClose ? 50.0 : 100.0;
73 destinationRaster->setEditable(
true );
74 std::unique_ptr<QgsRasterBlock> rasterBlock;
75 bool reclassed =
false;
76 bool isNoData =
false;
77 while ( iter.readNextRasterPart( band, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
80 feedback->
setProgress( maxProgressDuringBlockWriting * iter.progress( band ) );
83 auto reclassifiedBlock = std::make_unique<QgsRasterBlock>( destinationRaster->dataType( 1 ), iterCols, iterRows );
85 for (
int row = 0; row < iterRows; row++ )
89 for (
int column = 0; column < iterCols; column++ )
91 const double value = rasterBlock->valueAndNoData( row, column, isNoData );
93 reclassifiedBlock->setValue( row, column, destNoDataValue );
96 const double newValue = reclassifyValue( classes, value, reclassed );
98 reclassifiedBlock->setValue( row, column, newValue );
100 reclassifiedBlock->setValue( row, column, useNoDataForMissingValues ? destNoDataValue : value );
104 if ( !destinationRaster->writeBlock( reclassifiedBlock.get(), 1, iterLeft, iterTop ) )
106 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( destinationRaster->error().summary() ) );
109 destinationRaster->setEditable(
false );
111 if ( feedback && hasReportsDuringClose )
114 if ( !destinationRaster->closeWithProgress( scaledFeedback.get() ) )
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
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...
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.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
Base class for processing filters like renderers, reprojector, resampler etc.
Iterator for sequentially processing raster cells.
A rectangle specified with double values.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c