Skip to content

Commit 4925a54

Browse files
committedOct 9, 2015
Followup e5ea5ff, better API
1 parent c62d81a commit 4925a54

File tree

4 files changed

+63
-45
lines changed

4 files changed

+63
-45
lines changed
 

‎python/core/qgsvectorlayereditutils.sip

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ class QgsVectorLayerEditUtils
2727

2828
/** Adds a ring to polygon/multipolygon features
2929
* @param ring ring to add
30-
* @param modifiedFeatureId if specified, feature ID for feature ring was added to will be stored in this parameter
31-
* @param preferredFeatureIds if specified, the features will be the first candidates for adding a ring. Otherwise
30+
* @param targetFeatureIds if specified, only these features will be the candidates for adding a ring. Otherwise
3231
* all intersecting features are tested and the ring is added to the first valid feature.
32+
* @param modifiedFeatureId if specified, feature ID for feature that ring was added to will be stored in this parameter
3333
@return
3434
0 in case of success,
3535
1 problem with feature type,
3636
2 ring not closed,
3737
3 ring not valid,
3838
4 ring crosses existing rings,
3939
5 no feature found where ring can be inserted*/
40-
int addRing( const QList<QgsPoint>& ring, QgsFeatureId* modifiedFeatureId = 0 );
40+
int addRing( const QList<QgsPoint>& ring, const QgsFeatureIds& targetFeatureIds = QgsFeatureIds(), QgsFeatureId* modifiedFeatureId = 0 );
4141

