Skip to content
Permalink
Browse files

Pass transform context to raster calculator

  • Loading branch information
elpaso authored and nyalldawson committed Apr 17, 2019
1 parent 4e747d2 commit ac04339434e4ed0dc4834c9e395fa05c88c501aa
@@ -65,8 +65,11 @@ Performs raster layer calculations.
BandError,
};


QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries );
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows,
const QVector<QgsRasterCalculatorEntry> &rasterEntries,
const QgsCoordinateTransformContext &transformContext );
%Docstring
QgsRasterCalculator constructor.

@@ -77,10 +80,18 @@ QgsRasterCalculator constructor.
:param nOutputColumns: number of columns in output raster
:param nOutputRows: number of rows in output raster
:param rasterEntries: list of referenced raster layers
:param transformContext: coordinate transformation context

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

.. versionadded:: 3.8
%End

QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries );
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs,
int nOutputColumns, int nOutputRows,
const QVector<QgsRasterCalculatorEntry> &rasterEntries,
const QgsCoordinateTransformContext &transformContext );
%Docstring
QgsRasterCalculator constructor.

@@ -92,6 +103,45 @@ QgsRasterCalculator constructor.
:param nOutputColumns: number of columns in output raster
:param nOutputRows: number of rows in output raster
:param rasterEntries: list of referenced raster layers
:param transformContext: coordinate transformation context

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

.. versionadded:: 3.8
%End


QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries ) /Deprecated/;
%Docstring
QgsRasterCalculator constructor.

:param formulaString: formula for raster calculation
:param outputFile: output file path
:param outputFormat: output file format
:param outputExtent: output extent. CRS for output is taken from first entry in rasterEntries.
:param nOutputColumns: number of columns in output raster
:param nOutputRows: number of rows in output raster
:param rasterEntries: list of referenced raster layers

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

QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries ) /Deprecated/;
%Docstring
QgsRasterCalculator constructor.

:param formulaString: formula for raster calculation
:param outputFile: output file path
:param outputFormat: output file format
:param outputExtent: output extent, CRS is specified by outputCrs parameter
:param outputCrs: destination CRS for output raster
:param nOutputColumns: number of columns in output raster
:param nOutputRows: number of rows in output raster
:param rasterEntries: list of referenced raster layers

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

.. versionadded:: 2.10
%End
@@ -184,7 +184,8 @@ def _cellsize(layer):
crs,
width,
height,
entries)
entries,
context.transformContext())

res = calc.processCalculation(feedback)
if res == QgsRasterCalculator.ParserError:
@@ -36,6 +36,36 @@
#include "qgsgdalutils.h"
#endif

QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat, const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries, const QgsCoordinateTransformContext &transformContext )
: mFormulaString( formulaString )
, mOutputFile( outputFile )
, mOutputFormat( outputFormat )
, mOutputRectangle( outputExtent )
, mNumOutputColumns( nOutputColumns )
, mNumOutputRows( nOutputRows )
, mRasterEntries( rasterEntries )
, mTransformContext( transformContext )
{

}

QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows,
const QVector<QgsRasterCalculatorEntry> &rasterEntries, const QgsCoordinateTransformContext &transformContext )
: mFormulaString( formulaString )
, mOutputFile( outputFile )
, mOutputFormat( outputFormat )
, mOutputRectangle( outputExtent )
, mOutputCrs( outputCrs )
, mNumOutputColumns( nOutputColumns )
, mNumOutputRows( nOutputRows )
, mRasterEntries( rasterEntries )
, mTransformContext( transformContext )
{

}

// Deprecated!
QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries )
: mFormulaString( formulaString )
@@ -48,8 +78,11 @@ QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QS
{
//default to first layer's crs
mOutputCrs = mRasterEntries.at( 0 ).raster->crs();
mTransformContext = QgsProject::instance()->transformContext();
}


// Deprecated!
QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries )
: mFormulaString( formulaString )
@@ -61,6 +94,7 @@ QgsRasterCalculator::QgsRasterCalculator( const QString &formulaString, const QS
, mNumOutputRows( nOutputRows )
, mRasterEntries( rasterEntries )
{
mTransformContext = QgsProject::instance()->transformContext();
}

QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback *feedback )
@@ -176,7 +210,7 @@ QgsRasterCalculator::Result QgsRasterCalculator::processCalculation( QgsFeedback
if ( uniqueRasterEntries[layerRef.first].raster->crs() != mOutputCrs )
{
QgsRasterProjector proj;
proj.setCrs( ref.raster->crs(), mOutputCrs, ref.raster->dataProvider()->transformContext() );
proj.setCrs( ref.raster->crs(), mOutputCrs, mTransformContext );
proj.setInput( ref.raster->dataProvider() );
proj.setPrecision( QgsRasterProjector::Exact );
layerRef.second.reset( proj.block( ref.bandNumber, rect, mNumOutputColumns, 1 ) );
@@ -89,6 +89,7 @@ class ANALYSIS_EXPORT QgsRasterCalculator
BandError = 6, //!< Invalid band number for input
};


/**
* QgsRasterCalculator constructor.
* \param formulaString formula for raster calculation
@@ -98,9 +99,14 @@ class ANALYSIS_EXPORT QgsRasterCalculator
* \param nOutputColumns number of columns in output raster
* \param nOutputRows number of rows in output raster
* \param rasterEntries list of referenced raster layers
* \param transformContext coordinate transformation context
* \deprecated since QGIS 3.8, use the version with transformContext instead
* \since QGIS 3.8
*/
QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries );
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows,
const QVector<QgsRasterCalculatorEntry> &rasterEntries,
const QgsCoordinateTransformContext &transformContext );

