Skip to content
Permalink
Browse files

Merge pull request #9517 from qgis/backport-9417-on-release-3_6

Backport #9417 on release-3_6
  • Loading branch information
elpaso committed Mar 14, 2019
2 parents 568eeb4 + 3011f22 commit 3845d793c99796b71b8b74e45b278317eda88439
@@ -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
@@ -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 )
@@ -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
*/
@@ -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"""

@@ -552,10 +552,10 @@ def test_make_features_compatible_attributes(self):
f1.setAttributes([1, 'foo', 'blah'])
QgsVectorLayerUtils.matchAttributesToFields(f1, fields)
self.assertEqual(len(f1.attributes()), 4)
self.assertEqual(f1.attributes()[0], 'foo')
self.assertEqual(f1.attributes()[1], 1)
self.assertEqual(f1.attributes()[2], QVariant())
self.assertEqual(f1.attributes()[3], 'blah')
self.assertEqual(f1.attributes()[0], 1)
self.assertEqual(f1.attributes()[1], 'foo')
self.assertEqual(f1.attributes()[2], 'blah')
self.assertEqual(f1.attributes()[3], QVariant())

# case insensitive
fields2.append(QgsField('extra3', QVariant.String))

0 comments on commit 3845d79

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