Skip to content

Commit

Permalink
Tweak QgsProcessingUtils::combineLayerExtents for future proofing, re…
Browse files Browse the repository at this point in the history
…move deprecated usage
  • Loading branch information
nyalldawson committed Apr 17, 2019
1 parent 82f2cb1 commit b6bc1ee
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 24 deletions.
Expand Up @@ -177,11 +177,11 @@ temporary layer store.
%End


static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext );
static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );
%Docstring
Combines the extent of several map ``layers``. If specified, the target ``crs``
will be used to transform the layer's extent to the desired output reference system
using the specified ``transformContext``.
using the specified ``context``.

.. versionadded:: 3.10
%End
Expand Down
6 changes: 3 additions & 3 deletions python/plugins/processing/algs/grass7/Grass7Algorithm.py
Expand Up @@ -489,9 +489,9 @@ def processInputs(self, parameters, context, feedback):
elif layer.type() == QgsMapLayerType.VectorLayer:
self.loadVectorLayer(layerName, layer, external=None, feedback=feedback)

self.postInputs()
self.postInputs(context)

def postInputs(self):
def postInputs(self, context):
"""
After layer imports, we need to update some internal parameters
"""
Expand All @@ -500,7 +500,7 @@ def postInputs(self):

# Build GRASS region
if self.region.isEmpty():
self.region = QgsProcessingUtils.combineLayerExtents(self.inputLayers)
self.region = QgsProcessingUtils.combineLayerExtents(self.inputLayers, context)
command = 'g.region n={} s={} e={} w={}'.format(
self.region.yMaximum(), self.region.yMinimum(),
self.region.xMaximum(), self.region.xMinimum()
Expand Down
Expand Up @@ -33,7 +33,7 @@ def processInputs(alg, parameters, context, feedback):
# Use v.in.ogr
for name in ['first', 'second']:
alg.loadRasterLayerFromParameter(name, parameters, context, False, None)
alg.postInputs()
alg.postInputs(context)


def processOutputs(alg, parameters, context, feedback):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_blend_rgb.py
Expand Up @@ -37,7 +37,7 @@ def processInputs(alg, parameters, context, feedback):
# Use v.in.ogr
for name in ['first', 'second']:
alg.loadRasterLayerFromParameter(name, parameters, context, False, None)
alg.postInputs()
alg.postInputs(context)


def processCommand(alg, parameters, context, feedback):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_category.py
Expand Up @@ -52,7 +52,7 @@ def processInputs(alg, parameters, context, feedback):
parameters, context,
False, None)
alg.loadRasterLayerFromParameter('map', parameters, context)
alg.postInputs()
alg.postInputs(context)


def processCommand(alg, parameters, context, feedback):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_colors.py
Expand Up @@ -54,7 +54,7 @@ def processInputs(alg, parameters, context, feedback):
if raster:
alg.loadRasterLayerFromParameter('raster', parameters, context, False, None)

alg.postInputs()
alg.postInputs(context)


def processCommand(alg, parameters, context, feedback):
Expand Down
Expand Up @@ -35,7 +35,7 @@ def processInputs(alg, parameters, context, feedback):

# We need to import all the bands and color tables of the input raster
alg.loadRasterLayerFromParameter('map', parameters, context, False, None)
alg.postInputs()
alg.postInputs(context)


def processCommand(alg, parameters, context, feedback):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_null.py
Expand Up @@ -42,7 +42,7 @@ def processInputs(alg, parameters, context, feedback):

# We need to import without r.external
alg.loadRasterLayerFromParameter('map', parameters, context, False)
alg.postInputs()
alg.postInputs(context)


def processCommand(alg, parameters, context, feedback):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/r_rgb.py
Expand Up @@ -32,7 +32,7 @@ def processInputs(alg, parameters, context, feedback):

# We need to import all the bands and color tables of the input raster
alg.loadRasterLayerFromParameter('input', parameters, context, False, None)
alg.postInputs()
alg.postInputs(context)


def processCommand(alg, parameters, context, feedback):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/v_sample.py
Expand Up @@ -34,4 +34,4 @@ def processInputs(alg, parameters, context, feedback):
# and we can use r.external for the raster
alg.loadVectorLayerFromParameter('input', parameters, context, feedback, False)
alg.loadRasterLayerFromParameter('raster', parameters, context, True)
alg.postInputs()
alg.postInputs(context)
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/v_to_3d.py
Expand Up @@ -42,4 +42,4 @@ def processInputs(alg, parameters, context, feedback):

