Skip to content
Permalink
Browse files

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.
  • Loading branch information
nyalldawson committed Feb 16, 2018
1 parent 6d3b37a commit 52585cf2264e31dec7307767831baa1ebbbe2515
Showing with 15 additions and 2 deletions.
  1. +13 −0 src/core/providers/memory/qgsmemoryprovider.cpp
  2. +2 −2 tests/src/python/providertestbase.py
@@ -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 );

@@ -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):

0 comments on commit 52585cf

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