Skip to content

Commit d26d1b7

Browse files
authoredApr 6, 2017
Merge pull request #4336 from nyalldawson/background_raster
[FEATURE] Background saving of raster layers
2 parents c28ad0b + 110828f commit d26d1b7

13 files changed

+413
-79
lines changed
 

‎python/core/core.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@
298298
%Include raster/qgsrasterdataprovider.sip
299299
%Include raster/qgsrasterdrawer.sip
300300
%Include raster/qgsrasterfilewriter.sip
301+
%Include raster/qgsrasterfilewritertask.sip
301302
%Include raster/qgsrasterhistogram.sip
302303
%Include raster/qgsrasteridentifyresult.sip
303304
%Include raster/qgsrasterinterface.sip

‎python/core/raster/qgsrasterfilewriter.sip

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ class QgsRasterFileWriter
4949
@param crs crs to reproject to
5050
@param p dialog to show progress in */
5151
WriterError writeRaster( const QgsRasterPipe* pipe, int nCols, int nRows, const QgsRectangle& outputExtent,
52-
const QgsCoordinateReferenceSystem& crs, QProgressDialog* p = 0 );
52+
const QgsCoordinateReferenceSystem& crs, QgsRasterBlockFeedback *feedback = nullptr );
53+
54+
QString outputUrl() const;
5355

5456
void setOutputFormat( const QString& format );
5557
QString outputFormat() const;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/raster/qgsrasterfilewritertask.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsRasterFileWriterTask : QgsTask
13+
{
14+
%Docstring
15+
QgsTask task which performs a QgsRasterFileWriter layer saving operation as a background
16+
task. This can be used to save a raster layer out to a file without blocking the
17+
QGIS interface.
18+
\see QgsVectorFileWriterTask
19+
.. versionadded:: 3.0
20+
%End
21+
22+
%TypeHeaderCode
23+
#include "qgsrasterfilewritertask.h"
24+
%End
25+
26+
public:
27+
28+
QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe /Transfer/,
29+
int columns, int rows,
30+
const QgsRectangle &outputExtent,
31+
const QgsCoordinateReferenceSystem &crs );
32+
%Docstring
33+
Constructor for QgsRasterFileWriterTask. Takes a source writer,
34+
columns, rows, outputExtent and destination crs.
35+
Ownership of the pipe is transferred to the writer task, and will
36+
be deleted when the task completes.
37+
%End
38+
39+
virtual void cancel();
40+
41+
signals:
42+
43+
void writeComplete( const QString &outputUrl );
44+
%Docstring
45+
Emitted when writing the layer is successfully completed. The outputUrl
46+
parameter indicates the file path for the written file(s).
47+
%End
48+
49+
void errorOccurred( int error );
50+
%Docstring
51+
Emitted when an error occurs which prevented the file being written (or if
52+
the task is canceled). The writing error will be reported.
53+
%End
54+
55+
protected:
56+
57+
virtual bool run();
58+
virtual void finished( bool result );
59+
60+
};
61+
62+
/************************************************************************
63+
* This file has been generated automatically from *
64+
* *
65+
* src/core/raster/qgsrasterfilewritertask.h *
66+
* *
67+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
68+
************************************************************************/

‎src/app/qgisapp.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
#include "qgsquerybuilder.h"
220220
#include "qgsrastercalcdialog.h"
221221
#include "qgsrasterfilewriter.h"
222+
#include "qgsrasterfilewritertask.h"
222223
#include "qgsrasteriterator.h"
223224
#include "qgsrasterlayer.h"
224225
#include "qgsrasterlayerproperties.h"
@@ -6453,13 +6454,6 @@ void QgisApp::saveAsRasterFile()
64536454
fileWriter.setMaxTileHeight( d.maximumTileSizeY() );
64546455
}
64556456

6456-
QProgressDialog pd( QString(), tr( "Abort..." ), 0, 0 );
6457-
// Show the dialo immediately because cloning pipe can take some time (WCS)
6458-
pd.setLabelText( tr( "Reading raster" ) );
6459-
pd.setWindowTitle( tr( "Saving raster" ) );
6460-
pd.show();
6461-
pd.setWindowModality( Qt::WindowModal );
6462-
64636457
// TODO: show error dialogs
64646458
// TODO: this code should go somewhere else, but probably not into QgsRasterFileWriter
64656459
// clone pipe/provider is not really necessary, ready for threads
@@ -6525,35 +6519,51 @@ void QgisApp::saveAsRasterFile()
65256519
fileWriter.setPyramidsFormat( d.pyramidsFormat() );
65266520
fileWriter.setPyramidsConfigOptions( d.pyramidsConfigOptions() );
65276521

