Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix makeFeatureCompatible wrong field count
Fixes #21497 - Copying features from source layer to target layer - field values are not copied
  • Loading branch information
elpaso committed Mar 7, 2019
1 parent 41cc02f commit 0325a37
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsvectorlayerutils.sip.in
Expand Up @@ -231,6 +231,7 @@ If the ``feature`` does not have a valid fields container set, then the feature'
are simply truncated to match the number of fields present in ``fields`` (or if
less attributes are present in ``feature`` than in ``fields``, the feature's attributes
are padded with NULL values to match the required length).
Finally, the feature's fields are set to ``fields``.

.. versionadded:: 3.4
%End
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayerutils.cpp
Expand Up @@ -673,6 +673,7 @@ void QgsVectorLayerUtils::matchAttributesToFields( QgsFeature &feature, const Qg
feature.setAttributes( attributes );
}
}
feature.setFields( fields );
}

QgsFeatureList QgsVectorLayerUtils::makeFeatureCompatible( const QgsFeature &feature, const QgsVectorLayer *layer )
Expand Down
1 change: 1 addition & 0 deletions src/core/qgsvectorlayerutils.h
Expand Up @@ -237,6 +237,7 @@ class CORE_EXPORT QgsVectorLayerUtils
* are simply truncated to match the number of fields present in \a fields (or if
* less attributes are present in \a feature than in \a fields, the feature's attributes
* are padded with NULL values to match the required length).
* Finally, the feature's fields are set to \a fields.
*
* \since QGIS 3.4
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/src/python/test_qgsprocessinginplace.py
Expand Up @@ -412,6 +412,26 @@ def test_make_features_compatible_attributes(self):
self.assertEqual(new_features[0].attributes()[0], 1)
self.assertEqual(new_features[0].attributes()[1], 'foo')

def test_make_features_compatible_different_field_length(self):
"""Test regression #21497"""
fields = QgsFields()
fields.append(QgsField('int_f1', QVariant.Int))
f1 = QgsFeature(fields)
f1.setAttributes([12345])
f1.setGeometry(QgsGeometry.fromWkt('Point(9 45)'))

fields = QgsFields()
fields.append(QgsField('int_f2', QVariant.Int))
fields.append(QgsField('int_f1', QVariant.Int))
vl2 = QgsMemoryProviderUtils.createMemoryLayer(
'mymultiplayer', fields, QgsWkbTypes.Point, QgsCoordinateReferenceSystem(4326))
new_features = QgsVectorLayerUtils.makeFeaturesCompatible([f1], vl2)
self.assertEqual(new_features[0].attributes(), [None, 12345])

f1.setGeometry(QgsGeometry.fromWkt('MultiPoint((9 45))'))
new_features = QgsVectorLayerUtils.makeFeaturesCompatible([f1], vl2)
self.assertEqual(new_features[0].attributes(), [None, 12345])

def test_make_features_compatible_geometry(self):
"""Test corner cases for geometries"""

Expand Down

0 comments on commit 0325a37

Please sign in to comment.