Skip to content
Permalink
Browse files

fix cell statistics median method and add tests

  • Loading branch information
root676 authored and nyalldawson committed Nov 6, 2020
1 parent 88ac3ce commit 608d717ab005095b0492deca5b8f99a5f02ab726
@@ -329,14 +329,14 @@ double QgsRasterAnalysisUtils::meanFromCellValues( std::vector<double> cellValue
double QgsRasterAnalysisUtils::medianFromCellValues( std::vector<double> cellValues, int stackSize )
{
std::sort( cellValues.begin(), cellValues.end() );
int medianElementIdx = stackSize / 2;
if ( stackSize % 2 == 0 )
bool even = ( stackSize % 2 ) < 1;
if ( even )
{
return ( cellValues[medianElementIdx] + cellValues[medianElementIdx + 1] ) / 2.0;
return ( cellValues[stackSize / 2 - 1] + cellValues[stackSize / 2] ) / 2.0;
}
else
else //odd
{
return cellValues[medianElementIdx];
return cellValues[( stackSize + 1 ) / 2 - 1];
}
}

@@ -2326,6 +2326,17 @@ void TestQgsProcessingAlgs::cellStatistics_data()
<< QStringLiteral( "/cellstatistics_variance_result_float32.tif" )
<< Qgis::Float32;

/*
* Testcase 17: median with even number of layers
*/
QTest::newRow( "testcase_17" )
<< QStringList( {"/raster/statisticsRas1_float64.asc", "/raster/statisticsRas1_float64.asc", "/raster/statisticsRas2_float64.asc", "/raster/statisticsRas3_float64.asc"} )
<< QStringLiteral( "/raster/statisticsRas1_float64.asc" )
<< 3
<< false
<< QStringLiteral( "/cellstatistics_median_result_fourLayers.tif" )
<< Qgis::Float64;

}

void TestQgsProcessingAlgs::cellStatistics()
Binary file not shown.

0 comments on commit 608d717

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