Skip to content
Permalink
Browse files

add Equal to frequency processing test

  • Loading branch information
root676 authored and nyalldawson committed Jul 28, 2020
1 parent 175f749 commit 19702f47db36d21280e427adde94061f626ba541
Showing with 148 additions and 0 deletions.
  1. +148 −0 tests/src/analysis/testqgsprocessingalgs.cpp
@@ -102,6 +102,8 @@ class TestQgsProcessingAlgs: public QObject
void rasterLogicOp();
void cellStatistics_data();
void cellStatistics();
void equalToFrequency_data();
void equalToFrequency();
void roundRasterValues_data();
void roundRasterValues();

@@ -2267,6 +2269,152 @@ void TestQgsProcessingAlgs::cellStatistics()
}
}

void TestQgsProcessingAlgs::equalToFrequency_data()
{
QTest::addColumn<QString>( "inputValueRaster" );
QTest::addColumn<int>( "inputValueRasterBand" );
QTest::addColumn<QStringList>( "inputRasters" );
QTest::addColumn<bool>( "ignoreNoData" );
QTest::addColumn<QString>( "expectedRaster" );
QTest::addColumn<Qgis::DataType>( "expectedDataType" );

/*
* Testcase 1: don't ignore NoData
*/
QTest::newRow( "testcase_1" )
<< QStringLiteral( "/raster/valueRas1_float64.asc" )
<< 1
<< QStringList( {"/raster/statisticsRas1_float64.asc", "/raster/statisticsRas2_float64.asc", "/raster/statisticsRas3_float64.asc"} )
<< false
<< QStringLiteral( "/equalToFrequencyTest1.tif" )
<< Qgis::Int32;
/*
* Testcase 2: ignore NoData
*/
QTest::newRow( "testcase_2" )
<< QStringLiteral( "/raster/valueRas1_float64.asc" )
<< 1
<< QStringList( {"/raster/statisticsRas1_float64.asc", "/raster/statisticsRas2_float64.asc", "/raster/statisticsRas3_float64.asc"} )
<< true
<< QStringLiteral( "/equalToFrequencyTest2.tif" )
<< Qgis::Int32;

/*
* Testcase 3: NoData in value raster
*/
QTest::newRow( "testcase_3" )
<< QStringLiteral( "/raster/valueRas2_float64.asc" )
<< 1
<< QStringList( {"/raster/statisticsRas1_float64.asc", "/raster/statisticsRas2_float64.asc", "/raster/statisticsRas3_float64.asc"} )
<< false
<< QStringLiteral( "/equalToFrequencyTest3.tif" )
<< Qgis::Int32;

/*
* Testcase 4: test with random byte raster
*/
QTest::newRow( "testcase_4" )
<< QStringLiteral( "/raster/valueRas3_float64.asc" )
<< 1
<< QStringList( {"/raster/statisticsRas1_float64.asc", "/raster/statisticsRas2_float64.asc", "/raster/statisticsRas3_float64.asc"} )
<< false
<< QStringLiteral( "/equalToFrequencyTest4.tif" )
<< Qgis::Int32;
}

void TestQgsProcessingAlgs::equalToFrequency()
{
QFETCH( QString, inputValueRaster );
QFETCH( int, inputValueRasterBand );
QFETCH( QStringList, inputRasters );
QFETCH( bool, ignoreNoData );
QFETCH( QString, expectedRaster );
QFETCH( Qgis::DataType, expectedDataType );

//prepare input params
QgsProject p;
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:equaltofrequency" ) ) );

QString myDataPath( TEST_DATA_DIR ); //defined in CmakeLists.txt

QStringList inputDatasetPaths;

for ( const auto &raster : inputRasters )
{
inputDatasetPaths << myDataPath + raster;
}

std::unique_ptr<QgsRasterLayer> inputRasterLayer1 = qgis::make_unique< QgsRasterLayer >( myDataPath + inputRasters[0], "inputDataset", "gdal" );

//set project crs and ellipsoid from input layer
p.setCrs( inputRasterLayer1->crs(), true );

//set project after layer has been added so that transform context/ellipsoid from crs is also set
std::unique_ptr< QgsProcessingContext > context = qgis::make_unique< QgsProcessingContext >();
context->setProject( &p );

QVariantMap parameters;

parameters.insert( QStringLiteral( "INPUT_VALUE_RASTER" ), myDataPath + inputValueRaster );
parameters.insert( QStringLiteral( "INPUT_VALUE_RASTER_BAND"), inputValueRasterBand);
parameters.insert( QStringLiteral( "INPUT_RASTERS"), inputDatasetPaths);
parameters.insert( QStringLiteral( "IGNORE_NODATA" ), ignoreNoData );
parameters.insert( QStringLiteral( "OUTPUT" ), QgsProcessing::TEMPORARY_OUTPUT );

//prepare expectedRaster
std::unique_ptr<QgsRasterLayer> expectedRasterLayer = qgis::make_unique< QgsRasterLayer >( myDataPath + "/control_images/expected_equalToFrequency/" + expectedRaster, "expectedDataset", "gdal" );
std::unique_ptr< QgsRasterInterface > expectedInterface( expectedRasterLayer->dataProvider()->clone() );
QgsRasterIterator expectedIter( expectedInterface.get() );
expectedIter.startRasterRead( 1, expectedRasterLayer->width(), expectedRasterLayer->height(), expectedInterface->extent() );

//run alg...

bool ok = false;
QgsProcessingFeedback feedback;
QVariantMap results;

results = alg->run( parameters, *context, &feedback, &ok );
QVERIFY( ok );

//...and check results with expected datasets
std::unique_ptr<QgsRasterLayer> outputRaster = qgis::make_unique< QgsRasterLayer >( results.value( QStringLiteral( "OUTPUT" ) ).toString(), "output", "gdal" );
std::unique_ptr< QgsRasterInterface > outputInterface( outputRaster->dataProvider()->clone() );

QCOMPARE( outputInterface->dataType( 1 ), expectedDataType );
QCOMPARE( outputRaster->width(), expectedRasterLayer->width() );
QCOMPARE( outputRaster->height(), expectedRasterLayer->height() );

QgsRasterIterator outputIter( outputInterface.get() );
outputIter.startRasterRead( 1, outputRaster->width(), outputRaster->height(), outputInterface->extent() );
int outputIterLeft = 0;
int outputIterTop = 0;
int outputIterCols = 0;
int outputIterRows = 0;
int expectedIterLeft = 0;
int expectedIterTop = 0;
int expectedIterCols = 0;
int expectedIterRows = 0;

std::unique_ptr< QgsRasterBlock > outputRasterBlock;
std::unique_ptr< QgsRasterBlock > expectedRasterBlock;

while ( outputIter.readNextRasterPart( 1, outputIterCols, outputIterRows, outputRasterBlock, outputIterLeft, outputIterTop ) &&
expectedIter.readNextRasterPart( 1, expectedIterCols, expectedIterRows, expectedRasterBlock, expectedIterLeft, expectedIterTop ) )
{
for ( int row = 0; row < expectedIterRows; row++ )
{
for ( int column = 0; column < expectedIterCols; column++ )
{
double expectedValue = expectedRasterBlock->value( row, column );
double outputValue = outputRasterBlock->value( row, column );
QCOMPARE( outputValue, expectedValue );

Qgis::DataType outputDataType = outputRasterBlock->dataType();
QCOMPARE( outputDataType, expectedDataType);
}
}
}
}

void TestQgsProcessingAlgs::roundRasterValues_data()
{

0 comments on commit 19702f4

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