use enum for return value instead of plain int in the QgsNineCellFilter

This commit is contained in:
Alexander Bruy 2025-04-03 17:11:47 +01:00 committed by Nyall Dawson
parent 32433f7016
commit a584e533d9
8 changed files with 83 additions and 45 deletions

View File

@ -1,4 +1,12 @@
# The following has been generated automatically from src/analysis/raster/qgsninecellfilter.h
QgsNineCellFilter.Success = QgsNineCellFilter.Result.Success
QgsNineCellFilter.InputLayerError = QgsNineCellFilter.Result.InputLayerError
QgsNineCellFilter.DriverError = QgsNineCellFilter.Result.DriverError
QgsNineCellFilter.CreateOutputError = QgsNineCellFilter.Result.CreateOutputError
QgsNineCellFilter.InputBandError = QgsNineCellFilter.Result.InputBandError
QgsNineCellFilter.OutputBandError = QgsNineCellFilter.Result.OutputBandError
QgsNineCellFilter.RasterSizeError = QgsNineCellFilter.Result.RasterSizeError
QgsNineCellFilter.Canceled = QgsNineCellFilter.Result.Canceled
try:
QgsNineCellFilter.__abstract_methods__ = ['processNineCellWindow']
QgsNineCellFilter.__group__ = ['raster']

View File

@ -27,6 +27,18 @@ subclass
#include "qgsninecellfilter.h"
%End
public:
enum Result /BaseType=IntEnum/
{
Success,
InputLayerError,
DriverError,
CreateOutputError,
InputBandError,
OutputBandError,
RasterSizeError,
Canceled,
};
QgsNineCellFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat );
%Docstring
Constructor that takes input file, output file and output format (GDAL
@ -34,7 +46,7 @@ string)
%End
virtual ~QgsNineCellFilter();
int processRaster( QgsFeedback *feedback = 0 );
Result processRaster( QgsFeedback *feedback = 0 );
%Docstring
Starts the calculation, reads from mInputFile and stores the result in
mOutputFile
@ -42,7 +54,8 @@ mOutputFile
:param feedback: feedback object that receives update and that is
checked for cancellation.
:return: 0 in case of success
:return: QgsNineCellFilter.Success in case of success or error value on
failure.
%End
double cellSizeX() const;

View File

@ -27,6 +27,18 @@ subclass
#include "qgsninecellfilter.h"
%End
public:
enum Result
{
Success,
InputLayerError,
DriverError,
CreateOutputError,
InputBandError,
OutputBandError,
RasterSizeError,
Canceled,
};
QgsNineCellFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat );
%Docstring
Constructor that takes input file, output file and output format (GDAL
@ -34,7 +46,7 @@ string)
%End
virtual ~QgsNineCellFilter();
int processRaster( QgsFeedback *feedback = 0 );
Result processRaster( QgsFeedback *feedback = 0 );
%Docstring
Starts the calculation, reads from mInputFile and stores the result in
mOutputFile
@ -42,7 +54,8 @@ mOutputFile
:param feedback: feedback object that receives update and that is
checked for cancellation.
:return: 0 in case of success
:return: QgsNineCellFilter.Success in case of success or error value on
failure.
%End
double cellSizeX() const;

View File

