Skip to content

Commit 0325a37

Browse files
committedMar 7, 2019
Fix makeFeatureCompatible wrong field count
Fixes #21497 - Copying features from source layer to target layer - field values are not copied
1 parent 41cc02f commit 0325a37

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed
 

‎python/core/auto_generated/qgsvectorlayerutils.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ If the ``feature`` does not have a valid fields container set, then the feature'
231231
are simply truncated to match the number of fields present in ``fields`` (or if
232232
less attributes are present in ``feature`` than in ``fields``, the feature's attributes
233233
are padded with NULL values to match the required length).
234+
Finally, the feature's fields are set to ``fields``.
234235

235236
.. versionadded:: 3.4
236237
%End

‎src/core/qgsvectorlayerutils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ void QgsVectorLayerUtils::matchAttributesToFields( QgsFeature &feature, const Qg
673673
feature.setAttributes( attributes );
674674
}
675675
}
676+
feature.setFields( fields );
676677
}
677678

678679
QgsFeatureList QgsVectorLayerUtils::makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer *layer )

‎src/core/qgsvectorlayerutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ class CORE_EXPORT QgsVectorLayerUtils
237237
* are simply truncated to match the number of fields present in \a fields (or if
238238
* less attributes are present in \a feature than in \a fields, the feature's attributes
239239
* are padded with NULL values to match the required length).
240+
* Finally, the feature's fields are set to \a fields.
240241
*
241242
* \since QGIS 3.4
242243
*/

‎tests/src/python/test_qgsprocessinginplace.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,26 @@ def test_make_features_compatible_attributes(self):
412412
self.assertEqual(new_features[0].attributes()[0], 1)
413413
self.assertEqual(new_features[0].attributes()[1], 'foo')
414414

415+
def test_make_features_compatible_different_field_length(self):
416+
"""Test regression #21497"""
417+
fields = QgsFields()
418+
fields.append(QgsField('int_f1', QVariant.Int))
419+
f1 = QgsFeature(fields)
420+
f1.setAttributes([12345])
421+
f1.setGeometry(QgsGeometry.fromWkt('Point(9 45)'))
422+
423+
fields = QgsFields()
424+
fields.append(QgsField('int_f2', QVariant.Int))
425+
fields.append(QgsField('int_f1', QVariant.Int))
426+
vl2 = QgsMemoryProviderUtils.createMemoryLayer(
427+
'mymultiplayer', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326))
428+
new_features = QgsVectorLayerUtils.makeFeaturesCompatible([f1], vl2)
429+
self.assertEqual(new_features[0].attributes(), [None, 12345])
430+
431+
f1.setGeometry(QgsGeometry.fromWkt('MultiPoint((9 45))'))
432+
new_features = QgsVectorLayerUtils.makeFeaturesCompatible([f1], vl2)
433+
self.assertEqual(new_features[0].attributes(), [None, 12345])
434+
415435
def test_make_features_compatible_geometry(self):
416436
"""Test corner cases for geometries"""
417437

0 commit comments

Comments
 (0)
Please sign in to comment.