/**
* QgsRasterCalculator constructor.
@@ -112,10 +118,46 @@ class ANALYSIS_EXPORT QgsRasterCalculator
* \param nOutputColumns number of columns in output raster
* \param nOutputRows number of rows in output raster
* \param rasterEntries list of referenced raster layers
* \since QGIS 2.10
* \param transformContext coordinate transformation context
* \deprecated since QGIS 3.8, use the version with transformContext instead
* \since QGIS 3.8
*/
QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries );
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs,
int nOutputColumns, int nOutputRows,
const QVector<QgsRasterCalculatorEntry> &rasterEntries,
const QgsCoordinateTransformContext &transformContext );


/**
* QgsRasterCalculator constructor.
* \param formulaString formula for raster calculation
* \param outputFile output file path
* \param outputFormat output file format
* \param outputExtent output extent. CRS for output is taken from first entry in rasterEntries.
* \param nOutputColumns number of columns in output raster
* \param nOutputRows number of rows in output raster
* \param rasterEntries list of referenced raster layers
* \deprecated since QGIS 3.8, use the version with transformContext instead
*/
Q_DECL_DEPRECATED QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries ) SIP_DEPRECATED;

/**
* QgsRasterCalculator constructor.
* \param formulaString formula for raster calculation
* \param outputFile output file path
* \param outputFormat output file format
* \param outputExtent output extent, CRS is specified by outputCrs parameter
* \param outputCrs destination CRS for output raster
* \param nOutputColumns number of columns in output raster
* \param nOutputRows number of rows in output raster
* \param rasterEntries list of referenced raster layers
* \deprecated since QGIS 3.8, use the version with transformContext instead
* \since QGIS 2.10
*/
Q_DECL_DEPRECATED QgsRasterCalculator( const QString &formulaString, const QString &outputFile, const QString &outputFormat,
const QgsRectangle &outputExtent, const QgsCoordinateReferenceSystem &outputCrs, int nOutputColumns, int nOutputRows, const QVector<QgsRasterCalculatorEntry> &rasterEntries ) SIP_DEPRECATED;

/**
* Starts the calculation and writes a new raster.
@@ -172,6 +214,8 @@ class ANALYSIS_EXPORT QgsRasterCalculator

/***/
QVector<QgsRasterCalculatorEntry> mRasterEntries;

QgsCoordinateTransformContext mTransformContext;
};

#endif // QGSRASTERCALCULATOR_H
@@ -466,7 +466,8 @@ void TestQgsRasterCalculator::calcWithLayers()
QgsRasterCalculator rc( QStringLiteral( "\"landsat@1\" + 2" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 0 );

//open output file and check results
@@ -487,7 +488,8 @@ void TestQgsRasterCalculator::calcWithLayers()
QgsRasterCalculator rc2( QStringLiteral( "\"landsat@1\" + \"landsat@2\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc2.processCalculation() ), 0 );

//open output file and check results
@@ -532,7 +534,8 @@ void TestQgsRasterCalculator::calcWithReprojectedLayers()
QgsRasterCalculator rc( QStringLiteral( "\"landsat@1\" + \"landsat_4326@2\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 0 );

//open output file and check results
@@ -627,7 +630,8 @@ void TestQgsRasterCalculator::errors( )
QgsRasterCalculator rc( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 6 );
QCOMPARE( rc.lastError(), QStringLiteral( "Band number 0 is not valid for entry landsat@0" ) );

@@ -637,7 +641,8 @@ void TestQgsRasterCalculator::errors( )
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 6 );
QCOMPARE( rc.lastError(), QStringLiteral( "Band number 10 is not valid for entry landsat@0" ) );

@@ -650,7 +655,8 @@ void TestQgsRasterCalculator::errors( )
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 2 );
QCOMPARE( rc.lastError(), QStringLiteral( "No raster layer for entry landsat@0" ) );

@@ -662,15 +668,17 @@ void TestQgsRasterCalculator::errors( )
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "xxxxx" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 1 );
QCOMPARE( rc.lastError(), QStringLiteral( "Could not obtain driver for xxxxx" ) );

// bad filename
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
QStringLiteral( "/goodluckwritinghere/blah/blah.tif" ),
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 1 );
QCOMPARE( rc.lastError(), QStringLiteral( "Could not create output /goodluckwritinghere/blah/blah.tif" ) );

@@ -680,7 +688,8 @@ void TestQgsRasterCalculator::errors( )
rc = QgsRasterCalculator( QStringLiteral( "\"landsat@0\"" ),
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation( &feedback ) ), 3 );
QVERIFY( rc.lastError().isEmpty() );
}
@@ -743,7 +752,8 @@ void TestQgsRasterCalculator::calcFormulasWithReprojectedLayers()
QgsRasterCalculator rc( formula,
tmpName,
QStringLiteral( "GTiff" ),
extent, crs, 2, 3, entries );
extent, crs, 2, 3, entries,
QgsProject::instance()->transformContext() );
QCOMPARE( static_cast< int >( rc.processCalculation() ), 0 );
//open output file and check results
QgsRasterLayer *result = new QgsRasterLayer( tmpName, QStringLiteral( "result" ) );

0 comments on commit ac04339

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