Skip to content

Commit

Permalink
[processing] Correctly discard fid field values when running
Browse files Browse the repository at this point in the history
algorithms with the RegeneratePrimaryKey flag in in-place mode

Fixes qgis#37761, fixes qgis#33816
  • Loading branch information
nyalldawson committed Oct 15, 2020
1 parent feebb08 commit 6461d3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion python/plugins/processing/gui/AlgorithmExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
2 changes: 2 additions & 0 deletions src/core/processing/qgsprocessingutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 3 additions & 1 deletion tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 6461d3d

Please sign in to comment.