@@ -106,11 +106,12 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
106
106
float outputNodataValue = -FLT_MAX;
107
107
GDALSetRasterNoDataValue ( outputRasterBand, outputNodataValue );
108
108
109
-
110
- // If we need to read the raster as a whole
109
+ // Check if we need to read the raster as a whole (which is memory inefficient
110
+ // and not interruptable by the user) by checking if any raster matrix nodes are
111
+ // in the expression
111
112
bool requiresMatrix = ! calcNode->findNodes ( QgsRasterCalcNode::Type::tMatrix ).isEmpty ();
112
113
113
-
114
+ // Take the fast route (process one line at a time) if we can
114
115
if ( ! requiresMatrix )
115
116
{
116
117
// Map of raster names -> blocks
@@ -151,12 +152,12 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
151
152
break ;
152
153
}
153
154
154
- // Read one row
155
+ // Calculates the rect for a single row read
155
156
QgsRectangle rect ( mOutputRectangle );
156
157
rect.setYMaximum ( rect.yMaximum () - rowHeight * row );
157
158
rect.setYMinimum ( rect.yMaximum () - rowHeight );
158
159
159
- // Read blocks
160
+ // Read rows into input blocks
160
161
for ( auto &layerRef : inputBlocks )
161
162
{
162
163
QgsRasterCalculatorEntry ref = uniqueRasterEntries[layerRef.first ];
@@ -181,17 +182,14 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
181
182
for ( const auto &layerRef : inputBlocks )
182
183
{
183
184
_rasterData.insert ( layerRef.first , inputBlocks[layerRef.first ].get () );
184
- // for ( int i = 0; i < mNumOutputColumns; i++ )
185
- // qDebug() << "Input: " << row << i << " = " << inputBlocks[layerRef.first]->value(0, i);
186
185
}
187
186
188
187
if ( calcNode->calculate ( _rasterData, resultMatrix, 0 ) )
189
188
{
190
- // write scanline to the dataset
189
+ // write scanline to the dataset
191
190
for ( size_t i = 0 ; i < static_cast <size_t >( mNumOutputColumns ); i++ )
192
191
{
193
192
castedResult[i] = static_cast <float >( resultMatrix.data ()[i] );
194
- // qDebug() << "Calculated: " << row << i << " = " << castedResult[i];
195
193
}
196
194
if ( GDALRasterIO ( outputRasterBand, GF_Write, 0 , row, mNumOutputColumns , 1 , castedResult.data (), mNumOutputColumns , 1 , GDT_Float32, 0 , 0 ) != CE_None )
197
195
{
@@ -204,10 +202,8 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
204
202
{
205
203
feedback->setProgress ( 100.0 );
206
204
}
207
-
208
-
209
205
}
210
- else
206
+ else // Original code (memory inefficient route)
211
207
{
212
208
QMap< QString, QgsRasterBlock * > inputBlocks;
213
209
QVector<QgsRasterCalculatorEntry>::const_iterator it = mRasterEntries .constBegin ();
@@ -294,7 +290,6 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
294
290
295
291
}
296
292
297
-
298
293
if ( feedback && feedback->isCanceled () )
299
294
{
300
295
// delete the dataset without closing (because it is faster)
0 commit comments