Skip to content

Commit 52585cf

Browse files
committed
Fix memory provider handling of missing attributes when adding features
Ensure that features added to a memory provider (and returned when fetching features from a memory provider) always have the correct number of attributes present. Fixes many random behavior bugs when working with memory providers.
1 parent 6d3b37a commit 52585cf

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/core/providers/memory/qgsmemoryprovider.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,24 @@ bool QgsMemoryProvider::addFeatures( QgsFeatureList &flist, Flags )
346346
// whether or not to update the layer extent on the fly as we add features
347347
bool updateExtent = mFeatures.isEmpty() || !mExtent.isEmpty();
348348

349+
int fieldCount = mFields.count();
350+
349351
// TODO: sanity checks of fields and geometries
350352
for ( QgsFeatureList::iterator it = flist.begin(); it != flist.end(); ++it )
351353
{
352354
it->setId( mNextFeatureId );
353355
it->setValid( true );
356+
if ( it->attributes().count() < fieldCount )
357+
{
358+
// ensure features have the correct number of attributes by padding
359+
// them with null attributes for missing values
360+
QgsAttributes attributes = it->attributes();
361+
for ( int i = it->attributes().count(); i < mFields.count(); ++i )
362+
{
363+
attributes.append( QVariant( mFields.at( i ).type() ) );
364+
}
365+
it->setAttributes( attributes );
366+
}
354367

355368
mFeatures.insert( mNextFeatureId, *it );
356369

tests/src/python/providertestbase.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ def testAddFeatureMissingAttributes(self):
463463
f2.setId(added[1].id())
464464

465465
# check result - feature attributes MUST be padded out to required number of fields
466-
f1.setAttributes([6, -220, NULL, 'String', NULL])
467-
f2.setAttributes([7, 330, NULL, NULL, NULL])
466+
f1.setAttributes([6, -220, NULL, 'String', 'NULL'])
467+
f2.setAttributes([7, 330, NULL, NULL, 'NULL'])
468468
self.testGetFeatures(l.dataProvider(), [f1, f2])
469469

470470
def testAddFeaturesUpdateExtent(self):

0 commit comments

Comments
 (0)