Skip to content
Permalink
Browse files

Modernize code

  • Loading branch information
nyalldawson committed Jul 20, 2018
1 parent 4fccc5e commit d79dd73bd3242db9d5b33e6dbd0badbbfac25dd2
@@ -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 )
@@ -183,8 +183,6 @@ static QByteArray _readDtmData( QgsRasterDataProvider *provider, const QgsRectan
floatData[i] = std::numeric_limits<float>::quiet_NaN();
}
}

delete block;
}
return data;
}
@@ -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;
@@ -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 );
@@ -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
@@ -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 )
{
@@ -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
@@ -175,7 +170,7 @@ int QgsRasterCalculator::processCalculation( QgsFeedback *feedback )
}

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

@@ -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() )
@@ -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;
@@ -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 );
@@ -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++ )
@@ -216,7 +216,6 @@ QgsRasterBandStats QgsRasterInterface::bandStatistics( int bandNo,
myMean += myDelta / myRasterBandStats.elementCount;
mySumOfSquares += myDelta * ( myValue - myMean );
}
delete blk;
}
}

@@ -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++ )
@@ -489,7 +488,6 @@ QgsRasterHistogram QgsRasterInterface::histogram( int bandNo,
myHistogram.histogramVector[myBinIndex] += 1;
myHistogram.nonNullCount++;
}
delete blk;
}
}

0 comments on commit d79dd73

Please sign in to comment.
You can’t perform that action at this time.