Skip to content
Permalink
Browse files

fix Postgis Merge selected features regression: port 3.0 fixes #15741

  • Loading branch information
luipir committed May 22, 2017
1 parent 1be9997 commit 87116abd72ac52be042c3bff195a3cc931b035e6
@@ -56,6 +56,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
Q_OBJECT

friend class QgsTransaction;
friend class QgsVectorLayerEditBuffer;

public:

@@ -310,6 +310,34 @@ bool QgsVectorLayerEditBuffer::commitChanges( QStringList &commitErrors )
// no yes => changeAttributeValues
// yes yes => changeFeatures

// to fix https://issues.qgis.org/issues/15741
// first of all check if feature to add is compatible with provider type
// this check have to be done before all checks to avoid to clear internal
// buffer if some of next steps success.
if ( success && !mAddedFeatures.isEmpty() )
{
if ( cap & QgsVectorDataProvider::AddFeatures )
{
for ( QgsFeature f : mAddedFeatures )
{
if ( f.geometry().wkbType() == provider->wkbType() )
continue;

std::unique_ptr<QgsGeometry> convertedGeom( provider->convertToProviderType( f.geometry() ) );
if ( ! convertedGeom )
{
commitErrors << tr( "ERROR: %n feature(s) not added - geometry type is not compatible with the current layer.", "not added features count", mAddedFeatures.size() );
success = false;
}
}
}
else
{
commitErrors << tr( "ERROR: %n feature(s) not added - provider doesn't support adding features.", "not added features count", mAddedFeatures.size() );
success = false;
}
}

//
// update geometries
//
@@ -44,6 +44,13 @@ def createLayerWithOnePoint():
return layer


def createEmptyLinestringLayer():
layer = QgsVectorLayer("Linestring?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
assert layer.isValid()
return layer


class TestQgsVectorLayerEditBuffer(unittest.TestCase):

def testAddFeatures(self):
@@ -78,6 +85,27 @@ def testAddFeatures(self):
self.assertTrue(layer.editBuffer().isFeatureAdded(new_feature_ids[0]))
self.assertTrue(layer.editBuffer().isFeatureAdded(new_feature_ids[1]))

# check if error in case adding not adaptable geometry
# eg. a Multiline in a Line
layer = createEmptyLinestringLayer()
self.assertTrue(layer.startEditing())

self.assertEqual(layer.editBuffer().addedFeatures(), {})
self.assertFalse(layer.editBuffer().isFeatureAdded(1))
self.assertFalse(layer.editBuffer().isFeatureAdded(3))

# add a features with a multi line geometry of not toched lines =>
# cannot be forced to be linestring
multiline = [
[QgsPoint(1, 1), QgsPoint(2, 2)],
[QgsPoint(3, 3), QgsPoint(4, 4)],
]
f1 = QgsFeature(layer.fields(), 1)
f1.setGeometry(QgsGeometry.fromMultiPolyline(multiline))
f1.setAttributes(["test", 123])
self.assertTrue(layer.addFeatures([f1]))
self.assertFalse(layer.commitChanges())

def testAddMultipleFeatures(self):
# test adding multiple features to an edit buffer
layer = createEmptyLayer()

0 comments on commit 87116ab

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