Skip to content

Commit

Permalink
Fix QgsVectorLayerEditUtils::addRingV2 (#59066)
Browse files Browse the repository at this point in the history
  • Loading branch information
agiudiceandrea authored Oct 14, 2024
1 parent 3783037 commit 67d4332
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/core/vector/qgsvectorlayereditutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ Qgis::GeometryOperationResult staticAddRing( QgsVectorLayer *layer, std::unique_
fit = layer->getFeatures( QgsFeatureRequest().setFilterRect( bBox ).setFlags( Qgis::FeatureRequestFlag::ExactIntersect ) );
}

//find first valid feature we can add the ring to
//find valid features we can add the ring to
bool success = false;
while ( fit.nextFeature( f ) )
{
if ( !f.hasGeometry() )
Expand All @@ -193,6 +194,7 @@ Qgis::GeometryOperationResult staticAddRing( QgsVectorLayer *layer, std::unique_
}
if ( addRingReturnCode == Qgis::GeometryOperationResult::Success )
{
success = true;
layer->changeGeometry( f.id(), g );
if ( modifiedFeatureIds )
{
Expand All @@ -206,7 +208,7 @@ Qgis::GeometryOperationResult staticAddRing( QgsVectorLayer *layer, std::unique_
}
}

return addRingReturnCode;
return success ? Qgis::GeometryOperationResult::Success : addRingReturnCode;
}

Qgis::GeometryOperationResult QgsVectorLayerEditUtils::addRing( const QVector<QgsPointXY> &ring, const QgsFeatureIds &targetFeatureIds, QgsFeatureId *modifiedFeatureId )
Expand Down
29 changes: 29 additions & 0 deletions tests/src/python/test_qgsvectorlayereditutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,35 @@ def testAddRingV2SelectedFeatures(self):
"Polygon ((2 2, 6 2, 6 6, 2 6, 2 2))"
)

def testAddRingV2AtLeastOne(self):
# test adding ring on multi features
# Succeed if the ring can be added at least to 1 feature
layer = createEmptyPolygonLayer()
self.assertTrue(layer.startEditing())

pr = layer.dataProvider()
f1 = QgsFeature(layer.fields(), 1)
f1.setGeometry(QgsGeometry.fromWkt('POLYGON((0 0, 5 0, 5 5, 0 5, 0 0))'))
f2 = QgsFeature(layer.fields(), 1)
f2.setGeometry(QgsGeometry.fromWkt('POLYGON((2 2, 6 2, 6 6, 2 6, 2 2))'))
assert pr.addFeatures([f1, f2])
self.assertEqual(layer.featureCount(), 2)

vle = QgsVectorLayerEditUtils(layer)
result = vle.addRingV2(QgsLineString([QgsPoint(0.5, 3), QgsPoint(1.5, 3), QgsPoint(1.5, 1.5), QgsPoint(3, 1.5), QgsPoint(3, 0.5), QgsPoint(0.5, 0.5), QgsPoint(0.5, 3)]))
self.assertEqual(Qgis.GeometryOperationResult.Success, result[0])
self.assertEqual({1}, set(result[1]))
layer.commitChanges()

self.assertEqual(
layer.getFeature(1).geometry().asWkt(),
"Polygon ((0 0, 5 0, 5 5, 0 5, 0 0),(0.5 3, 1.5 3, 1.5 1.5, 3 1.5, 3 0.5, 0.5 0.5, 0.5 3))"
)
self.assertEqual(
layer.getFeature(2).geometry().asWkt(),
"Polygon ((2 2, 6 2, 6 6, 2 6, 2 2))"
)

def testMoveVertex(self):
layer = QgsVectorLayer("Point", "point", "memory")
self.assertTrue(layer.startEditing())
Expand Down

0 comments on commit 67d4332

Please sign in to comment.