@ -426,6 +426,7 @@ ALLOWED_NON_CLASS_ENUMS = [
"QgsNewHttpConnection::ConnectionType",
"QgsNewHttpConnection::Flag",
"QgsNewHttpConnection::WfsVersionIndex",
"QgsNineCellFilter::Result",
"QgsOfflineEditing::ContainerType",
"QgsOfflineEditing::ProgressMode",
"QgsOgcUtils::FilterVersion",

View File

@ -40,8 +40,7 @@ QgsNineCellFilter::QgsNineCellFilter( const QString &inputFile, const QString &o
{
}
// TODO: return an anum instead of an int
int QgsNineCellFilter::processRaster( QgsFeedback *feedback )
QgsNineCellFilter::Result QgsNineCellFilter::processRaster( QgsFeedback *feedback )
{
#ifdef HAVE_OPENCL
if ( QgsOpenClUtils::enabled() && QgsOpenClUtils::available() && !openClProgramBaseName().isEmpty() )
@ -74,7 +73,7 @@ int QgsNineCellFilter::processRaster( QgsFeedback *feedback )
return processRasterCPU( feedback );
}
#ifndef _MSC_VER
return 1;
return InputLayerError;
#endif
#else
return processRasterCPU( feedback );
@ -116,7 +115,6 @@ GDALDriverH QgsNineCellFilter::openOutputDriver()
return outputDriver;
}
gdal::dataset_unique_ptr QgsNineCellFilter::openOutputFile( GDALDatasetH inputDataset, GDALDriverH outputDriver )
{
if ( !inputDataset )
@ -164,8 +162,7 @@ gdal::dataset_unique_ptr QgsNineCellFilter::openOutputFile( GDALDatasetH inputDa
#ifdef HAVE_OPENCL
// TODO: return an anum instead of an int
int QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *feedback )
QgsNineCellFilter::Result QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *feedback )
{
GDALAllRegister();
@ -174,41 +171,41 @@ int QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *fee
const gdal::dataset_unique_ptr inputDataset( openInputFile( xSize, ySize ) );
if ( !inputDataset )
{
return 1; //opening of input file failed
return InputLayerError; //opening of input file failed
}
//output driver
GDALDriverH outputDriver = openOutputDriver();
if ( !outputDriver )
{
return 2;
return DriverError;
}
gdal::dataset_unique_ptr outputDataset( openOutputFile( inputDataset.get(), outputDriver ) );
if ( !outputDataset )
{
return 3; //create operation on output file failed
return CreateOutputError; //create operation on output file failed
}
//open first raster band for reading (operation is only for single band raster)
GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset.get(), 1 );
if ( !rasterBand )
{
return 4;
return InputBandError;
}
mInputNodataValue = GDALGetRasterNoDataValue( rasterBand, nullptr );
GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset.get(), 1 );
if ( !outputRasterBand )
{
return 5;
return OutputBandError;
}
// set nodata value
GDALSetRasterNoDataValue( outputRasterBand, mOutputNodataValue );
if ( ySize < 3 ) //we require at least three rows (should be true for most datasets)
{
return 6;
return RasterSizeError;
}
// Prepare context and queue
@ -331,15 +328,14 @@ int QgsNineCellFilter::processRasterGPU( const QString &source, QgsFeedback *fee
{
//delete the dataset without closing (because it is faster)
gdal::fast_delete_and_close( outputDataset, outputDriver, mOutputFile );
return 7;
return Canceled;
}
return 0;
return Success;
}
#endif
// TODO: return an anum instead of an int
int QgsNineCellFilter::processRasterCPU( QgsFeedback *feedback )
QgsNineCellFilter::Result QgsNineCellFilter::processRasterCPU( QgsFeedback *feedback )
{
GDALAllRegister();
@ -348,41 +344,41 @@ int QgsNineCellFilter::processRasterCPU( QgsFeedback *feedback )
const gdal::dataset_unique_ptr inputDataset( openInputFile( xSize, ySize ) );
if ( !inputDataset )
{
return 1; //opening of input file failed
return InputLayerError; //opening of input file failed
}
//output driver
GDALDriverH outputDriver = openOutputDriver();
if ( !outputDriver )
{
return 2;
return DriverError;
}
gdal::dataset_unique_ptr outputDataset( openOutputFile( inputDataset.get(), outputDriver ) );
if ( !outputDataset )
{
return 3; //create operation on output file failed
return CreateOutputError; //create operation on output file failed
}
//open first raster band for reading (operation is only for single band raster)
GDALRasterBandH rasterBand = GDALGetRasterBand( inputDataset.get(), 1 );
if ( !rasterBand )
{
return 4;
return InputBandError;
}
mInputNodataValue = GDALGetRasterNoDataValue( rasterBand, nullptr );
GDALRasterBandH outputRasterBand = GDALGetRasterBand( outputDataset.get(), 1 );
if ( !outputRasterBand )
{
return 5;
return OutputBandError;
}
// set nodata value
GDALSetRasterNoDataValue( outputRasterBand, mOutputNodataValue );
if ( ySize < 3 ) //we require at least three rows (should be true for most datasets)
{
return 6;
return RasterSizeError;
}
//keep only three scanlines in memory at a time, make room for initial and final nodata
@ -471,7 +467,7 @@ int QgsNineCellFilter::processRasterCPU( QgsFeedback *feedback )
{
//delete the dataset without closing (because it is faster)
gdal::fast_delete_and_close( outputDataset, outputDriver, mOutputFile );
return 7;
return Canceled;
}
return 0;
return Success;
}

View File

