Skip to content

Commit

Permalink
move scale factor to processAlgorithm()
Browse files Browse the repository at this point in the history
  • Loading branch information
root676 authored and nyalldawson committed Apr 12, 2020
1 parent 053e4ed commit 0612721
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/analysis/processing/qgsalgorithmroundrastervalues.cpp
Expand Up @@ -187,23 +187,26 @@ QVariantMap QgsRoundRasterValuesAlgorithm::processAlgorithm( const QVariantMap &
}
else if ( mRoundingDirection == 0 && mDecimalPrecision > -1 )
{
roundedVal = roundUp( val, mDecimalPrecision );
double m = ( val < 0.0 ) ? -1.0 : 1.0;
roundedVal = roundUp( val, m, mDecimalPrecision );
}
else if ( mRoundingDirection == 1 && mDecimalPrecision < 0 )
{
roundedVal = roundNearestBaseN( val, mMultipleOfBaseN );
}
else if ( mRoundingDirection == 1 && mDecimalPrecision > -1 )
{
roundedVal = roundNearest( val, mDecimalPrecision );
double m = ( val < 0.0 ) ? -1.0 : 1.0;
roundedVal = roundNearest( val, m, mDecimalPrecision );
}
else if ( mRoundingDirection == 2 && mDecimalPrecision < 0 )
{
roundedVal = roundDownBaseN( val, mMultipleOfBaseN );
}
else
{
roundedVal = roundDown( val, mDecimalPrecision );
double m = ( val < 0.0 ) ? -1.0 : 1.0;
roundedVal = roundDown( val, m, mDecimalPrecision );
}
//integer values get automatically cast to double when reading and back to int when writing
analysisRasterBlock->setValue( row, column, roundedVal );
Expand All @@ -220,23 +223,20 @@ QVariantMap QgsRoundRasterValuesAlgorithm::processAlgorithm( const QVariantMap &
return outputs;
}

double QgsRoundRasterValuesAlgorithm::roundNearest( double &value, int &decimals )
double QgsRoundRasterValuesAlgorithm::roundNearest( double &value, double &m, int &decimals )
{
double m = ( value < 0.0 ) ? -1.0 : 1.0;
double scaleFactor = std::pow( 10.0, decimals );
return ( std::round( value * m * scaleFactor ) / scaleFactor ) * m;
}

double QgsRoundRasterValuesAlgorithm::roundUp( double &value, int &decimals )
double QgsRoundRasterValuesAlgorithm::roundUp( double &value, double &m, int &decimals )
{
double m = ( value < 0.0 ) ? -1.0 : 1.0;
double scaleFactor = std::pow( 10.0, decimals );
return ( std::ceil( value * m * scaleFactor ) / scaleFactor ) * m;
}

double QgsRoundRasterValuesAlgorithm::roundDown( double &value, int &decimals )
double QgsRoundRasterValuesAlgorithm::roundDown( double &value, double &m, int &decimals )
{
double m = ( value < 0.0 ) ? -1.0 : 1.0;
double scaleFactor = std::pow( 10.0, decimals );
return ( std::floor( value * m * scaleFactor ) / scaleFactor ) * m;
}
Expand Down
6 changes: 3 additions & 3 deletions src/analysis/processing/qgsalgorithmroundrastervalues.h
Expand Up @@ -55,9 +55,9 @@ class QgsRoundRasterValuesAlgorithm : public QgsProcessingAlgorithm
QgsProcessingFeedback *feedback ) override;

private:
double roundNearest( double &value, int &decimals );
double roundUp( double &value, int &decimals );
double roundDown( double &value, int &decimals );
double roundNearest( double &value, double &m, int &decimals );
double roundUp( double &value, double &m, int &decimals );
double roundDown( double &value, double &m, int &decimals );
double roundNearestBaseN( double &value, int &multipleOfBaseN );
double roundUpBaseN( double &value, int &multipleOfBaseN );
double roundDownBaseN( double &value, int &multipleOfBaseN );
Expand Down

0 comments on commit 0612721

Please sign in to comment.