Skip to content
Permalink
Browse files

Tweak QgsProcessingUtils::combineLayerExtents for future proofing, re…

…move deprecated usage
  • Loading branch information
nyalldawson committed Apr 2, 2019
1 parent 82f2cb1 commit b6bc1ee2d48c9e19e60a84e30b64a848010f93ba
@@ -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
@@ -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
"""
@@ -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()
@@ -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):
@@ -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):
@@ -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):
@@ -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):
@@ -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):
@@ -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):
@@ -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):
@@ -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)
@@ -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)
@@ -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:
@@ -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 )
@@ -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() );
@@ -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 )
@@ -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
@@ -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
@@ -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 );

0 comments on commit b6bc1ee

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