Skip to content
Permalink
Browse files

Fix QgsVectorLayer::updateFeature cannot add geometry to feature

with no existing geometry

Fixes #17678
  • Loading branch information
nyalldawson committed Jan 4, 2018
1 parent 5ecb560 commit 71cdb8cf8e99b249a310cfb359c79d82baccb008
Showing with 24 additions and 1 deletion.
  1. +1 −1 src/core/qgsvectorlayer.cpp
  2. +23 −0 tests/src/python/test_qgsvectorlayer.py
@@ -985,7 +985,7 @@ bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipD
bool hasChanged = false;
bool hasError = false;

if ( updatedFeature.hasGeometry() && currentFeature.hasGeometry() && !updatedFeature.geometry().isGeosEqual( currentFeature.geometry() ) )
if ( ( updatedFeature.hasGeometry() || currentFeature.hasGeometry() ) && !updatedFeature.geometry().isGeosEqual( currentFeature.geometry() ) )
{
if ( changeGeometry( updatedFeature.id(), updatedFeature.geometry(), true ) )
{
@@ -746,6 +746,29 @@ def testUpdateFeature(self):
self.assertEqual(new_feature.attributes(), ['new',321])
self.assertEqual(new_feature.geometry().asPoint(), QgsPointXY(-200, -200))

# add feature with no geometry
f6 = QgsFeature()
f6.setAttributes(["test6", 555])
self.assertTrue(layer.dataProvider().addFeatures([f6]))
features = [f for f in layer.getFeatures()]

# update feature with no geometry -> have geometry
f = features[-1]
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(-350, -250)))
self.assertTrue(layer.updateFeature(f))
new_feature = next(layer.getFeatures(QgsFeatureRequest(f.id())))
self.assertEqual(new_feature.attributes(), ['test6', 555])
self.assertTrue(new_feature.hasGeometry())
self.assertEqual(new_feature.geometry().asPoint(), QgsPointXY(-350, -250))

# update feature from geometry -> no geometry
f = features[1]
f.clearGeometry()
self.assertTrue(layer.updateFeature(f))
new_feature = next(layer.getFeatures(QgsFeatureRequest(f.id())))
self.assertEqual(new_feature.attributes(), ['test2', 457])
self.assertFalse(new_feature.hasGeometry())

# ADD ATTRIBUTE

def test_AddAttribute(self):

0 comments on commit 71cdb8c

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