Skip to content

Commit 79c907b

Browse files
committed
Add unique_ptr variant to QgsRasterIterator::readNextRasterPart
1 parent dc77c59 commit 79c907b

8 files changed

+57
-39
lines changed

python/core/auto_generated/raster/qgsrasteriterator.sip.in

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ caller should delete the block.
4747
:param topLeftCol: top left column
4848
:param topLeftRow: top left row
4949

50-
:return: false if the last part was already returned*
50+
:return: false if the last part was already returned
5151
%End
5252

53+
5354
void stopRasterRead( int bandNumber );
5455

5556
const QgsRasterInterface *input() const;

src/analysis/processing/qgsalgorithmrasterlayeruniquevalues.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const Q
134134
int iterTop = 0;
135135
int iterCols = 0;
136136
int iterRows = 0;
137-
QgsRasterBlock *rasterBlock = nullptr;
138-
while ( iter.readNextRasterPart( band, iterCols, iterRows, &rasterBlock, iterLeft, iterTop ) )
137+
std::unique_ptr< QgsRasterBlock > rasterBlock;
138+
while ( iter.readNextRasterPart( band, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
139139
{
140140
feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
141141
for ( int row = 0; row < iterRows; row++ )
@@ -155,7 +155,6 @@ QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const Q
155155
}
156156
}
157157
}
158-
delete rasterBlock;
159158
}
160159

161160
QMap< double, qgssize > sortedUniqueValues;

src/analysis/processing/qgsrasteranalysisutils.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ void QgsRasterAnalysisUtils::statisticsFromMiddlePointTest( QgsRasterInterface *
7272
iter.setMaximumTileHeight( maxHeight );
7373
iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
7474

75-
QgsRasterBlock *block = nullptr;
75+
std::unique_ptr< QgsRasterBlock > block;
7676
int iterLeft = 0;
7777
int iterTop = 0;
7878
int iterCols = 0;
7979
int iterRows = 0;
80-
while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, &block, iterLeft, iterTop ) )
80+
while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop ) )
8181
{
8282
double cellCenterY = rasterBBox.yMinimum() + ( iterTop + iterRows - 0.5 ) * cellSizeY;
8383
for ( int row = 0; row < iterRows; ++row )
@@ -98,7 +98,6 @@ void QgsRasterAnalysisUtils::statisticsFromMiddlePointTest( QgsRasterInterface *
9898
}
9999
cellCenterY -= cellSizeY;
100100
}
101-
delete block;
102101
}
103102
}
104103

@@ -126,12 +125,12 @@ void QgsRasterAnalysisUtils::statisticsFromPreciseIntersection( QgsRasterInterfa
126125
iter.setMaximumTileHeight( maxHeight );
127126
iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
128127

129-
QgsRasterBlock *block = nullptr;
128+
std::unique_ptr< QgsRasterBlock > block;
130129
int iterLeft = 0;
131130
int iterTop = 0;
132131
int iterCols = 0;
133132
int iterRows = 0;
134-
while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, &block, iterLeft, iterTop ) )
133+
while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop ) )
135134
{
136135
double currentY = rasterBBox.yMinimum() + ( iterTop + iterRows - 0.5 ) * cellSizeY;
137136
for ( int row = 0; row < iterRows; ++row )

src/analysis/processing/qgsreclassifyutils.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ void QgsReclassifyUtils::reclassify( const QVector<QgsReclassifyUtils::RasterCla
4848
int iterCols = 0;
4949
int iterRows = 0;
5050
destinationRaster->setEditable( true );
51-
QgsRasterBlock *rasterBlock = nullptr;
51+
std::unique_ptr< QgsRasterBlock > rasterBlock;
5252
bool reclassed = false;
53-
while ( iter.readNextRasterPart( band, iterCols, iterRows, &rasterBlock, iterLeft, iterTop ) )
53+
while ( iter.readNextRasterPart( band, iterCols, iterRows, rasterBlock, iterLeft, iterTop ) )
5454
{
5555
if ( feedback )
5656
feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks );
@@ -76,8 +76,6 @@ void QgsReclassifyUtils::reclassify( const QVector<QgsReclassifyUtils::RasterCla
7676
}
7777
}
7878
destinationRaster->writeBlock( reclassifiedBlock.get(), 1, iterLeft, iterTop );
79-
80-
delete rasterBlock;
8179
}
8280
destinationRaster->setEditable( false );
8381
}