4242
/** Adds a new part polygon to a multipart feature
4343
@return

‎src/core/qgsvectorlayer.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,21 @@ int QgsVectorLayer::addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureI
10641064
return 6;
10651065

10661066
QgsVectorLayerEditUtils utils( this );
1067-
return utils.addRing( ring, featureId, mSelectedFeatureIds );
1067+
int result = 5;
1068+
1069+
//first try with selected features
1070+
if ( !mSelectedFeatureIds.isEmpty() )
1071+
{
1072+
result = utils.addRing( ring, mSelectedFeatureIds, featureId );
1073+
}
1074+
1075+
if ( result != 0 )
1076+
{
1077+
//try with all intersecting features
1078+
result = utils.addRing( ring, QgsFeatureIds(), featureId );
1079+
}
1080+
1081+
return result;
10681082
}
10691083

10701084
int QgsVectorLayer::addRing( QgsCurveV2* ring, QgsFeatureId* featureId )
@@ -1087,7 +1101,22 @@ int QgsVectorLayer::addRing( QgsCurveV2* ring, QgsFeatureId* featureId )
10871101
}
10881102

10891103
QgsVectorLayerEditUtils utils( this );
1090-
return utils.addRing( ring, featureId, mSelectedFeatureIds );
1104+
int result = 5;
1105+
1106+
//first try with selected features
1107+
if ( !mSelectedFeatureIds.isEmpty() )
1108+
{
1109+
result = utils.addRing( static_cast< QgsCurveV2* >( ring->clone() ), mSelectedFeatureIds, featureId );
1110+
}
1111+
1112+
if ( result != 0 )
1113+
{
1114+
//try with all intersecting features
1115+
result = utils.addRing( static_cast< QgsCurveV2* >( ring->clone() ), QgsFeatureIds(), featureId );
1116+
}
1117+
1118+
delete ring;
1119+
return result;
10911120
}
10921121

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

‎src/core/qgsvectorlayereditutils.cpp

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool QgsVectorLayerEditUtils::deleteVertex( QgsFeatureId atFeatureId, int atVert
104104
return true;
105105
}
106106

107-
int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring, QgsFeatureId* modifiedFeatureId, const QgsFeatureIds& preferredFeatureIds )
107+
int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring, const QgsFeatureIds& targetFeatureIds, QgsFeatureId* modifiedFeatureId )
108108
{
109109
QgsLineStringV2* ringLine = new QgsLineStringV2();
110110
QList< QgsPointV2 > ringPoints;
@@ -114,10 +114,10 @@ int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring, QgsFeatureId*
114114
ringPoints.append( QgsPointV2( ringIt->x(), ringIt->y() ) );
115115
}
116116
ringLine->setPoints( ringPoints );
117-
return addRing( ringLine, modifiedFeatureId, preferredFeatureIds );
117+
return addRing( ringLine, targetFeatureIds, modifiedFeatureId );
118118
}
119119

120-
int QgsVectorLayerEditUtils::addRing( QgsCurveV2* ring, QgsFeatureId* modifiedFeatureId, const QgsFeatureIds& preferredFeatureIds )
120+
int QgsVectorLayerEditUtils::addRing( QgsCurveV2* ring, const QgsFeatureIds& targetFeatureIds, QgsFeatureId* modifiedFeatureId )
121121
{
122122
if ( !L->hasGeometryType() )
123123
{
@@ -128,43 +128,32 @@ int QgsVectorLayerEditUtils::addRing( QgsCurveV2* ring, QgsFeatureId* modifiedFe
128128
int addRingReturnCode = 5; //default: return code for 'ring not inserted'
129129
QgsFeature f;
130130

131-
//see if part can be added to preferred features
132-
if ( !preferredFeatureIds.isEmpty() )
131+
QgsFeatureIterator fit;
132+
if ( !targetFeatureIds.isEmpty() )
133133
{
134-
QgsFeatureIterator fit = L->getFeatures( QgsFeatureRequest().setFilterFids( preferredFeatureIds ) );
135-
while ( fit.nextFeature( f ) )
136-
{
137-
//add ring takes ownership of ring, and deletes it if there's an error
138-
addRingReturnCode = f.geometry()->addRing( static_cast< QgsCurveV2* >( ring->clone() ) );
139-
if ( addRingReturnCode == 0 )
140-
{
141-
L->editBuffer()->changeGeometry( f.id(), f.geometry() );
142-
if ( modifiedFeatureId )
143-
*modifiedFeatureId = f.id();
144-
145-
break;
146-
}
147-
}
134+
//check only specified features
135+
fit = L->getFeatures( QgsFeatureRequest().setFilterFids( targetFeatureIds ) );
148136
}
149-
150-
//no match so far, so check other intersecting features
151-
if ( addRingReturnCode != 0 )
137+
else
152138
{
139+
//check all intersecting features
153140
QgsRectangle bBox = ring->boundingBox();
154-
QgsFeatureIterator fit = L->getFeatures( QgsFeatureRequest().setFilterRect( bBox ).setFlags( QgsFeatureRequest::ExactIntersect ) );
155-
while ( fit.nextFeature( f ) )
141+
fit = L->getFeatures( QgsFeatureRequest().setFilterRect( bBox ).setFlags( QgsFeatureRequest::ExactIntersect ) );
142+
}
143+
144+
//find first valid feature we can add the ring to
145+
while ( fit.nextFeature( f ) )
146+
{
147+
//add ring takes ownership of ring, and deletes it if there's an error
148+
addRingReturnCode = f.geometry()->addRing( static_cast< QgsCurveV2* >( ring->clone() ) );
149+
if ( addRingReturnCode == 0 )
156150
{
157-
//add ring takes ownership of ring, and deletes it if there's an error
158-
addRingReturnCode = f.geometry()->addRing( static_cast< QgsCurveV2* >( ring->clone() ) );
159-
if ( addRingReturnCode == 0 )
160-
{
161-
L->editBuffer()->changeGeometry( f.id(), f.geometry() );
162-
if ( modifiedFeatureId )
163-
*modifiedFeatureId = f.id();
151+
L->editBuffer()->changeGeometry( f.id(), f.geometry() );
152+
if ( modifiedFeatureId )
153+
*modifiedFeatureId = f.id();
164154

165-
//setModified( true, true );
166-
break;
167-
}
155+
//setModified( true, true );
156+
break;
168157
}
169158
}
170159

‎src/core/qgsvectorlayereditutils.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,31 @@ class CORE_EXPORT QgsVectorLayerEditUtils
5555

5656
/** Adds a ring to polygon/multipolygon features
5757
* @param ring ring to add
58-
* @param featureId if specified, feature ID for feature ring was added to will be stored in this parameter
59-
* @param preferredFeatureIds if specified, the features will be the first candidates for adding a ring. Otherwise
58+
* @param targetFeatureIds if specified, only these features will be the candidates for adding a ring. Otherwise
6059
* all intersecting features are tested and the ring is added to the first valid feature.
60+
* @param modifiedFeatureId if specified, feature ID for feature that ring was added to will be stored in this parameter
6161
@return
6262
0 in case of success,
6363
1 problem with feature type,
6464
2 ring not closed,
6565
3 ring not valid,
6666
4 ring crosses existing rings,
6767
5 no feature found where ring can be inserted*/
68-
int addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId = 0, const QgsFeatureIds& preferredFeatureIds = QgsFeatureIds() );
68+
int addRing( const QList<QgsPoint>& ring, const QgsFeatureIds& targetFeatureIds = QgsFeatureIds(), QgsFeatureId* modifiedFeatureId = 0 );
6969

7070
/** Adds a ring to polygon/multipolygon features
7171
* @param ring ring to add
72-
* @param modifiedFeatureId if specified, feature ID for feature ring was added to will be stored in this parameter
73-
* @param preferredFeatureIds if specified, the features will be the first candidates for adding a ring. Otherwise
72+
* @param targetFeatureIds if specified, only these features will be the candidates for adding a ring. Otherwise
7473
* all intersecting features are tested and the ring is added to the first valid feature.
74+
* @param modifiedFeatureId if specified, feature ID for feature that ring was added to will be stored in this parameter
7575
@return
7676
0 in case of success,
7777
1 problem with feature type,
7878
2 ring not closed,
7979
3 ring not valid,
8080
4 ring crosses existing rings,
8181
5 no feature found where ring can be inserted*/
82-
int addRing( QgsCurveV2* ring, QgsFeatureId* modifiedFeatureId = 0, const QgsFeatureIds& preferredFeatureIds = QgsFeatureIds() );
82+
int addRing( QgsCurveV2* ring, const QgsFeatureIds& preferredFeatureIds = QgsFeatureIds(), QgsFeatureId* modifiedFeatureId = 0 );
8383

8484
/** Adds a new part polygon to a multipart feature
8585
@return

0 commit comments

Comments
 (0)
Please sign in to comment.