Skip to content
Permalink
Browse files

Merge pull request #4604 from boundlessgeo/postgis_merge_features_por…

…t3_fix#15741

Fix Postgis Merge selected features regression (fix #15741)
  • Loading branch information
alexbruy committed Jun 12, 2017
2 parents 1be9997 + ac1a579 commit e850c82cb01b0ae66d7d3c1f7781fd677ba2ec1f
@@ -56,6 +56,7 @@ class CORE_EXPORT QgsVectorDataProvider : public QgsDataProvider, public QgsFeat
Q_OBJECT

friend class QgsTransaction;
friend class QgsVectorLayerEditBuffer;

public:

@@ -310,6 +310,35 @@ 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.hasGeometry() ) ||
( f.geometry().wkbType() == provider->wkbType() ) )
continue;

if ( provider->convertToProviderType( f.geometry() ).isNull() )
{
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;
break;
}
}
}
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 touched lines =>
# cannot be forced to be linestring
multiline = [
[QgsPointXY(1, 1), QgsPointXY(2, 2)],
[QgsPointXY(3, 3), QgsPointXY(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 e850c82

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