Skip to content

Commit 787dd34

Browse files
committed
[processing] More helpful errors when raster inputs are not valid
1 parent bddcb7d commit 787dd34

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

python/core/processing/qgsprocessingalgorithm.sip.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,23 @@ should correspond to the invalid source parameter name.
756756

757757
.. versionadded:: 3.2
758758

759+
.. seealso:: :py:func:`invalidRasterError`
760+
761+
.. seealso:: :py:func:`invalidSinkError`
762+
%End
763+
764+
static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
765+
%Docstring
766+
Returns a user-friendly string to use as an error when a raster layer input could
767+
not be loaded.
768+
769+
The ``parameters`` argument should give the algorithms parameter map, and the ``name``
770+
should correspond to the invalid source parameter name.
771+
772+
.. versionadded:: 3.2
773+
774+
.. seealso:: :py:func:`invalidSourceError`
775+
759776
.. seealso:: :py:func:`invalidSinkError`
760777
%End
761778

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

772789
.. seealso:: :py:func:`invalidSourceError`
790+
791+
.. seealso:: :py:func:`invalidRasterError`
773792
%End
774793

775794
private:

src/analysis/processing/qgsalgorithmrasterlayeruniquevalues.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool QgsRasterLayerUniqueValuesReportAlgorithm::prepareAlgorithm( const QVariant
7777
int band = parameterAsInt( parameters, QStringLiteral( "BAND" ), context );
7878

7979
if ( !layer )
80-
return false;
80+
throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT" ) ) );
8181

8282
mInterface.reset( layer->dataProvider()->clone() );
8383
mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( band );

src/analysis/processing/qgsalgorithmzonalhistogram.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool QgsZonalHistogramAlgorithm::prepareAlgorithm( const QVariantMap &parameters
7777
mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context );
7878

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

8282
mInterface.reset( layer->dataProvider()->clone() );
8383
mHasNoDataValue = layer->dataProvider()->sourceHasNoDataValue( mBand );

src/core/processing/qgsprocessingalgorithm.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ bool QgsProcessingAlgorithm::checkParameterValues( const QVariantMap &parameters
9898
*message = invalidSourceError( parameters, def->name() );
9999
else if ( def->type() == QgsProcessingParameterFeatureSink::typeName() )
100100
*message = invalidSinkError( parameters, def->name() );
101+
else if ( def->type() == QgsProcessingParameterRasterLayer::typeName() )
102+
*message = invalidRasterError( parameters, def->name() );
101103
else
102104
*message = QObject::tr( "Incorrect parameter value for %1" ).arg( def->name() );
103105
}
@@ -695,6 +697,28 @@ QString QgsProcessingAlgorithm::invalidSourceError( const QVariantMap &parameter
695697
}
696698
}
697699

700+
QString QgsProcessingAlgorithm::invalidRasterError( const QVariantMap &parameters, const QString &name )
701+
{
702+
if ( !parameters.contains( name ) )
703+
return QObject::tr( "Could not load source layer for %1: no value specified for parameter" ).arg( name );
704+
else
705+
{
706+
QVariant var = parameters.value( name );
707+
if ( var.canConvert<QgsProperty>() )
708+
{
709+
QgsProperty p = var.value< QgsProperty >();
710+
if ( p.propertyType() == QgsProperty::StaticProperty )
711+
{
712+
var = p.staticValue();
713+
}
714+
}
715+
if ( !var.toString().isEmpty() )
716+
return QObject::tr( "Could not load source layer for %1: %2 not found" ).arg( name, var.toString() );
717+
else
718+
return QObject::tr( "Could not load source layer for %1: invalid value" ).arg( name );
719+
}
720+
}
721+
698722
QString QgsProcessingAlgorithm::invalidSinkError( const QVariantMap &parameters, const QString &name )
699723
{
700724
if ( !parameters.contains( name ) )

src/core/processing/qgsprocessingalgorithm.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,25 @@ class CORE_EXPORT QgsProcessingAlgorithm
753753
*
754754
* \since QGIS 3.2
755755
*
756+
* \see invalidRasterError()
756757
* \see invalidSinkError()
757758
*/
758759
static QString invalidSourceError( const QVariantMap &parameters, const QString &name );
759760

761+
/**
762+
* Returns a user-friendly string to use as an error when a raster layer input could
763+
* not be loaded.
764+
*
765+
* The \a parameters argument should give the algorithms parameter map, and the \a name
766+
* should correspond to the invalid source parameter name.
767+
*
768+
* \since QGIS 3.2
769+
*
770+
* \see invalidSourceError()
771+
* \see invalidSinkError()
772+
*/
773+
static QString invalidRasterError( const QVariantMap &parameters, const QString &name );
774+
760775
/**
761776
* Returns a user-friendly string to use as an error when a sink parameter could
762777
* not be created.
@@ -767,6 +782,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
767782
* \since QGIS 3.2
768783
*
769784
* \see invalidSourceError()
785+
* \see invalidRasterError()
770786
*/
771787
static QString invalidSinkError( const QVariantMap &parameters, const QString &name );
772788

tests/src/analysis/testqgsprocessing.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,16 @@ void TestQgsProcessing::parameterRasterLayer()
34233423
// optional with direct layer
34243424
def.reset( new QgsProcessingParameterRasterLayer( "optional", QString(), QVariant::fromValue( r1 ), true ) );
34253425
QCOMPARE( QgsProcessingParameters::parameterAsRasterLayer( def.get(), params, context )->id(), r1->id() );
3426+
3427+
// invalidRasterError
3428+
params.clear();
3429+
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "MISSING" ) ), QStringLiteral( "Could not load source layer for MISSING: no value specified for parameter" ) );
3430+
params.insert( QStringLiteral( "INPUT" ), QStringLiteral( "my layer" ) );
3431+
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "INPUT" ) ), QStringLiteral( "Could not load source layer for INPUT: my layer not found" ) );
3432+
params.insert( QStringLiteral( "INPUT" ), QgsProperty::fromValue( "my prop layer" ) );
3433+
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "INPUT" ) ), QStringLiteral( "Could not load source layer for INPUT: my prop layer not found" ) );
3434+
params.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( v1 ) );
3435+
QCOMPARE( QgsProcessingAlgorithm::invalidRasterError( params, QStringLiteral( "INPUT" ) ), QStringLiteral( "Could not load source layer for INPUT: invalid value" ) );
34263436
}
34273437

34283438
void TestQgsProcessing::parameterEnum()

0 commit comments

Comments
 (0)