Skip to content

Commit

Permalink
[processing] Fix generated layers stored in geopackage always
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyalldawson committed Oct 18, 2020
1 parent 69686c2 commit 0675d99
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -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<QgsVectorLayer>( uri, destination, providerKey, layerOptions );
// update destination to layer ID
Expand Down Expand Up @@ -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<QgsVectorLayer>( 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 );
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -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" );
Expand All @@ -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" );
Expand Down

0 comments on commit 0675d99

Please sign in to comment.