Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Modernize code
(cherry-picked from d79dd73)
  • Loading branch information
nyalldawson committed Jul 20, 2018
1 parent 4a997c9 commit 6ec310b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 28 deletions.
10 changes: 3 additions & 7 deletions src/3d/terrain/qgsdemterraintileloader_p.cpp
Expand Up @@ -162,7 +162,7 @@ static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectan
projector->setInput( provider );
input = projector.get();
}
QgsRasterBlock *block = input->block( 1, extent, res, res );
std::unique_ptr< QgsRasterBlock > block( input->block( 1, extent, res, res ) );

QByteArray data;
if ( block )
Expand All @@ -183,8 +183,6 @@ static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectan
floatData[i] = std::numeric_limits<float>::quiet_NaN();
}
}

delete block;
}
return data;
}
Expand Down Expand Up @@ -227,15 +225,14 @@ QByteArray QgsDemHeightMapGenerator::renderSynchronously( int x, int y, int z )
QgsRectangle fullExtent = mTilingScheme.tileToExtent( 0, 0, 0 );
extent = extent.intersect( fullExtent );

QgsRasterBlock *block = mDtm->dataProvider()->block( 1, extent, mResolution, mResolution );
std::unique_ptr< QgsRasterBlock > block( mDtm->dataProvider()->block( 1, extent, mResolution, mResolution ) );

QByteArray data;
if ( block )
{
block->convert( Qgis::Float32 ); // currently we expect just floats
data = block->data();
data.detach(); // this should make a deep copy
delete block;
}

return data;
Expand All @@ -248,11 +245,10 @@ float QgsDemHeightMapGenerator::heightAt( double x, double y )
QgsRectangle rect = mDtm->extent();
if ( mDtmCoarseData.isEmpty() )
{
QgsRasterBlock *block = mDtm->dataProvider()->block( 1, rect, res, res );
std::unique_ptr< QgsRasterBlock > block( mDtm->dataProvider()->block( 1, rect, res, res ) );
block->convert( Qgis::Float32 );
mDtmCoarseData = block->data();
mDtmCoarseData.detach(); // make a deep copy
delete block;
}

int cellX = ( int )( ( x - rect.xMinimum() ) / rect.width() * res + .5f );
Expand Down
17 changes: 6 additions & 11 deletions src/analysis/raster/qgsrastercalculator.cpp
Expand Up @@ -61,7 +61,7 @@ int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )
{
//prepare search string / tree
QString errorString;
QgsRasterCalcNode *calcNode = QgsRasterCalcNode::parseRasterCalcString( mFormulaString, errorString );
std::unique_ptr< QgsRasterCalcNode > calcNode( QgsRasterCalcNode::parseRasterCalcString( mFormulaString, errorString ) );
if ( !calcNode )
{
//error
Expand All @@ -74,12 +74,11 @@ int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )
{
if ( !it->raster ) // no raster layer in entry
{
delete calcNode;
qDeleteAll( inputBlocks );
return static_cast< int >( InputLayerError );
}

QgsRasterBlock *block = nullptr;
std::unique_ptr< QgsRasterBlock > block;
// if crs transform needed
if ( it->raster->crs() != mOutputCrs )
{
Expand All @@ -90,27 +89,23 @@ int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )

QgsRasterBlockFeedback *rasterBlockFeedback = new QgsRasterBlockFeedback();
QObject::connect( feedback, &QgsFeedback::canceled, rasterBlockFeedback, &QgsRasterBlockFeedback::cancel );
block = proj.block( it->bandNumber, mOutputRectangle, mNumOutputColumns, mNumOutputRows, rasterBlockFeedback );
block.reset( proj.block( it->bandNumber, mOutputRectangle, mNumOutputColumns, mNumOutputRows, rasterBlockFeedback ) );
if ( rasterBlockFeedback->isCanceled() )
{
delete block;
delete calcNode;
qDeleteAll( inputBlocks );
return static_cast< int >( Canceled );
}
}
else
{
block = it->raster->dataProvider()->block( it->bandNumber, mOutputRectangle, mNumOutputColumns, mNumOutputRows );
block.reset( it->raster->dataProvider()->block( it->bandNumber, mOutputRectangle, mNumOutputColumns, mNumOutputRows ) );
}
if ( block->isEmpty() )
{
delete block;
delete calcNode;
qDeleteAll( inputBlocks );
return static_cast<int>( MemoryError );
}
inputBlocks.insert( it->ref, block );
inputBlocks.insert( it->ref, block.release() );
}

