QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsreclassifyutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsreclassifyutils.cpp
3 ---------------------
4 begin : June, 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson 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
18#include "qgsreclassifyutils.h"
19#include "qgsrasterinterface.h"
20#include "qgsrasteriterator.h"
21#include "qgsrasterblock.h"
24
25#include "qgis.h"
26
28
29void QgsReclassifyUtils::reportClasses( const QVector<QgsReclassifyUtils::RasterClass> &classes, QgsProcessingFeedback *feedback )
30{
31 int i = 1;
32 feedback->pushInfo( QObject::tr( "Using classes:" ) );
33 for ( const RasterClass &c : classes )
34 {
35 feedback->pushInfo( QStringLiteral( " %1) %2 %3 %4" ).arg( i ).arg( c.asText() ).arg( QChar( 0x2192 ) ).arg( c.value ) );
36 i++;
37 }
38}
39
40void QgsReclassifyUtils::checkForOverlaps( const QVector<QgsReclassifyUtils::RasterClass> &classes, QgsProcessingFeedback *feedback )
41{
42 // test each class against each other class
43 for ( int i = 0; i < classes.count() - 1; i++ )
44 {
45 for ( int j = i + 1; j < classes.count(); j++ )
46 {
47 const QgsReclassifyUtils::RasterClass &class1 = classes.at( i );
48 const QgsReclassifyUtils::RasterClass &class2 = classes.at( j );
49 if ( class1.overlaps( class2 ) )
50 {
51 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() ) );
52 }
53 }
54 }
55}
56
57void QgsReclassifyUtils::reclassify( const QVector<QgsReclassifyUtils::RasterClass> &classes, QgsRasterInterface *sourceRaster, int band, const QgsRectangle &extent, int sourceWidthPixels, int sourceHeightPixels, QgsRasterDataProvider *destinationRaster, double destNoDataValue, bool useNoDataForMissingValues, QgsProcessingFeedback *feedback )
58{
61
62 QgsRasterIterator iter( sourceRaster );
63 iter.startRasterRead( band, sourceWidthPixels, sourceHeightPixels, extent );
64
65 const int nbBlocksWidth = static_cast<int>( std::ceil( 1.0 * sourceWidthPixels / maxWidth ) );
66 const int nbBlocksHeight = static_cast<int>( std::ceil( 1.0 * sourceHeightPixels / maxHeight ) );
67 const int nbBlocks = nbBlocksWidth * nbBlocksHeight;
68
69 int iterLeft = 0;
70 int iterTop = 0;
71 int iterCols = 0;
72 int iterRows = 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 ) )
78 {
79 if ( feedback )
80 feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
81 if ( feedback && feedback->isCanceled() )
82 break;
83 std::unique_ptr<QgsRasterBlock> reclassifiedBlock = std::make_unique<QgsRasterBlock>( destinationRaster->dataType( 1 ), iterCols, iterRows );
84
85 for ( int row = 0; row < iterRows; row++ )
86 {
87 if ( feedback && feedback->isCanceled() )
88 break;
89 for ( int column = 0; column < iterCols; column++ )
90 {
91 const double value = rasterBlock->valueAndNoData( row, column, isNoData );
92 if ( isNoData )
93 reclassifiedBlock->setValue( row, column, destNoDataValue );
94 else
95 {
96 const double newValue = reclassifyValue( classes, value, reclassed );
97 if ( reclassed )
98 reclassifiedBlock->setValue( row, column, newValue );
99 else
100 reclassifiedBlock->setValue( row, column, useNoDataForMissingValues ? destNoDataValue : value );
101 }
102 }
103 }
104 destinationRaster->writeBlock( reclassifiedBlock.get(), 1, iterLeft, iterTop );
105 }
106 destinationRaster->setEditable( false );
107}
108
109
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
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 raster data providers.
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.
Base class for processing filters like renderers, reprojector, resampler etc.
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.
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