# We need to import the vector layer with v.in.ogr
alg.loadVectorLayerFromParameter('input', parameters, context, feedback, False)
alg.postInputs()
alg.postInputs(context)
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/RasterCalculator.py
Expand Up @@ -127,7 +127,7 @@ def processAlgorithm(self, parameters, context, feedback):
bbox = transform.transformBoundingBox(bbox)

if bbox.isNull() and layers:
bbox = QgsProcessingUtils.combineLayerExtents(layers, crs)
bbox = QgsProcessingUtils.combineLayerExtents(layers, crs, context)

cellsize = self.parameterAsDouble(parameters, self.CELLSIZE, context)
if cellsize == 0 and not layers:
Expand Down
7 changes: 4 additions & 3 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -627,7 +627,7 @@ void QgsProcessingUtils::createFeatureSinkPython( QgsFeatureSink **sink, QString
}


QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext )
QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context )
{
QgsRectangle extent;
for ( const QgsMapLayer *layer : layers )
Expand All @@ -638,7 +638,7 @@ QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *>
if ( crs.isValid() )
{
//transform layer extent to target CRS
QgsCoordinateTransform ct( layer->crs(), crs, transformContext );
QgsCoordinateTransform ct( layer->crs(), crs, context.transformContext() );
try
{
QgsRectangle reprojExtent = ct.transformBoundingBox( layer->extent() );
Expand All @@ -662,7 +662,8 @@ QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *>
// Deprecated
QgsRectangle QgsProcessingUtils::combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs )
{
return QgsProcessingUtils::combineLayerExtents( layers, crs, QgsCoordinateTransformContext( ) );
QgsProcessingContext context;
return QgsProcessingUtils::combineLayerExtents( layers, crs, context );
}

QVariant QgsProcessingUtils::generateIteratingDestination( const QVariant &input, const QVariant &id, QgsProcessingContext &context )
Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingutils.h
Expand Up @@ -217,10 +217,10 @@ class CORE_EXPORT QgsProcessingUtils
/**
* Combines the extent of several map \a layers. If specified, the target \a crs
* will be used to transform the layer's extent to the desired output reference system
* using the specified \a transformContext.
* using the specified \a context.
* \since QGIS 3.10
*/
static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &transformContext );
static QgsRectangle combineLayerExtents( const QList<QgsMapLayer *> &layers, const QgsCoordinateReferenceSystem &crs, QgsProcessingContext &context );

/**
* Combines the extent of several map \a layers. If specified, the target \a crs
Expand Down
9 changes: 5 additions & 4 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -6236,7 +6236,8 @@ void TestQgsProcessing::checkParamValues()

void TestQgsProcessing::combineLayerExtent()
{
QgsRectangle ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() );
QgsProcessingContext context;
QgsRectangle ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>(), context );
QVERIFY( ext.isNull() );

QString testDataDir = QStringLiteral( TEST_DATA_DIR ) + '/'; //defined in CmakeLists.txt
Expand All @@ -6248,20 +6249,20 @@ void TestQgsProcessing::combineLayerExtent()
QFileInfo fi2( raster2 );
std::unique_ptr< QgsRasterLayer > r2( new QgsRasterLayer( fi2.filePath(), "R2" ) );

ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() << r1.get() );
ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() << r1.get(), context );
QGSCOMPARENEAR( ext.xMinimum(), 1535375.000000, 10 );
QGSCOMPARENEAR( ext.xMaximum(), 1535475, 10 );
QGSCOMPARENEAR( ext.yMinimum(), 5083255, 10 );
QGSCOMPARENEAR( ext.yMaximum(), 5083355, 10 );

ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() << r1.get() << r2.get() );
ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() << r1.get() << r2.get(), context );
QGSCOMPARENEAR( ext.xMinimum(), 781662, 10 );
QGSCOMPARENEAR( ext.xMaximum(), 1535475, 10 );
QGSCOMPARENEAR( ext.yMinimum(), 3339523, 10 );
QGSCOMPARENEAR( ext.yMaximum(), 5083355, 10 );

// with reprojection
ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() << r1.get() << r2.get(), QgsCoordinateReferenceSystem::fromEpsgId( 3785 ) );
ext = QgsProcessingUtils::combineLayerExtents( QList< QgsMapLayer *>() << r1.get() << r2.get(), QgsCoordinateReferenceSystem::fromEpsgId( 3785 ), context );
QGSCOMPARENEAR( ext.xMinimum(), 1995320, 10 );
QGSCOMPARENEAR( ext.xMaximum(), 2008833, 10 );
QGSCOMPARENEAR( ext.yMinimum(), 3523084, 10 );
Expand Down

0 comments on commit b6bc1ee

Please sign in to comment.