Skip to content

Commit

Permalink
Expose choice of bounds handling during reclassification
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 13, 2018
1 parent f0f5211 commit 18ddc4e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
39 changes: 35 additions & 4 deletions src/analysis/processing/qgsalgorithmreclassifybylayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ void QgsReclassifyAlgorithmBase::initAlgorithm( const QVariantMap &configuration
QObject::tr( "Output no data value" ), QgsProcessingParameterNumber::Double, -9999 );
noDataValueParam->setFlags( QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( noDataValueParam.release() );

std::unique_ptr< QgsProcessingParameterEnum > boundsHandling = qgis::make_unique< QgsProcessingParameterEnum >( QStringLiteral( "RANGE_BOUNDARIES" ),
QObject::tr( "Range boundaries" ), QStringList() << QObject::tr( "min < value <= max" )
<< QObject::tr( "min <= value < max" )
<< QObject::tr( "min <= value <= max" )
<< QObject::tr( "min < value < max" ),
false, 0 );
boundsHandling->setFlags( QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( boundsHandling.release() );

addParameter( new QgsProcessingParameterRasterDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Reclassified raster" ) ) );
}

Expand All @@ -71,6 +81,27 @@ bool QgsReclassifyAlgorithmBase::prepareAlgorithm( const QVariantMap &parameters

mNoDataValue = parameterAsDouble( parameters, QStringLiteral( "NO_DATA" ), context );


int boundsType = parameterAsEnum( parameters, QStringLiteral( "RANGE_BOUNDARIES" ), context );
switch ( boundsType )
{
case 0:
mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMax;
break;

case 1:
mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMin;
break;

case 2:
mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMinAndMax;
break;

case 3:
mBoundsType = QgsReclassifyUtils::RasterClass::Exclusive;
break;
}

return _prepareAlgorithm( parameters, context, feedback );
}

Expand Down Expand Up @@ -167,7 +198,7 @@ bool QgsReclassifyByLayerAlgorithm::_prepareAlgorithm( const QVariantMap &parame
return true;
}

QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByLayerAlgorithm::createClasses( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByLayerAlgorithm::createClasses( QgsRasterRange::BoundsType boundsType, const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
{
QVector< QgsReclassifyUtils::RasterClass > classes;
QgsFeature f;
Expand All @@ -184,7 +215,7 @@ QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByLayerAlgorithm::createCl
if ( !ok )
throw QgsProcessingException( QObject::tr( "Invalid output value: %1" ).arg( f.attribute( mValueFieldIdx ).toString() ) );

classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, QgsRasterRange::IncludeMax, value );
classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, boundsType, value );
}
return classes;
}
Expand Down Expand Up @@ -233,7 +264,7 @@ bool QgsReclassifyByTableAlgorithm::_prepareAlgorithm( const QVariantMap &, QgsP
return true;
}

QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByTableAlgorithm::createClasses( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByTableAlgorithm::createClasses( QgsReclassifyUtils::RasterClass::BoundsType boundsType, const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
const QVariantList table = parameterAsMatrix( parameters, QStringLiteral( "TABLE" ), context );
int rows = table.count() / 3;
Expand All @@ -251,7 +282,7 @@ QVector<QgsReclassifyUtils::RasterClass> QgsReclassifyByTableAlgorithm::createCl
if ( !ok )
throw QgsProcessingException( QObject::tr( "Invalid output value: %1" ).arg( table.at( row * 3 + 2 ).toString() ) );

classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, QgsRasterRange::IncludeMax, value );
classes << QgsReclassifyUtils::RasterClass( minValue, maxValue, boundsType, value );
}
return classes;
}
Expand Down
9 changes: 7 additions & 2 deletions src/analysis/processing/qgsalgorithmreclassifybylayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class QgsReclassifyAlgorithmBase : public QgsProcessingAlgorithm
/**
* Returns a list of classes to use during the reclassification.
*/
virtual QVector< QgsReclassifyUtils::RasterClass > createClasses( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;
virtual QVector< QgsReclassifyUtils::RasterClass > createClasses(
QgsReclassifyUtils::RasterClass::BoundsType boundsType,
const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) = 0;

QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override final;
Expand All @@ -70,6 +72,7 @@ class QgsReclassifyAlgorithmBase : public QgsProcessingAlgorithm
double mRasterUnitsPerPixelY = 0;
int mNbCellsXProvider = 0;
int mNbCellsYProvider = 0;
QgsReclassifyUtils::RasterClass::BoundsType mBoundsType = QgsReclassifyUtils::RasterClass::IncludeMax;
};

/**
Expand All @@ -90,7 +93,9 @@ class QgsReclassifyByLayerAlgorithm : public QgsReclassifyAlgorithmBase
protected:
void addAlgorithmParams() override;
bool _prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVector< QgsReclassifyUtils::RasterClass > createClasses( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVector< QgsReclassifyUtils::RasterClass > createClasses(
QgsReclassifyUtils::RasterClass::BoundsType boundsType,
const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;

private:
int mMinFieldIdx = -1;
Expand Down

0 comments on commit 18ddc4e

Please sign in to comment.