Skip to content

Commit

Permalink
Improve name of memory layers output from processing
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 26, 2017
1 parent 7451422 commit 3d307b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingparameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3163,7 +3163,7 @@ bool QgsProcessingParameterFeatureSink::fromVariantMap( const QVariantMap &map )
QString QgsProcessingParameterFeatureSink::generateTemporaryDestination() const
{
if ( supportsNonFileBasedOutputs() )
return QStringLiteral( "memory:" );
return QStringLiteral( "memory:%1" ).arg( description() );
else
return QgsProcessingDestinationParameter::generateTemporaryDestination();
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/processing/qgsprocessingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs

if ( destination.isEmpty() || destination.startsWith( QStringLiteral( "memory:" ) ) )
{
// strip "memory:" from start of destination
if ( destination.startsWith( QStringLiteral( "memory:" ) ) )
destination = destination.mid( 7 );

if ( destination.isEmpty() )
destination = QStringLiteral( "output" );

// memory provider cannot be used with QgsVectorLayerImport - so create layer manually
std::unique_ptr< QgsVectorLayer > layer( QgsMemoryProviderUtils::createMemoryLayer( destination, fields, geometryType, crs ) );
if ( !layer || !layer->isValid() )
Expand Down
23 changes: 20 additions & 3 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,24 @@ void TestQgsProcessing::createFeatureSink()
QVERIFY( layer );
QCOMPARE( static_cast< QgsProxyFeatureSink *>( sink.get() )->destinationSink(), layer->dataProvider() );
QCOMPARE( layer->dataProvider()->name(), QStringLiteral( "memory" ) );
QCOMPARE( layer->name(), QStringLiteral( "memory:mylayer" ) );
QCOMPARE( layer->name(), QStringLiteral( "mylayer" ) );
QCOMPARE( destination, layer->id() );
QCOMPARE( context.temporaryLayerStore()->mapLayer( layer->id() ), layer ); // layer should be in store
QCOMPARE( layer->featureCount(), 0L );
QVERIFY( sink->addFeature( f ) );
QCOMPARE( layer->featureCount(), 1L );
context.temporaryLayerStore()->removeAllMapLayers();
layer = nullptr;

// nameless memory layer
destination = QStringLiteral( "memory:" );
sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, QgsFields(), QgsWkbTypes::Point, QgsCoordinateReferenceSystem() ) );
QVERIFY( sink.get() );
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination, context, false ) );
QVERIFY( layer );
QCOMPARE( static_cast< QgsProxyFeatureSink *>( sink.get() )->destinationSink(), layer->dataProvider() );
QCOMPARE( layer->dataProvider()->name(), QStringLiteral( "memory" ) );
QCOMPARE( layer->name(), QString( "output" ) ); // should fall back to "output" name
QCOMPARE( destination, layer->id() );
QCOMPARE( context.temporaryLayerStore()->mapLayer( layer->id() ), layer ); // layer should be in store
QCOMPARE( layer->featureCount(), 0L );
Expand All @@ -1327,7 +1344,7 @@ void TestQgsProcessing::createFeatureSink()
QVERIFY( layer );
QCOMPARE( static_cast< QgsProxyFeatureSink *>( sink.get() )->destinationSink(), layer->dataProvider() );
QCOMPARE( layer->dataProvider()->name(), QStringLiteral( "memory" ) );
QCOMPARE( layer->name(), QStringLiteral( "memory:mylayer" ) );
QCOMPARE( layer->name(), QStringLiteral( "mylayer" ) );
QCOMPARE( layer->wkbType(), QgsWkbTypes::PointZM );
QCOMPARE( layer->crs().authid(), QStringLiteral( "EPSG:3111" ) );
QCOMPARE( layer->fields().size(), 1 );
Expand Down Expand Up @@ -5369,7 +5386,7 @@ void TestQgsProcessing::modelExecution()
model2.addChildAlgorithm( alg2c2 );
params = model2.parametersForChildAlgorithm( model2.childAlgorithm( "cx2" ), modelInputs, childResults, expContext );
QCOMPARE( params.value( "INPUT" ).toString(), QStringLiteral( "dest.shp" ) );
QCOMPARE( params.value( "OUTPUT" ).toString(), QStringLiteral( "memory:" ) );
QCOMPARE( params.value( "OUTPUT" ).toString(), QStringLiteral( "memory:Centroids" ) );
QCOMPARE( params.count(), 2 );

variables = model2.variablesForChildAlgorithm( "cx2", context );
Expand Down

0 comments on commit 3d307b4

Please sign in to comment.