6528-
QgsRasterFileWriter::WriterError err = fileWriter.writeRaster( pipe.get(), d.nColumns(), d.nRows(), d.outputRectangle(), d.outputCrs(), &pd );
6529-
if ( err != QgsRasterFileWriter::NoError )
6530-
{
6531-
QMessageBox::warning( this, tr( "Error" ),
6532-
tr( "Cannot write raster error code: %1" ).arg( err ),
6533-
QMessageBox::Ok );
6522+
bool tileMode = d.tileMode();
6523+
bool addToCanvas = d.addToCanvas();
6524+
QPointer< QgsRasterLayer > rlWeakPointer( rasterLayer );
65346525

6535-
}
6536-
else
6526+
QgsRasterFileWriterTask *writerTask = new QgsRasterFileWriterTask( fileWriter, pipe.release(), d.nColumns(), d.nRows(),
6527+
d.outputRectangle(), d.outputCrs() );
6528+
6529+
// when writer is successful:
6530+
6531+
connect( writerTask, &QgsRasterFileWriterTask::writeComplete, this, [this, tileMode, addToCanvas, rlWeakPointer ]( const QString & newFilename )
65376532
{
6538-
QString fileName( d.outputFileName() );
6539-
if ( d.tileMode() )
6533+
QString fileName = newFilename;
6534+
if ( tileMode )
65406535
{
65416536
QFileInfo outputInfo( fileName );
65426537
fileName = QStringLiteral( "%1/%2.vrt" ).arg( fileName, outputInfo.fileName() );
65436538
}
65446539

6545-
if ( d.addToCanvas() )
6540+
if ( addToCanvas )
65466541
{
65476542
addRasterLayers( QStringList( fileName ) );
65486543
}
6544+
if ( rlWeakPointer )
6545+
emit layerSavedAs( rlWeakPointer, fileName );
65496546

6550-
emit layerSavedAs( rasterLayer, fileName );
65516547
messageBar()->pushMessage( tr( "Saving done" ),
65526548
tr( "Export to raster file has been completed" ),
65536549
QgsMessageBar::INFO, messageTimeout() );
6554-
}
6550+
} );
6551+
6552+
// when an error occurs:
6553+
connect( writerTask, &QgsRasterFileWriterTask::errorOccurred, this, [ = ]( int error )
6554+
{
6555+
if ( error != QgsRasterFileWriter::WriteCanceled )
6556+
{
6557+
QMessageBox::warning( this, tr( "Error" ),
6558+
tr( "Cannot write raster error code: %1" ).arg( error ),
6559+
QMessageBox::Ok );
6560+
}
6561+
} );
6562+
6563+
QgsApplication::taskManager()->addTask( writerTask );
65556564
}
65566565

6566+
65576567
void QgisApp::saveAsFile()
65586568
{
65596569
QgsMapLayer *layer = activeLayer();

‎src/core/CMakeLists.txt

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ SET(QGIS_CORE_SRCS
336336
raster/qgsrasterblock.cpp
337337
raster/qgsrasterchecker.cpp
338338
raster/qgsrasterdataprovider.cpp
339+
raster/qgsrasterfilewritertask.cpp
339340
raster/qgsrasteridentifyresult.cpp
340341
raster/qgsrasterinterface.cpp
341342
raster/qgsrasteriterator.cpp
@@ -597,6 +598,7 @@ SET(QGIS_CORE_MOC_HDRS
597598
processing/qgsprocessingfeedback.h
598599
processing/qgsprocessingregistry.h
599600

601+
raster/qgsrasterfilewritertask.h
600602
raster/qgsrasterlayer.h
601603
raster/qgsrasterdataprovider.h
602604

‎src/core/qgsvectorfilewritertask.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020

2121
QgsVectorFileWriterTask::QgsVectorFileWriterTask( QgsVectorLayer *layer, const QString &fileName, const QgsVectorFileWriter::SaveVectorOptions &options )
22-
: QgsTask( tr( "Saving %1 " ).arg( fileName ), QgsTask::CanCancel )
22+
: QgsTask( tr( "Saving %1" ).arg( fileName ), QgsTask::CanCancel )
2323
, mLayer( layer )
2424
, mDestFileName( fileName )
2525
, mOptions( options )

‎src/core/qgsvectorfilewritertask.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
* task. This can be used to save a vector layer out to a file without blocking the
3131
* QGIS interface.
3232
* \since QGIS 3.0
33+
* \see QgsRasterFileWriterTask
3334
*/
3435
class CORE_EXPORT QgsVectorFileWriterTask : public QgsTask
3536
{

0 commit comments

Comments
 (0)
Please sign in to comment.