Skip to content

Commit 59d55fc

Browse files
committedNov 23, 2017
[processing] Ensure correct sublayer is loaded when saving output
to destination with multiple sublayers
1 parent f7a317b commit 59d55fc

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed
 

‎src/core/processing/qgsprocessingutils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,12 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
416416
return nullptr;
417417

418418
// use destination string as layer name (eg "postgis:..." )
419+
if ( !layerName.isEmpty() )
420+
uri += QStringLiteral( "|layername=%1" ).arg( layerName );
419421
std::unique_ptr< QgsVectorLayer > layer( new QgsVectorLayer( uri, destination, providerKey ) );
420422
// update destination to layer ID
421423
destination = layer->id();
424+
422425
context.temporaryLayerStore()->addMapLayer( layer.release() );
423426
return exporter.release();
424427
}

‎tests/src/analysis/testqgsprocessing.cpp

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,6 @@ void TestQgsProcessing::createFeatureSink()
13341334
QVERIFY( sink->addFeature( f ) );
13351335
QCOMPARE( layer->featureCount(), 1L );
13361336
context.temporaryLayerStore()->removeAllMapLayers();
1337-
layer = nullptr;
13381337

13391338
// non memory layer output
13401339
destination = QDir::tempPath() + "/create_feature_sink.tab";
@@ -1354,8 +1353,6 @@ void TestQgsProcessing::createFeatureSink()
13541353
QCOMPARE( layer->fields().at( 0 ).name(), QStringLiteral( "my_field" ) );
13551354
QCOMPARE( layer->fields().at( 0 ).type(), QVariant::String );
13561355
QCOMPARE( layer->featureCount(), 1L );
1357-
delete layer;
1358-
layer = nullptr;
13591356

13601357
// no extension, should default to shp
13611358
destination = QDir::tempPath() + "/create_feature_sink2";
@@ -1364,7 +1361,6 @@ void TestQgsProcessing::createFeatureSink()
13641361
QVERIFY( sink.get() );
13651362
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "PointZ(1 2 3)" ) ) );
13661363
QVERIFY( sink->addFeature( f ) );
1367-
QVERIFY( !layer );
13681364
QCOMPARE( destination, prevDest );
13691365
sink.reset( nullptr );
13701366
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination, context, true ) );
@@ -1375,13 +1371,51 @@ void TestQgsProcessing::createFeatureSink()
13751371
QCOMPARE( layer->fields().at( 1 ).name(), QStringLiteral( "my_field" ) );
13761372
QCOMPARE( layer->fields().at( 1 ).type(), QVariant::String );
13771373
QCOMPARE( layer->featureCount(), 1L );
1378-
delete layer;
1379-
layer = nullptr;
13801374

13811375
//windows style path
13821376
destination = "d:\\temp\\create_feature_sink.tab";
13831377
sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, fields, QgsWkbTypes::Polygon, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) );
13841378
QVERIFY( sink.get() );
1379+
1380+
// save to geopackage
1381+
QString geopackagePath = QDir::tempPath() + "/packaged.gpkg";
1382+
if ( QFileInfo::exists( geopackagePath ) )
1383+
QFile::remove( geopackagePath );
1384+
destination = QStringLiteral( "ogr:dbname='%1' table=\"polygons\" (geom) sql=" ).arg( geopackagePath );
1385+
sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, fields, QgsWkbTypes::Polygon, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) );
1386+
QVERIFY( sink.get() );
1387+
f = QgsFeature( fields );
1388+
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Polygon((0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
1389+
f.setAttributes( QgsAttributes() << "val" );
1390+
QVERIFY( sink->addFeature( f ) );
1391+
sink.reset( nullptr );
1392+
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination, context, true ) );
1393+
QVERIFY( layer->isValid() );
1394+
QCOMPARE( layer->wkbType(), QgsWkbTypes::Polygon );
1395+
QVERIFY( layer->getFeatures().nextFeature( f ) );
1396+
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val" ) );
1397+
1398+
// add another output to the same geopackage
1399+
QString destination2 = QStringLiteral( "ogr:dbname='%1' table=\"points\" (geom) sql=" ).arg( geopackagePath );
1400+
sink.reset( QgsProcessingUtils::createFeatureSink( destination2, context, fields, QgsWkbTypes::Point, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) );
1401+
QVERIFY( sink.get() );
1402+
f = QgsFeature( fields );
1403+
f.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "Point(0 0)" ) ) );
1404+
f.setAttributes( QgsAttributes() << "val2" );
1405+
QVERIFY( sink->addFeature( f ) );
1406+
sink.reset( nullptr );
1407+
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination2, context, true ) );
1408+
QVERIFY( layer->isValid() );
1409+
QCOMPARE( layer->wkbType(), QgsWkbTypes::Point );
1410+
QVERIFY( layer->getFeatures().nextFeature( f ) );
1411+
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val2" ) );
1412+
1413+
// original polygon layer should remain
1414+
layer = qobject_cast< QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( destination, context, true ) );
1415+
QVERIFY( layer->isValid() );
1416+
QCOMPARE( layer->wkbType(), QgsWkbTypes::Polygon );
1417+
QVERIFY( layer->getFeatures().nextFeature( f ) );
1418+
QCOMPARE( f.attribute( "my_field" ).toString(), QStringLiteral( "val" ) );
13851419
}
13861420

13871421
void TestQgsProcessing::parameters()

0 commit comments

Comments
 (0)
Please sign in to comment.