From 0675d99b34ee209a09857dfcba47cb5a92f55154 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sun, 18 Oct 2020 15:14:47 +1000 Subject: [PATCH] [processing] Fix generated layers stored in geopackage always report a feature count of 0 Remove outdated approach which saw an empty copy of the destination layer loaded immediately, and which was ultimately added to the project and was unaware of features subsequently added to the underlying geopackage data source. --- src/core/processing/qgsprocessingutils.cpp | 12 +++++++----- tests/src/analysis/testqgsprocessing.cpp | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/core/processing/qgsprocessingutils.cpp b/src/core/processing/qgsprocessingutils.cpp index 9efaef673dc3..f0194fe68a2b 100644 --- a/src/core/processing/qgsprocessingutils.cpp +++ b/src/core/processing/qgsprocessingutils.cpp @@ -798,7 +798,11 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs // use destination string as layer name (eg "postgis:..." ) if ( !layerName.isEmpty() ) - uri += QStringLiteral( "|layername=%1" ).arg( layerName ); + { + QVariantMap parts = QgsProviderRegistry::instance()->decodeUri( providerKey, uri ); + parts.insert( QStringLiteral( "layerName" ), layerName ); + uri = QgsProviderRegistry::instance()->encodeUri( providerKey, parts ); + } std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique( uri, destination, providerKey, layerOptions ); // update destination to layer ID @@ -829,11 +833,9 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs // use destination string as layer name (eg "postgis:..." ) if ( !layerName.isEmpty() ) uri += QStringLiteral( "|layername=%1" ).arg( layerName ); - std::unique_ptr< QgsVectorLayer > layer = qgis::make_unique( uri, destination, providerKey, layerOptions ); - // update destination to layer ID - destination = layer->id(); + // update destination to generated URI + destination = uri; - context.temporaryLayerStore()->addMapLayer( layer.release() ); return new QgsProcessingFeatureSink( exporter.release(), destination, context, true ); } } diff --git a/tests/src/analysis/testqgsprocessing.cpp b/tests/src/analysis/testqgsprocessing.cpp index 8c5fbdb29941..a4dc01d23792 100644 --- a/tests/src/analysis/testqgsprocessing.cpp +++ b/tests/src/analysis/testqgsprocessing.cpp @@ -1958,6 +1958,7 @@ void TestQgsProcessing::createFeatureSink() destination = QStringLiteral( "ogr:dbname='%1' table=\"polygons\" (geom) sql=" ).arg( geopackagePath ); sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, fields, QgsWkbTypes::Polygon, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) ); QVERIFY( sink.get() ); + QCOMPARE( destination, QStringLiteral( "%1|layername=polygons" ).arg( geopackagePath ) ); f = QgsFeature( fields ); f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) ); f.setAttributes( QgsAttributes() << "val" ); @@ -1973,6 +1974,7 @@ void TestQgsProcessing::createFeatureSink() QString destination2 = QStringLiteral( "ogr:dbname='%1' table=\"points\" (geom) sql=" ).arg( geopackagePath ); sink.reset( QgsProcessingUtils::createFeatureSink( destination2, context, fields, QgsWkbTypes::Point, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) ); QVERIFY( sink.get() ); + QCOMPARE( destination2, QStringLiteral( "%1|layername=points" ).arg( geopackagePath ) ); f = QgsFeature( fields ); f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Point(0 0)" ) ) ); f.setAttributes( QgsAttributes() << "val2" );