Skip to content

Commit 29e2b6a

Browse files
committed
Fix fill ring tool creates more features than needed (fix #13354)
(cherry-picked from 45a6f71)
1 parent 4d7cb24 commit 29e2b6a

7 files changed

+27
-17
lines changed

python/core/qgsvectorlayer.sip

+4-2
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,9 @@ class QgsVectorLayer : QgsMapLayer
595595
*/
596596
bool deleteSelectedFeatures( int *deletedCount = 0 );
597597

598-
/**Adds a ring to polygon/multipolygon features
598+
/** Adds a ring to polygon/multipolygon features
599+
* @param ring ring to add
600+
* @param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
599601
@return
600602
0 in case of success,
601603
1 problem with feature type,
@@ -604,7 +606,7 @@ class QgsVectorLayer : QgsMapLayer
604606
4 ring crosses existing rings,
605607
5 no feature found where ring can be inserted
606608
6 layer not editable */
607-
int addRing( const QList<QgsPoint>& ring );
609+
int addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId = 0 );
608610

609611
/**Adds a new part polygon to a multipart feature
610612
@return

python/core/qgsvectorlayereditutils.sip

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ class QgsVectorLayerEditUtils
2626
bool deleteVertex( QgsFeatureId atFeatureId, int atVertex );
2727

2828
/** Adds a ring to polygon/multipolygon features
29+
* @param ring ring to add
30+
* @param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
2931
@return
3032
0 in case of success,
3133
1 problem with feature type,
3234
2 ring not closed,
3335
3 ring not valid,
3436
4 ring crosses existing rings,
3537
5 no feature found where ring can be inserted*/
36-
int addRing( const QList<QgsPoint>& ring );
38+
int addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId = 0 );
3739

3840
/** Adds a new part polygon to a multipart feature
3941
@return

src/app/qgsmaptoolfillring.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ void QgsMapToolFillRing::canvasMapReleaseEvent( QgsMapMouseEvent * e )
7979
closePolygon();
8080

8181
vlayer->beginEditCommand( tr( "Ring added and filled" ) );
82-
int addRingReturnCode = vlayer->addRing( points() );
82+
QList< QgsPoint > pointList = points();
83+
84+
QgsFeatureId modifiedFid;
85+
int addRingReturnCode = vlayer->addRing( pointList, &modifiedFid );
8386
if ( addRingReturnCode != 0 )
8487
{
8588
QString errorMessage;
@@ -122,7 +125,7 @@ void QgsMapToolFillRing::canvasMapReleaseEvent( QgsMapMouseEvent * e )
122125
yMin = std::numeric_limits<double>::max();
123126
yMax = -std::numeric_limits<double>::max();
124127

125-
Q_FOREACH ( const QgsPoint& point, points() )
128+
Q_FOREACH ( const QgsPoint& point, pointList )
126129
{
127130
xMin = qMin( xMin, point.x() );
128131
xMax = qMax( xMax, point.x() );
@@ -135,18 +138,16 @@ void QgsMapToolFillRing::canvasMapReleaseEvent( QgsMapMouseEvent * e )
135138
bBox.setXMaximum( xMax );
136139
bBox.setYMaximum( yMax );
137140

138-
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterRect( bBox ).setFlags( QgsFeatureRequest::ExactIntersect ) );
141+
QgsFeatureIterator fit = vlayer->getFeatures( QgsFeatureRequest().setFilterFid( modifiedFid ) );
139142

140143
QgsFeature f;
141144
bool res = false;
142-
while ( fit.nextFeature( f ) )
145+
if ( fit.nextFeature( f ) )
143146
{
144147
//create QgsFeature with wkb representation
145148
QgsFeature* ft = new QgsFeature( vlayer->pendingFields(), 0 );
146149

147-
QgsGeometry *g;
148-
g = QgsGeometry::fromPolygon( QgsPolygon() << points().toVector() );
149-
ft->setGeometry( g );
150+
ft->setGeometry( QgsGeometry::fromPolygon( QgsPolygon() << pointList.toVector() ) );
150151
ft->setAttributes( f.attributes() );
151152

152153
if ( QgsApplication::keyboardModifiers() == Qt::ControlModifier )

src/core/qgsvectorlayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1065,13 +1065,13 @@ bool QgsVectorLayer::deleteSelectedFeatures( int* deletedCount )
10651065
return deleted == count;
10661066
}
10671067

1068-
int QgsVectorLayer::addRing( const QList<QgsPoint>& ring )
1068+
int QgsVectorLayer::addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId )
10691069
{
10701070
if ( !mEditBuffer || !mDataProvider )
10711071
return 6;
10721072

10731073
QgsVectorLayerEditUtils utils( this );
1074-
return utils.addRing( ring );
1074+
return utils.addRing( ring, featureId );
10751075
}
10761076

10771077
int QgsVectorLayer::addPart( const QList<QgsPoint> &points )

src/core/qgsvectorlayer.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
958958
*/
959959
bool deleteSelectedFeatures( int *deletedCount = 0 );
960960

961-
/**Adds a ring to polygon/multipolygon features
961+
/** Adds a ring to polygon/multipolygon features
962+
* @param ring ring to add
963+
* @param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
962964
@return
963965
0 in case of success,
964966
1 problem with feature type,
@@ -967,7 +969,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
967969
4 ring crosses existing rings,
968970
5 no feature found where ring can be inserted
969971
6 layer not editable */
970-
int addRing( const QList<QgsPoint>& ring );
972+
int addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId = 0 );
971973

972974
/**Adds a new part polygon to a multipart feature
973975
@return

src/core/qgsvectorlayereditutils.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool QgsVectorLayerEditUtils::deleteVertex( QgsFeatureId atFeatureId, int atVert
9797
}
9898

9999

100-
int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring )
100+
int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId )
101101
{
102102
if ( !L->hasGeometryType() )
103103
return 5;
@@ -125,7 +125,8 @@ int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring )
125125
if ( addRingReturnCode == 0 )
126126
{
127127
L->editBuffer()->changeGeometry( f.id(), f.geometry() );
128-
128+
if ( featureId )
129+
*featureId = f.id();
129130
//setModified( true, true );
130131
break;
131132
}

src/core/qgsvectorlayereditutils.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,16 @@ class CORE_EXPORT QgsVectorLayerEditUtils
4747
bool deleteVertex( QgsFeatureId atFeatureId, int atVertex );
4848

4949
/** Adds a ring to polygon/multipolygon features
50+
* @param ring ring to add
51+
* @param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
5052
@return
5153
0 in case of success,
5254
1 problem with feature type,
5355
2 ring not closed,
5456
3 ring not valid,
5557
4 ring crosses existing rings,
5658
5 no feature found where ring can be inserted*/
57-
int addRing( const QList<QgsPoint>& ring );
59+
int addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId = 0 );
5860

5961
/** Adds a new part polygon to a multipart feature
6062
@return

0 commit comments

Comments
 (0)