Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the "Add ring" map tool / QgsVectorLayerEditUtils::addRingV2 (Fix #59053) #59066

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading