Skip to content

Commit

Permalink
Add transform context to raster file writer task
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso authored and nyalldawson committed Apr 17, 2019
1 parent 6e0bd82 commit 3d687e5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
23 changes: 21 additions & 2 deletions python/core/auto_generated/raster/qgsrasterfilewritertask.sip.in
Expand Up @@ -28,15 +28,34 @@ QGIS interface.
%End
public:

QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe /Transfer/,
int columns, int rows,
const QgsRectangle &outputExtent,
const QgsCoordinateReferenceSystem &crs ) /Deprecated/;
%Docstring
Constructor for QgsRasterFileWriterTask. Takes a source ``writer``,
``columns``, ``rows``, ``outputExtent`` and destination ``crs``.
Ownership of the ``pipe`` is transferred to the writer task, and will
be deleted when the task completes.

.. deprecated:: since QGIS 3.8, use version with transformContext instead
%End


QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe /Transfer/,
int columns, int rows,
const QgsRectangle &outputExtent,
const QgsCoordinateReferenceSystem &crs );
const QgsCoordinateReferenceSystem &crs,
const QgsCoordinateTransformContext &transformContext
);
%Docstring
Constructor for QgsRasterFileWriterTask. Takes a source ``writer``,
``columns``, ``rows``, ``outputExtent`` and destination ``crs``.
``columns``, ``rows``, ``outputExtent``, destination ``crs`` and
coordinate ``transformContext`` .
Ownership of the ``pipe`` is transferred to the writer task, and will
be deleted when the task completes.

.. deprecated:: since QGIS 3.8, use version with transformContext instead
%End

virtual void cancel();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.cpp
Expand Up @@ -7595,7 +7595,7 @@ QString QgisApp::saveAsRasterFile( QgsRasterLayer *rasterLayer, const bool defau
QString outputFormat = d.outputFormat();

QgsRasterFileWriterTask *writerTask = new QgsRasterFileWriterTask( fileWriter, pipe.release(), d.nColumns(), d.nRows(),
d.outputRectangle(), QgsProject::instance()->transformContext(), d.outputCrs() );
d.outputRectangle(), d.outputCrs(), QgsProject::instance()->transformContext() );

// when writer is successful:

Expand Down
21 changes: 16 additions & 5 deletions src/core/raster/qgsrasterfilewritertask.cpp
Expand Up @@ -17,11 +17,20 @@

#include "qgsrasterfilewritertask.h"
#include "qgsrasterinterface.h"
#include "qgsrasterdataprovider.h"

QgsRasterFileWriterTask::QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe,
int columns, int rows,
// Deprecated!
QgsRasterFileWriterTask::QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe, int columns, int rows,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &crs )
: QgsRasterFileWriterTask( writer, pipe, columns, rows, outputExtent, crs,
( pipe && pipe->provider() ) ? pipe->provider()->transformContext() : QgsCoordinateTransformContext() )
{
}

QgsRasterFileWriterTask::QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe, int columns, int rows,
const QgsRectangle &outputExtent,
const QgsCoordinateReferenceSystem &crs )
const QgsCoordinateReferenceSystem &crs,
const QgsCoordinateTransformContext &transformContext )
: QgsTask( tr( "Saving %1" ).arg( writer.outputUrl() ), QgsTask::CanCancel )
, mWriter( writer )
, mRows( rows )
Expand All @@ -30,7 +39,9 @@ QgsRasterFileWriterTask::QgsRasterFileWriterTask( const QgsRasterFileWriter &wri
, mCrs( crs )
, mPipe( pipe )
, mFeedback( new QgsRasterBlockFeedback() )
{}
, mTransformContext( transformContext )
{
}

void QgsRasterFileWriterTask::cancel()
{
Expand All @@ -45,7 +56,7 @@ bool QgsRasterFileWriterTask::run()

connect( mFeedback.get(), &QgsRasterBlockFeedback::progressChanged, this, &QgsRasterFileWriterTask::setProgress );

mError = mWriter.writeRaster( mPipe.get(), mColumns, mRows, mExtent, mCrs, mFeedback.get() );
mError = mWriter.writeRaster( mPipe.get(), mColumns, mRows, mExtent, mCrs, mTransformContext, mFeedback.get() );

return mError == QgsRasterFileWriter::NoError;
}
Expand Down
22 changes: 21 additions & 1 deletion src/core/raster/qgsrasterfilewritertask.h
Expand Up @@ -21,6 +21,7 @@
#include "qgis_core.h"
#include "qgstaskmanager.h"
#include "qgsrasterfilewriter.h"
#include "qgscoordinatetransformcontext.h"
#include "qgsrasterinterface.h"
#include "qgsrasterpipe.h"

Expand All @@ -45,11 +46,28 @@ class CORE_EXPORT QgsRasterFileWriterTask : public QgsTask
* \a columns, \a rows, \a outputExtent and destination \a crs.
* Ownership of the \a pipe is transferred to the writer task, and will
* be deleted when the task completes.
* \deprecated since QGIS 3.8, use version with transformContext instead
*/
Q_DECL_DEPRECATED QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe SIP_TRANSFER,
int columns, int rows,
const QgsRectangle &outputExtent,
const QgsCoordinateReferenceSystem &crs ) SIP_DEPRECATED;


/**
* Constructor for QgsRasterFileWriterTask. Takes a source \a writer,
* \a columns, \a rows, \a outputExtent, destination \a crs and
* coordinate \a transformContext .
* Ownership of the \a pipe is transferred to the writer task, and will
* be deleted when the task completes.
* \deprecated since QGIS 3.8, use version with transformContext instead
*/
QgsRasterFileWriterTask( const QgsRasterFileWriter &writer, QgsRasterPipe *pipe SIP_TRANSFER,
int columns, int rows,
const QgsRectangle &outputExtent,
const QgsCoordinateReferenceSystem &crs );
const QgsCoordinateReferenceSystem &crs,
const QgsCoordinateTransformContext &transformContext
);

void cancel() override;

Expand Down Expand Up @@ -87,6 +105,8 @@ class CORE_EXPORT QgsRasterFileWriterTask : public QgsTask

QgsRasterFileWriter::WriterError mError = QgsRasterFileWriter::NoError;

QgsCoordinateTransformContext mTransformContext;

};

#endif //QGSRASTERFILEWRITERTASK_H
5 changes: 3 additions & 2 deletions tests/src/python/test_qgsrasterfilewritertask.py
Expand Up @@ -17,6 +17,7 @@

from qgis.core import (
QgsApplication,
QgsCoordinateTransformContext,
QgsRasterLayer,
QgsRasterPipe,
QgsRasterFileWriter,
Expand Down Expand Up @@ -57,7 +58,7 @@ def testSuccess(self):
tmp = create_temp_filename('success.tif')
writer = QgsRasterFileWriter(tmp)

task = QgsRasterFileWriterTask(writer, pipe, 100, 100, raster_layer.extent(), raster_layer.crs())
task = QgsRasterFileWriterTask(writer, pipe, 100, 100, raster_layer.extent(), raster_layer.crs(), QgsCoordinateTransformContext())

task.writeComplete.connect(self.onSuccess)
task.errorOccurred.connect(self.onFail)
Expand All @@ -82,7 +83,7 @@ def testLayerRemovalBeforeRun(self):
tmp = create_temp_filename('remove_layer.tif')
writer = QgsRasterFileWriter(tmp)

task = QgsRasterFileWriterTask(writer, pipe, 100, 100, raster_layer.extent(), raster_layer.crs())
task = QgsRasterFileWriterTask(writer, pipe, 100, 100, raster_layer.extent(), raster_layer.crs(), QgsCoordinateTransformContext())

task.writeComplete.connect(self.onSuccess)
task.errorOccurred.connect(self.onFail)
Expand Down

0 comments on commit 3d687e5

Please sign in to comment.