1
0
mirror of https://github.com/qgis/QGIS.git synced 2025-04-29 00:03:59 -04:00

QgsRasterFileWriter: improve error reporting

This commit is contained in:
Even Rouault 2019-09-15 23:48:25 +02:00 committed by Nyall Dawson
parent 7e3c9d98e2
commit 5e70084d32
5 changed files with 68 additions and 3 deletions

@ -71,6 +71,17 @@ parameter indicates the file path for the written file(s).
%Docstring %Docstring
Emitted when an error occurs which prevented the file being written (or if Emitted when an error occurs which prevented the file being written (or if
the task is canceled). The writing ``error`` will be reported. the task is canceled). The writing ``error`` will be reported.
.. deprecated:: since QGIS 3.10. Use errorOccurred(int, const QString&)
%End
void errorOccurred( int error, const QString &errorMessage );
%Docstring
Emitted when an error occurs which prevented the file being written (or if
the task is canceled). The writing ``error`` will be reported and a
``errorMessage`` will be potentially set.
.. versionadded:: 3.10
%End %End
protected: protected:

@ -7816,12 +7816,24 @@ QString QgisApp::saveAsRasterFile( QgsRasterLayer *rasterLayer, const bool defau
} ); } );
// when an error occurs: // when an error occurs:
connect( writerTask, &QgsRasterFileWriterTask::errorOccurred, this, [ = ]( int error ) connect( writerTask, qgis::overload< int, const QString &>::of( &QgsRasterFileWriterTask::errorOccurred ), this, [ = ]( int error, const QString & errorMessage )
{ {
if ( error != QgsRasterFileWriter::WriteCanceled ) if ( error != QgsRasterFileWriter::WriteCanceled )
{ {
QString errorCodeStr;
if ( error == QgsRasterFileWriter::SourceProviderError )
errorCodeStr = tr( "source provider" );
else if ( error == QgsRasterFileWriter::DestProviderError )
errorCodeStr = tr( "destination provider" );
else if ( error == QgsRasterFileWriter::CreateDatasourceError )
errorCodeStr = tr( "data source creation" );
else if ( error == QgsRasterFileWriter::WriteError )
errorCodeStr = tr( "write error" );
QString fullErrorMsg( tr( "Cannot write raster. Error code: %1" ).arg( errorCodeStr ) );
if ( !errorMessage.isEmpty() )
fullErrorMsg += "\n" + errorMessage;
QMessageBox::warning( this, tr( "Save Raster" ), QMessageBox::warning( this, tr( "Save Raster" ),
tr( "Cannot write raster. Error code: %1" ).arg( error ), fullErrorMsg,
QMessageBox::Ok ); QMessageBox::Ok );
} }
} ); } );

@ -323,16 +323,28 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeDataRaster( const Qgs
} }
if ( !destProvider->isValid() ) if ( !destProvider->isValid() )
{ {
if ( feedback && !destProvider->error().isEmpty() )
{
feedback->appendError( destProvider->error().summary() );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
if ( nCols != destProvider->xSize() || nRows != destProvider->ySize() ) if ( nCols != destProvider->xSize() || nRows != destProvider->ySize() )
{ {
QgsDebugMsg( QStringLiteral( "Created raster does not have requested dimensions" ) ); QgsDebugMsg( QStringLiteral( "Created raster does not have requested dimensions" ) );
if ( feedback )
{
feedback->appendError( QObject::tr( "Created raster does not have requested dimensions" ) );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
if ( nBands != destProvider->bandCount() ) if ( nBands != destProvider->bandCount() )
{ {
QgsDebugMsg( QStringLiteral( "Created raster does not have requested band count" ) ); QgsDebugMsg( QStringLiteral( "Created raster does not have requested band count" ) );
if ( feedback )
{
feedback->appendError( QObject::tr( "Created raster does not have requested band count" ) );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
if ( nBands ) if ( nBands )
@ -583,21 +595,37 @@ QgsRasterFileWriter::WriterError QgsRasterFileWriter::writeImageRaster( QgsRaste
} }
if ( !destProvider->isValid() ) if ( !destProvider->isValid() )
{ {
if ( feedback && !destProvider->error().isEmpty() )
{
feedback->appendError( destProvider->error().summary() );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
if ( nCols != destProvider->xSize() || nRows != destProvider->ySize() ) if ( nCols != destProvider->xSize() || nRows != destProvider->ySize() )
{ {
QgsDebugMsg( QStringLiteral( "Created raster does not have requested dimensions" ) ); QgsDebugMsg( QStringLiteral( "Created raster does not have requested dimensions" ) );
if ( feedback )
{
feedback->appendError( QObject::tr( "Created raster does not have requested dimensions" ) );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
if ( nOutputBands != destProvider->bandCount() ) if ( nOutputBands != destProvider->bandCount() )
{ {
QgsDebugMsg( QStringLiteral( "Created raster does not have requested band count" ) ); QgsDebugMsg( QStringLiteral( "Created raster does not have requested band count" ) );
if ( feedback )
{
feedback->appendError( QObject::tr( "Created raster does not have requested band count" ) );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
if ( Qgis::Byte != destProvider->dataType( 1 ) ) if ( Qgis::Byte != destProvider->dataType( 1 ) )
{ {
QgsDebugMsg( QStringLiteral( "Created raster does not have requested data type" ) ); QgsDebugMsg( QStringLiteral( "Created raster does not have requested data type" ) );
if ( feedback )
{
feedback->appendError( QObject::tr( "Created raster does not have requested data type" ) );
}
return CreateDatasourceError; return CreateDatasourceError;
} }
} }

@ -66,7 +66,13 @@ void QgsRasterFileWriterTask::finished( bool result )
if ( result ) if ( result )
emit writeComplete( mWriter.outputUrl() ); emit writeComplete( mWriter.outputUrl() );
else else
{
emit errorOccurred( mError ); emit errorOccurred( mError );
QString errorMsg;
if ( !mFeedback->errors().isEmpty() )
errorMsg = mFeedback->errors().front();
emit errorOccurred( mError, errorMsg );
}
} }

@ -81,9 +81,18 @@ class CORE_EXPORT QgsRasterFileWriterTask : public QgsTask
/** /**
* Emitted when an error occurs which prevented the file being written (or if * Emitted when an error occurs which prevented the file being written (or if
* the task is canceled). The writing \a error will be reported. * the task is canceled). The writing \a error will be reported.
* \deprecated since QGIS 3.10. Use errorOccurred(int, const QString&)
*/ */
void errorOccurred( int error ); void errorOccurred( int error );
/**
* Emitted when an error occurs which prevented the file being written (or if
* the task is canceled). The writing \a error will be reported and a
* \a errorMessage will be potentially set.
* \since QGIS 3.10
*/
void errorOccurred( int error, const QString &errorMessage );
protected: protected:
bool run() override; bool run() override;
@ -105,7 +114,6 @@ class CORE_EXPORT QgsRasterFileWriterTask : public QgsTask
QgsRasterFileWriter::WriterError mError = QgsRasterFileWriter::NoError; QgsRasterFileWriter::WriterError mError = QgsRasterFileWriter::NoError;
QgsCoordinateTransformContext mTransformContext; QgsCoordinateTransformContext mTransformContext;
}; };
#endif //QGSRASTERFILEWRITERTASK_H #endif //QGSRASTERFILEWRITERTASK_H