Skip to content

Commit

Permalink
[processing] Fix extent and CRS parameters do not evaluate in
Browse files Browse the repository at this point in the history
models when linked to a layer parameter/output value

References discussion on dev mailing list
  • Loading branch information
nyalldawson committed Jan 23, 2018
1 parent c87f5aa commit d415ad8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
38 changes: 31 additions & 7 deletions src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsCrs( const QgsP
if ( !definition )
return QgsCoordinateReferenceSystem();

QVariant val = parameters.value( definition->name() );

QString crsText = parameterAsString( definition, parameters, context );
if ( crsText.isEmpty() )
crsText = definition->defaultValue().toString();
Expand All @@ -478,7 +480,9 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsCrs( const QgsP
return context.project()->crs();

// maybe a map layer reference
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( crsText, context ) )
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
return layer->crs();
else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( crsText, context ) )
return layer->crs();

// else CRS from string
Expand Down Expand Up @@ -517,13 +521,16 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara
return rr;
}

// maybe parameter is a direct layer value?
QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );

QString rectText;
if ( val.canConvert<QgsProperty>() )
rectText = val.value< QgsProperty >().valueAsString( context.expressionContext(), definition->defaultValue().toString() );
else
rectText = val.toString();

if ( rectText.isEmpty() )
if ( rectText.isEmpty() && !layer )
return QgsRectangle();

QRegularExpression rx( "^(.*?)\\s*,\\s*(.*?),\\s*(.*?),\\s*(.*?)\\s*(?:\\[(.*)\\])?\\s*$" );
Expand Down Expand Up @@ -559,7 +566,10 @@ QgsRectangle QgsProcessingParameters::parameterAsExtent( const QgsProcessingPara
}

// try as layer extent
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( rectText, context ) )
if ( !layer )
layer = QgsProcessingUtils::mapLayerFromString( rectText, context );

if ( layer )
{
QgsRectangle rect = layer->extent();
if ( crs.isValid() && layer->crs().isValid() && crs != layer->crs() )
Expand Down Expand Up @@ -650,7 +660,13 @@ QgsGeometry QgsProcessingParameters::parameterAsExtentGeometry( const QgsProcess
}

// try as layer extent
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( rectText, context ) )

// maybe parameter is a direct layer value?
QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) );
if ( !layer )
layer = QgsProcessingUtils::mapLayerFromString( rectText, context );

if ( layer )
{
QgsRectangle rect = layer->extent();
QgsGeometry g = QgsGeometry::fromRect( rect );
Expand Down Expand Up @@ -698,10 +714,10 @@ QgsCoordinateReferenceSystem QgsProcessingParameters::parameterAsExtentCrs( cons
}

// try as layer crs
if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
{
if ( QgsMapLayer *layer = qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( val ) ) )
return layer->crs();
else if ( QgsMapLayer *layer = QgsProcessingUtils::mapLayerFromString( valueAsString, context ) )
return layer->crs();
}

if ( context.project() )
return context.project()->crs();
Expand Down Expand Up @@ -1308,6 +1324,10 @@ bool QgsProcessingParameterCrs::checkValueIsAcceptable( const QVariant &input, Q
return true;
}

// direct map layer value
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
return true;

if ( input.type() != QVariant::String || input.toString().isEmpty() )
return mFlags & FlagOptional;

Expand Down Expand Up @@ -1424,6 +1444,10 @@ bool QgsProcessingParameterExtent::checkValueIsAcceptable( const QVariant &input
return !r.isNull();
}

// direct map layer value
if ( qobject_cast< QgsMapLayer * >( qvariant_cast<QObject *>( input ) ) )
return true;

if ( input.type() != QVariant::String || input.toString().isEmpty() )
return mFlags & FlagOptional;

Expand Down
23 changes: 22 additions & 1 deletion tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,7 @@ void TestQgsProcessing::parameterCrs()
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
QVERIFY( def->checkValueIsAcceptable( "EPSG:12003" ) );
QVERIFY( def->checkValueIsAcceptable( "EPSG:3111" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );

Expand All @@ -1861,6 +1862,8 @@ void TestQgsProcessing::parameterCrs()
params.insert( "non_optional", v1->id() );
QCOMPARE( QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).authid(), QString( "EPSG:3111" ) );
QVERIFY( def->checkValueIsAcceptable( v1->id() ) );
params.insert( "non_optional", QVariant::fromValue( v1 ) );
QCOMPARE( QgsProcessingParameters::parameterAsCrs( def.get(), params, context ).authid(), QString( "EPSG:3111" ) );

// special ProjectCrs string
params.insert( "non_optional", QStringLiteral( "ProjectCrs" ) );
Expand Down Expand Up @@ -2112,6 +2115,24 @@ void TestQgsProcessing::parameterExtent()
QGSCOMPARENEAR( ext.yMinimum(), 5083255, 100 );
QGSCOMPARENEAR( ext.yMaximum(), 5083355, 100 );

// layer as parameter
params.insert( "non_optional", QVariant::fromValue( r1 ) );
QVERIFY( def->checkValueIsAcceptable( QVariant::fromValue( r1 ) ) );
QCOMPARE( QgsProcessingParameters::parameterAsExtent( def.get(), params, context ), r1->extent() );
QCOMPARE( QgsProcessingParameters::parameterAsExtentCrs( def.get(), params, context ).authid(), QStringLiteral( "EPSG:4326" ) );
ext = QgsProcessingParameters::parameterAsExtent( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QGSCOMPARENEAR( ext.xMinimum(), 1535375, 100 );
QGSCOMPARENEAR( ext.xMaximum(), 1535475, 100 );
QGSCOMPARENEAR( ext.yMinimum(), 5083255, 100 );
QGSCOMPARENEAR( ext.yMaximum(), 5083355, 100 );
QgsGeometry gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QCOMPARE( gExt.constGet()->vertexCount(), 5 );
ext = gExt.boundingBox();
QGSCOMPARENEAR( ext.xMinimum(), 1535375, 100 );
QGSCOMPARENEAR( ext.xMaximum(), 1535475, 100 );
QGSCOMPARENEAR( ext.yMinimum(), 5083255, 100 );
QGSCOMPARENEAR( ext.yMaximum(), 5083355, 100 );

// string representing a non-project layer source
params.insert( "non_optional", raster2 );
QVERIFY( def->checkValueIsAcceptable( raster2 ) );
Expand All @@ -2126,7 +2147,7 @@ void TestQgsProcessing::parameterExtent()
QGSCOMPARENEAR( ext.xMaximum(), 18.045658, 0.01 );
QGSCOMPARENEAR( ext.yMinimum(), 30.151856, 0.01 );
QGSCOMPARENEAR( ext.yMaximum(), 30.257289, 0.01 );
QgsGeometry gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
gExt = QgsProcessingParameters::parameterAsExtentGeometry( def.get(), params, context, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QCOMPARE( gExt.constGet()->vertexCount(), 85 );
ext = gExt.boundingBox();
QGSCOMPARENEAR( ext.xMinimum(), 17.924273, 0.01 );
Expand Down

0 comments on commit d415ad8

Please sign in to comment.