Skip to content

Commit

Permalink
Fix memory provider handling of missing attributes when adding features
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyalldawson committed Feb 16, 2018
1 parent 6d3b37a commit 52585cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/core/providers/memory/qgsmemoryprovider.cpp
Expand Up @@ -346,11 +346,24 @@ bool QgsMemoryProvider::addFeatures( QgsFeatureList &flist, Flags )
// whether or not to update the layer extent on the fly as we add features
bool updateExtent = mFeatures.isEmpty() || !mExtent.isEmpty();

int fieldCount = mFields.count();

// TODO: sanity checks of fields and geometries
for ( QgsFeatureList::iterator it = flist.begin(); it != flist.end(); ++it )
{
it->setId( mNextFeatureId );
it->setValid( true );
if ( it->attributes().count() < fieldCount )
{
// ensure features have the correct number of attributes by padding
// them with null attributes for missing values
QgsAttributes attributes = it->attributes();
for ( int i = it->attributes().count(); i < mFields.count(); ++i )
{
attributes.append( QVariant( mFields.at( i ).type() ) );
}
it->setAttributes( attributes );
}

mFeatures.insert( mNextFeatureId, *it );

Expand Down
4 changes: 2 additions & 2 deletions tests/src/python/providertestbase.py
Expand Up @@ -463,8 +463,8 @@ def testAddFeatureMissingAttributes(self):
f2.setId(added[1].id())

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

def testAddFeaturesUpdateExtent(self):
Expand Down

0 comments on commit 52585cf

Please sign in to comment.