Skip to content
Permalink
Browse files

[processing] Correctly discard fid field values when running

algorithms with the RegeneratePrimaryKey flag in in-place mode

Fixes #37761, fixes #33816
  • Loading branch information
nyalldawson committed Oct 15, 2020
1 parent 4b3703d commit db7b97705967041d4eaca7e7f69cbf49f5692aef
@@ -240,9 +240,13 @@ def execute_in_place_run(alg, parameters, context=None, feedback=None, raise_exc

active_layer.deleteFeatures(active_layer.selectedFeatureIds())

regenerate_primary_key = result_layer.customProperty('OnConvertFormatRegeneratePrimaryKey', False)
sink_flags = QgsFeatureSink.SinkFlags(QgsFeatureSink.RegeneratePrimaryKey) if regenerate_primary_key \
else QgsFeatureSink.SinkFlags()

for f in result_layer.getFeatures():
new_features.extend(QgsVectorLayerUtils.
makeFeaturesCompatible([f], active_layer))
makeFeaturesCompatible([f], active_layer, sink_flags))

# Get the new ids
old_ids = set([f.id() for f in active_layer.getFeatures(req)])
@@ -715,6 +715,8 @@ QgsFeatureSink *QgsProcessingUtils::createFeatureSink( QString &destination, Qgs
throw QgsProcessingException( QObject::tr( "Could not create memory layer" ) );
}

layer->setCustomProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ), static_cast< bool >( sinkFlags & QgsFeatureSink::RegeneratePrimaryKey ) );

// update destination to layer ID
destination = layer->id();

@@ -1808,6 +1808,7 @@ void TestQgsProcessing::createFeatureSink()
QCOMPARE( static_cast< QgsProxyFeatureSink *>( sink.get() )->destinationSink(), layer->dataProvider() );
QCOMPARE( layer->dataProvider()->name(), QStringLiteral( "memory" ) );
QCOMPARE( destination, layer->id() );
QVERIFY( !layer->customProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ) ).toBool() );
QCOMPARE( context.temporaryLayerStore()->mapLayer( layer->id() ), layer ); // layer should be in store
QgsFeature f;
QCOMPARE( layer->featureCount(), 0L );
@@ -1854,13 +1855,14 @@ void TestQgsProcessing::createFeatureSink()
destination = QStringLiteral( "memory:mylayer" );
QgsFields fields;
fields.append( QgsField( QStringLiteral( "my_field" ), QVariant::String, QString(), 100 ) );
sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, fields, QgsWkbTypes::PointZM, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ) ) );
sink.reset( QgsProcessingUtils::createFeatureSink( destination, context, fields, QgsWkbTypes::PointZM, QgsCoordinateReferenceSystem::fromEpsgId( 3111 ), QVariantMap(), QStringList(), QStringList(), QgsFeatureSink::RegeneratePrimaryKey ) );
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(), QStringLiteral( "mylayer" ) );
QVERIFY( layer->customProperty( QStringLiteral( "OnConvertFormatRegeneratePrimaryKey" ) ).toBool() );
QCOMPARE( layer->wkbType(), QgsWkbTypes::PointZM );
QCOMPARE( layer->crs().authid(), QStringLiteral( "EPSG:3111" ) );
QCOMPARE( layer->fields().size(), 1 );

0 comments on commit db7b977

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