Skip to content
Permalink
Browse files

Fix use of direct map layers as inputs for source parameters

  • Loading branch information
nyalldawson committed Jun 28, 2017
1 parent 891481d commit d2b9652d4b3cf82ce4aff743bc26be81302c2528
@@ -59,6 +59,7 @@

from qgis.core import (QgsVectorLayer,
QgsRasterLayer,
QgsMapLayer,
QgsProject,
QgsApplication,
QgsProcessingContext,
@@ -241,7 +242,10 @@ def check_results(self, results, context, params, expected):
if expected_result['type'] in ('vector', 'table'):
if 'compare' in expected_result and not expected_result['compare']:
# skipping the comparison, so just make sure output is valid
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
if isinstance(results[id], QgsMapLayer):
result_lyr = results[id]
else:
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
self.assertTrue(result_lyr.isValid())
continue

@@ -254,7 +258,11 @@ def check_results(self, results, context, params, expected):
except KeyError as e:
raise KeyError('Expected result {} does not exist in {}'.format(str(e), list(results.keys())))

result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)
if isinstance(results[id], QgsMapLayer):
assert False
result_lyr = results[id]
else:
result_lyr = QgsProcessingUtils.mapLayerFromString(results[id], context)

compare = expected_result.get('compare', {})

@@ -277,6 +277,11 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
val = fromVar.source;
}

if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( val ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}

QString layerRef;
if ( val.canConvert<QgsProperty>() )
{
@@ -285,6 +290,11 @@ QgsProcessingFeatureSource *QgsProcessingParameters::parameterAsSource( const Qg
else if ( !val.isValid() || val.toString().isEmpty() )
{
// fall back to default
if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( definition->defaultValue() ) ) )
{
return new QgsProcessingFeatureSource( layer, context );
}

layerRef = definition->defaultValue().toString();
}
else
@@ -3924,6 +3924,15 @@ void TestQgsProcessing::processingFeatureSource()
QVERIFY( source->getFeatures().nextFeature( f2 ) );
QCOMPARE( f2.geometry(), f.geometry() );

// direct map layer
params.insert( QStringLiteral( "layer" ), QVariant::fromValue( layer ) );
source.reset( QgsProcessingParameters::parameterAsSource( def.get(), params, context ) );
// can't directly match it to layer, so instead just get the feature and test that it matches what we expect
QVERIFY( source.get() );
QVERIFY( source->getFeatures().nextFeature( f2 ) );
QCOMPARE( f2.geometry(), f.geometry() );


// next using property based definition
params.insert( QStringLiteral( "layer" ), QgsProcessingFeatureSourceDefinition( QgsProperty::fromExpression( QStringLiteral( "trim('%1' + ' ')" ).arg( layer->id() ) ), false ) );
source.reset( QgsProcessingParameters::parameterAsSource( def.get(), params, context ) );

0 comments on commit d2b9652

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