@ -37,6 +37,19 @@ class QgsFeedback;
class ANALYSIS_EXPORT QgsNineCellFilter
{
public:
//! Result of the calculation \since QGIS 3.44
enum Result
{
Success = 0, //!< Operation completed successfully
InputLayerError = 1, //!< Error reading input file
DriverError = 2, //!< Could not open the driver for the specified format
CreateOutputError = 3, //!< Error creating output file
InputBandError = 4, //!< Error reading input raster band
OutputBandError = 5, //!< Error reading output raster band
RasterSizeError = 6, //!< Raster height is too small (need at least 3 rows)
Canceled = 7, //!< User canceled calculation
};
//! Constructor that takes input file, output file and output format (GDAL string)
QgsNineCellFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat );
virtual ~QgsNineCellFilter() = default;
@ -44,9 +57,9 @@ class ANALYSIS_EXPORT QgsNineCellFilter
/**
* Starts the calculation, reads from mInputFile and stores the result in mOutputFile
* \param feedback feedback object that receives update and that is checked for cancellation.
* \returns 0 in case of success
* \returns QgsNineCellFilter::Success in case of success or error value on failure.
*/
int processRaster( QgsFeedback *feedback = nullptr );
Result processRaster( QgsFeedback *feedback = nullptr );
double cellSizeX() const { return mCellSizeX; }
void setCellSizeX( double size ) { mCellSizeX = size; }
@ -119,9 +132,9 @@ class ANALYSIS_EXPORT QgsNineCellFilter
/**
* \brief processRasterCPU executes the computation on the CPU
* \param feedback instance of QgsFeedback, to allow for progress monitoring and cancellation
* \return an opaque integer for error codes: 0 in case of success
* \returns QgsNineCellFilter::Success in case of success or error value on failure
*/
int processRasterCPU( QgsFeedback *feedback = nullptr );
Result processRasterCPU( QgsFeedback *feedback = nullptr );
#ifdef HAVE_OPENCL
@ -129,9 +142,9 @@ class ANALYSIS_EXPORT QgsNineCellFilter
* \brief processRasterGPU executes the computation on the GPU
* \param source path to the OpenCL source file
* \param feedback instance of QgsFeedback, to allow for progress monitoring and cancellation
* \return an opaque integer for error codes: 0 in case of success
* \returns QgsNineCellFilter::Success in case of success or error value on failure
*/
int processRasterGPU( const QString &source, QgsFeedback *feedback = nullptr );
Result processRasterGPU( const QString &source, QgsFeedback *feedback = nullptr );
/**
* \brief addExtraRasterParams allow derived classes to add parameters needed

View File

@ -112,8 +112,7 @@ void TestNineCellFilters::_testAlg( const QString &name, bool useOpenCl )
#endif
const QString refFile( referenceFile( name ) );
T ninecellFilter( SRC_FILE, tmpFile, "GTiff" );
const int res = ninecellFilter.processRaster();
QVERIFY( res == 0 );
QCOMPARE( static_cast<int>( ninecellFilter.processRaster() ), 0 );
// Produced file
QgsAlignRaster::RasterInfo out( tmpFile );
@ -131,7 +130,6 @@ void TestNineCellFilters::_testAlg( const QString &name, bool useOpenCl )
// Reference
QgsAlignRaster::RasterInfo ref( refFile );
//qDebug() << "Comparing " << tmpFile << refFile;
_rasterCompare( out, ref );
}
@ -215,8 +213,6 @@ void TestNineCellFilters::_rasterCompare( QgsAlignRaster::RasterInfo &out, QgsAl
const double outVal = out.identify( x, y );
const double refVal = ref.identify( x, y );
const double diff( qAbs( outVal - refVal ) );
//qDebug() << outVal << refVal;
//qDebug() << "Identify " << x << "," << y << " diff " << diff << " check: < " << tolerance;
QVERIFY( diff <= tolerance );
}
}
@ -236,8 +232,7 @@ void TestNineCellFilters::testCreationOptions()
QgsAspectFilter ninecellFilter( SRC_FILE, tmpFile, "GTiff" );
ninecellFilter.setCreationOptions( QStringList() << "TFW=YES" );
const int res = ninecellFilter.processRaster();
QVERIFY( res == 0 );
QCOMPARE( static_cast<int>( ninecellFilter.processRaster() ), 0 );
QVERIFY( worldFile.exists() );
worldFile.remove();
@ -249,8 +244,7 @@ void TestNineCellFilters::testNoDataValue()
QgsAspectFilter ninecellFilter( SRC_FILE, tmpFile, "GTiff" );
ninecellFilter.setOutputNodataValue( -5555.0 );
const int res = ninecellFilter.processRaster();
QVERIFY( res == 0 );
QCOMPARE( static_cast<int>( ninecellFilter.processRaster() ), 0 );
//open output file and check results
const std::unique_ptr<QgsRasterLayer> result = std::make_unique<QgsRasterLayer>( tmpFile, QStringLiteral( "raster" ), QStringLiteral( "gdal" ) );

View File

@ -1185,13 +1185,13 @@ void TestQgsRasterCalculator::testNoDataValue()
tmpFile.close();
QgsRasterCalculator rc( QStringLiteral( "\"landsat@1\" + 2" ), tmpName, QStringLiteral( "GTiff" ), extent, crs, 2, 3, entries, QgsProject::instance()->transformContext() );
rc.setNoDataValue( -9999.0 );
rc.setNoDataValue( -5555.0 );
QCOMPARE( static_cast<int>( rc.processCalculation() ), 0 );
//open output file and check results
const std::unique_ptr<QgsRasterLayer> result = std::make_unique<QgsRasterLayer>( tmpName, QStringLiteral( "raster" ), QStringLiteral( "gdal" ) );
QVERIFY( result->dataProvider()->sourceHasNoDataValue( 1 ) );
QCOMPARE( result->dataProvider()->sourceNoDataValue( 1 ), -9999.0 );
QCOMPARE( result->dataProvider()->sourceNoDataValue( 1 ), -5555.0 );
}