//open output dataset for writing
Expand Down Expand Up @@ -175,7 +170,7 @@ int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )
}

//close datasets and release memory
delete calcNode;
calcNode.reset();
qDeleteAll( inputBlocks );
inputBlocks.clear();

Expand Down
7 changes: 2 additions & 5 deletions src/core/raster/qgsrasterchecker.cpp
Expand Up @@ -142,8 +142,8 @@ bool QgsRasterChecker::runTest( const QString &verifiedKey, QString verifiedUri,

int width = expectedProvider->xSize();
int height = expectedProvider->ySize();
QgsRasterBlock *expectedBlock = expectedProvider->block( band, expectedProvider->extent(), width, height );
QgsRasterBlock *verifiedBlock = verifiedProvider->block( band, expectedProvider->extent(), width, height );
std::unique_ptr< QgsRasterBlock > expectedBlock( expectedProvider->block( band, expectedProvider->extent(), width, height ) );
std::unique_ptr< QgsRasterBlock > verifiedBlock( verifiedProvider->block( band, expectedProvider->extent(), width, height ) );

if ( !expectedBlock || !expectedBlock->isValid() ||
!verifiedBlock || !verifiedBlock->isValid() )
Expand Down Expand Up @@ -182,9 +182,6 @@ bool QgsRasterChecker::runTest( const QString &verifiedKey, QString verifiedUri,
htmlTable += QLatin1String( "</table>" );

mReport += htmlTable;

delete expectedBlock;
delete verifiedBlock;
}
delete verifiedProvider;
delete expectedProvider;
Expand Down
8 changes: 3 additions & 5 deletions src/core/raster/qgsrasterinterface.cpp
Expand Up @@ -69,7 +69,7 @@ void QgsRasterInterface::initStatistics( QgsRasterBandStats &statistics,
if ( xRes < srcXRes ) xRes = srcXRes;
if ( yRes < srcYRes ) yRes = srcYRes;
}
QgsDebugMsgLevel( QString( "xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ), 4 );
QgsDebugMsgLevel( QStringLiteral( "xRes = %1 yRes = %2" ).arg( xRes ).arg( yRes ), 4 );

statistics.width = static_cast <int>( finalExtent.width() / xRes );
statistics.height = static_cast <int>( finalExtent.height() / yRes );
Expand Down Expand Up @@ -180,7 +180,7 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int bandNo,

QgsRectangle myPartExtent( xmin, ymin, xmax, ymax );

QgsRasterBlock *blk = block( bandNo, myPartExtent, myBlockWidth, myBlockHeight, feedback );
std::unique_ptr< QgsRasterBlock > blk( block( bandNo, myPartExtent, myBlockWidth, myBlockHeight, feedback ) );

// Collect the histogram counts.
for ( qgssize i = 0; i < ( static_cast< qgssize >( myBlockHeight ) ) * myBlockWidth; i++ )
Expand Down Expand Up @@ -216,7 +216,6 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int bandNo,
myMean += myDelta / myRasterBandStats.elementCount;
mySumOfSquares += myDelta * ( myValue - myMean );
}
delete blk;
}
}

Expand Down Expand Up @@ -466,7 +465,7 @@ QgsRasterHistogram QgsRasterInterface::histogram( int bandNo,

QgsRectangle myPartExtent( xmin, ymin, xmax, ymax );

QgsRasterBlock *blk = block( bandNo, myPartExtent, myBlockWidth, myBlockHeight, feedback );
std::unique_ptr< QgsRasterBlock > blk( block( bandNo, myPartExtent, myBlockWidth, myBlockHeight, feedback ) );

// Collect the histogram counts.
for ( qgssize i = 0; i < ( static_cast< qgssize >( myBlockHeight ) ) * myBlockWidth; i++ )
Expand All @@ -489,7 +488,6 @@ QgsRasterHistogram QgsRasterInterface::histogram( int bandNo,
myHistogram.histogramVector[myBinIndex] += 1;
myHistogram.nonNullCount++;
}
delete blk;
}
}

Expand Down

0 comments on commit 6ec310b

Please sign in to comment.