src/core/raster/qgsrasterdrawer.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsM
5252

5353
// We know that the output data type of last pipe filter is QImage data
5454

55-
QgsRasterBlock *block = nullptr;
55+
std::unique_ptr< QgsRasterBlock > block;
5656

5757
// readNextRasterPart calcs and resets nCols, nRows, topLeftCol, topLeftRow
5858
while ( mIterator->readNextRasterPart( bandNumber, nCols, nRows,
59-
&block, topLeftCol, topLeftRow ) )
59+
block, topLeftCol, topLeftRow ) )
6060
{
6161
if ( !block )
6262
{
@@ -100,8 +100,6 @@ void QgsRasterDrawer::draw( QPainter *p, QgsRasterViewPort *viewPort, const QgsM
100100

101101
drawImage( p, viewPort, img, topLeftCol, topLeftRow, qgsMapToPixel );
102102

103-
delete block;
104-
105103
if ( feedback && feedback->renderPartialOutput() )
106104
{
107105
// go back to the default composition mode

src/core/raster/qgsrasterfilewriter.cpp

+6-11
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,11 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
501501
int fileIndex = 0;
502502

503503
//create destProvider for whole dataset here
504-
QgsRasterDataProvider *destProvider = nullptr;
505504
double pixelSize;
506505
double geoTransform[6];
507506
globalOutputParameters( outputExtent, nCols, nRows, geoTransform, pixelSize );
508507

509-
destProvider = initOutput( nCols, nRows, crs, geoTransform, 4, Qgis::Byte );
508+
std::unique_ptr< QgsRasterDataProvider > destProvider( initOutput( nCols, nRows, crs, geoTransform, 4, Qgis::Byte ) );
510509

511510
iter->startRasterRead( 1, nCols, nRows, outputExtent, feedback );
512511

@@ -518,8 +517,8 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
518517
nParts = nPartsX * nPartsY;
519518
}
520519

521-
QgsRasterBlock *inputBlock = nullptr;
522-
while ( iter->readNextRasterPart( 1, iterCols, iterRows, &inputBlock, iterLeft, iterTop ) )
520+
std::unique_ptr< QgsRasterBlock > inputBlock;
521+
while ( iter->readNextRasterPart( 1, iterCols, iterRows, inputBlock, iterLeft, iterTop ) )
523522
{
524523
if ( !inputBlock )
525524
{
@@ -531,7 +530,6 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
531530
feedback->setProgress( 100.0 * fileIndex / static_cast< double >( nParts ) );
532531
if ( feedback->isCanceled() )
533532
{
534-
delete inputBlock;
535533
break;
536534
}
537535
}
@@ -564,16 +562,15 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
564562
memcpy( reinterpret_cast< char * >( blueData ) + i, &blue, 1 );
565563
memcpy( reinterpret_cast< char * >( alphaData ) + i, &alpha, 1 );
566564
}
567-
delete inputBlock;
568565

569566
//create output file
570567
if ( mTiledMode )
571568
{
572569
//delete destProvider;
573-
QgsRasterDataProvider *partDestProvider = createPartProvider( outputExtent,
570+
std::unique_ptr< QgsRasterDataProvider > partDestProvider( createPartProvider( outputExtent,
574571
nCols, iterCols, iterRows,
575572
iterLeft, iterTop, mOutputUrl, fileIndex,
576-
4, Qgis::Byte, crs );
573+
4, Qgis::Byte, crs ) );
577574

578575
if ( partDestProvider )
579576
{
@@ -587,7 +584,6 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
587584
addToVRT( partFileName( fileIndex ), 2, iterCols, iterRows, iterLeft, iterTop );
588585
addToVRT( partFileName( fileIndex ), 3, iterCols, iterRows, iterLeft, iterTop );
589586
addToVRT( partFileName( fileIndex ), 4, iterCols, iterRows, iterLeft, iterTop );
590-
delete partDestProvider;
591587
}
592588
}
593589
else if ( destProvider )
@@ -600,8 +596,7 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
600596

601597
++fileIndex;
602598
}
603-
604-
delete destProvider;
599+
destProvider.reset();
605600

606601
qgsFree( redData );
607602
qgsFree( greenData );

src/core/raster/qgsrasteriterator.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,18 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber,
6363
QgsRasterBlock **block,
6464
int &topLeftCol, int &topLeftRow )
6565
{
66-
QgsDebugMsgLevel( "Entered", 4 );
6766
*block = nullptr;
67+
std::unique_ptr< QgsRasterBlock > nextBlock;
68+
bool result = readNextRasterPart( bandNumber, nCols, nRows, nextBlock, topLeftCol, topLeftRow );
69+
if ( result )
70+
*block = nextBlock.release();
71+
return result;
72+
}
73+
74+
bool QgsRasterIterator::readNextRasterPart( int bandNumber, int &nCols, int &nRows, std::unique_ptr<QgsRasterBlock> &block, int &topLeftCol, int &topLeftRow )
75+
{
76+
QgsDebugMsgLevel( QStringLiteral( "Entered" ), 4 );
77+
block.reset();
6878
//get partinfo
6979
QMap<int, RasterPartInfo>::iterator partIt = mRasterPartInfos.find( bandNumber );
7080
if ( partIt == mRasterPartInfos.end() )
@@ -93,7 +103,7 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber,
93103
//read data block
94104
nCols = std::min( mMaximumTileWidth, pInfo.nCols - pInfo.currentCol );
95105
nRows = std::min( mMaximumTileHeight, pInfo.nRows - pInfo.currentRow );
96-
QgsDebugMsgLevel( QString( "nCols = %1 nRows = %2" ).arg( nCols ).arg( nRows ), 4 );
106+
QgsDebugMsgLevel( QStringLiteral( "nCols = %1 nRows = %2" ).arg( nCols ).arg( nRows ), 4 );
97107

98108
//get subrectangle
99109
QgsRectangle viewPortExtent = mExtent;
@@ -105,7 +115,7 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber,
105115
double ymax = viewPortExtent.yMaximum() - pInfo.currentRow / static_cast< double >( pInfo.nRows ) * viewPortExtent.height();
106116
QgsRectangle blockRect( xmin, ymin, xmax, ymax );
107117

108-
*block = mInput->block( bandNumber, blockRect, nCols, nRows, mFeedback );
118+
block.reset( mInput->block( bandNumber, blockRect, nCols, nRows, mFeedback ) );
109119
topLeftCol = pInfo.currentCol;
110120
topLeftRow = pInfo.currentRow;
111121

src/core/raster/qgsrasteriterator.h

+26-8
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,37 @@ class CORE_EXPORT QgsRasterIterator
4848

4949
/**
5050
* Fetches next part of raster data, caller takes ownership of the block and
51-
caller should delete the block.
52-
\param bandNumber band to read
53-
\param nCols number of columns on output device
54-
\param nRows number of rows on output device
55-
\param block address of block pointer
56-
\param topLeftCol top left column
57-
\param topLeftRow top left row
58-
\returns false if the last part was already returned*/
51+
* caller should delete the block.
52+
* \param bandNumber band to read
53+
* \param nCols number of columns on output device
54+
* \param nRows number of rows on output device
55+
* \param block address of block pointer
56+
* \param topLeftCol top left column
57+
* \param topLeftRow top left row
58+
* \returns false if the last part was already returned
59+
*/
5960
bool readNextRasterPart( int bandNumber,
6061
int &nCols, int &nRows,
6162
QgsRasterBlock **block,
6263
int &topLeftCol, int &topLeftRow );
6364

65+
/**
66+
* Fetches next part of raster data.
67+
* \param bandNumber band to read
68+
* \param nCols number of columns on output device
69+
* \param nRows number of rows on output device
70+
* \param block address of block pointer
71+
* \param topLeftCol top left column
72+
* \param topLeftRow top left row
73+
* \returns false if the last part was already returned
74+
* \note Not available in Python bindings
75+
* \since QGIS 3.2
76+
*/
77+
bool readNextRasterPart( int bandNumber,
78+
int &nCols, int &nRows,
79+
std::unique_ptr< QgsRasterBlock > &block,
80+
int &topLeftCol, int &topLeftRow ) SIP_SKIP;
81+
6482
void stopRasterRead( int bandNumber );
6583

6684
const QgsRasterInterface *input() const { return mInput; }

0 commit comments

Comments
 (0)