Skip to content
Permalink
Browse files

[processing] More helpful errors when raster inputs are not valid

  • Loading branch information
nyalldawson committed May 6, 2018
1 parent bddcb7d commit 787dd3413e471cf0729676c1218878c08b7d3408
@@ -756,6 +756,23 @@ should correspond to the invalid source parameter name.

.. versionadded:: 3.2

.. seealso:: :py:func:`invalidRasterError`

.. seealso:: :py:func:`invalidSinkError`
%End

static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
%Docstring
Returns a user-friendly string to use as an error when a raster layer input could
not be loaded.

The ``parameters`` argument should give the algorithms parameter map, and the ``name``
should correspond to the invalid source parameter name.

.. versionadded:: 3.2

.. seealso:: :py:func:`invalidSourceError`

.. seealso:: :py:func:`invalidSinkError`
%End

@@ -770,6 +787,8 @@ should correspond to the invalid source parameter name.
.. versionadded:: 3.2

.. seealso:: :py:func:`invalidSourceError`

.. seealso:: :py:func:`invalidRasterError`
%End

private:
@@ -77,7 +77,7 @@ bool QgsRasterLayerUniqueValuesReportAlgorithm::prepareAlgorithm( const QVariant
int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );

if ( !layer )
return false;
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );

mInterface.reset( layer->dataProvider()->clone() );
mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );
@@ -77,7 +77,7 @@ bool QgsZonalHistogramAlgorithm::prepareAlgorithm( const QVariantMap &parameters
mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );

if ( !layer )
throw QgsProcessingException( QObject::tr( "Could not load raster layer" ) );
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) );

mInterface.reset( layer->dataProvider()->clone() );
mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( mBand );
@@ -98,6 +98,8 @@ bool QgsProcessingAlgorithm::checkParameterValues( const QVariantMap &parameters
*message = invalidSourceError( parameters, def->name() );
else if ( def->type() == QgsProcessingParameterFeatureSink::typeName() )
*message = invalidSinkError( parameters, def->name() );
else if ( def->type() == QgsProcessingParameterRasterLayer::typeName() )
*message = invalidRasterError( parameters, def->name() );
else
*message = QObject::tr( "Incorrect parameter value for %1" ).arg( def->name() );
}
@@ -695,6 +697,28 @@ QString QgsProcessingAlgorithm::invalidSourceError( const QVariantMap &parameter
}
}

QString QgsProcessingAlgorithm::invalidRasterError( const QVariantMap &parameters, const QString &name )
{
if ( !parameters.contains( name ) )
return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
else
{
QVariant var = parameters.value( name );
if ( var.canConvert<QgsProperty>() )
{
QgsProperty p = var.value< QgsProperty >();
if ( p.propertyType() == QgsProperty::StaticProperty )
{
var = p.staticValue();
}
}
if ( !var.toString().isEmpty() )
return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
else
return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
}
}

QString QgsProcessingAlgorithm::invalidSinkError( const QVariantMap &parameters, const QString &name )
{
if ( !parameters.contains( name ) )
@@ -753,10 +753,25 @@ class CORE_EXPORT QgsProcessingAlgorithm
*
* \since QGIS 3.2
*
* \see invalidRasterError()
* \see invalidSinkError()
*/
static QString invalidSourceError( const QVariantMap &parameters, const QString &name );

/**
* Returns a user-friendly string to use as an error when a raster layer input could
* not be loaded.
*
* The \a parameters argument should give the algorithms parameter map, and the \a name
* should correspond to the invalid source parameter name.
*
* \since QGIS 3.2
*
* \see invalidSourceError()
* \see invalidSinkError()
*/
static QString invalidRasterError( const QVariantMap &parameters, const QString &name );

/**
* Returns a user-friendly string to use as an error when a sink parameter could
* not be created.
@@ -767,6 +782,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
* \since QGIS 3.2
*
* \see invalidSourceError()
* \see invalidRasterError()
*/
static QString invalidSinkError( const QVariantMap &parameters, const QString &name );

@@ -3423,6 +3423,16 @@ void TestQgsProcessing::parameterRasterLayer()
// optional with direct layer
def.reset( new QgsProcessingParameterRasterLayer( "optional", QString(), QVariant::fromValue( r1 ), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );

// invalidRasterError
params.clear();
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "MISSING" ) ), QStringLiteral( "Could not load source layer for MISSING: no value specified for parameter" ) );
params.insert( QStringLiteral( "INPUT" ), QStringLiteral( "my layer" ) );
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "INPUT" ) ), QStringLiteral( "Could not load source layer for INPUT: my layer not found" ) );
params.insert( QStringLiteral( "INPUT" ), QgsProperty::fromValue( "my prop layer" ) );
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "INPUT" ) ), QStringLiteral( "Could not load source layer for INPUT: my prop layer not found" ) );
params.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( v1 ) );
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "INPUT" ) ), QStringLiteral( "Could not load source layer for INPUT: invalid value" ) );
}

void TestQgsProcessing::parameterEnum()

0 comments on commit 787dd34

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