Skip to content

Commit

Permalink
Fix fill ring tool creates more features than needed (fix #13354)
Browse files Browse the repository at this point in the history
(cherry-picked from 45a6f71)
  • Loading branch information
nyalldawson committed Nov 19, 2015
1 parent 4d7cb24 commit 29e2b6a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 17 deletions.
6 changes: 4 additions & 2 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -595,7 +595,9 @@ class QgsVectorLayer : QgsMapLayer
*/
bool deleteSelectedFeatures( int *deletedCount = 0 );

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

/**Adds a new part polygon to a multipart feature
@return
Expand Down
4 changes: 3 additions & 1 deletion python/core/qgsvectorlayereditutils.sip
Expand Up @@ -26,14 +26,16 @@ class QgsVectorLayerEditUtils
bool deleteVertex( QgsFeatureId atFeatureId, int atVertex );

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

/** Adds a new part polygon to a multipart feature
@return
Expand Down
15 changes: 8 additions & 7 deletions src/app/qgsmaptoolfillring.cpp
Expand Up @@ -79,7 +79,10 @@ void QgsMapToolFillRing::canvasMapReleaseEvent( QgsMapMouseEvent * e )
closePolygon();

vlayer->beginEditCommand( tr( "Ring added and filled" ) );
int addRingReturnCode = vlayer->addRing( points() );
QList< QgsPoint > pointList = points();

QgsFeatureId modifiedFid;
int addRingReturnCode = vlayer->addRing( pointList, &modifiedFid );
if ( addRingReturnCode != 0 )
{
QString errorMessage;
Expand Down Expand Up @@ -122,7 +125,7 @@ void QgsMapToolFillRing::canvasMapReleaseEvent( QgsMapMouseEvent * e )
yMin = std::numeric_limits<double>::max();
yMax = -std::numeric_limits<double>::max();

Q_FOREACH ( const QgsPoint& point, points() )
Q_FOREACH ( const QgsPoint& point, pointList )
{
xMin = qMin( xMin, point.x() );
xMax = qMax( xMax, point.x() );
Expand All @@ -135,18 +138,16 @@ void QgsMapToolFillRing::canvasMapReleaseEvent( QgsMapMouseEvent * e )
bBox.setXMaximum( xMax );
bBox.setYMaximum( yMax );

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

QgsFeature f;
bool res = false;
while ( fit.nextFeature( f ) )
if ( fit.nextFeature( f ) )
{
//create QgsFeature with wkb representation
QgsFeature* ft = new QgsFeature( vlayer->pendingFields(), 0 );

QgsGeometry *g;
g = QgsGeometry::fromPolygon( QgsPolygon() << points().toVector() );
ft->setGeometry( g );
ft->setGeometry( QgsGeometry::fromPolygon( QgsPolygon() << pointList.toVector() ) );
ft->setAttributes( f.attributes() );

if ( QgsApplication::keyboardModifiers() == Qt::ControlModifier )
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1065,13 +1065,13 @@ bool QgsVectorLayer::deleteSelectedFeatures( int* deletedCount )
return deleted == count;
}

int QgsVectorLayer::addRing( const QList<QgsPoint>& ring )
int QgsVectorLayer::addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId )
{
if ( !mEditBuffer || !mDataProvider )
return 6;

QgsVectorLayerEditUtils utils( this );
return utils.addRing( ring );
return utils.addRing( ring, featureId );
}

int QgsVectorLayer::addPart( const QList<QgsPoint> &points )
Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayer.h
Expand Up @@ -958,7 +958,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
bool deleteSelectedFeatures( int *deletedCount = 0 );

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

/**Adds a new part polygon to a multipart feature
@return
Expand Down
5 changes: 3 additions & 2 deletions src/core/qgsvectorlayereditutils.cpp
Expand Up @@ -97,7 +97,7 @@ bool QgsVectorLayerEditUtils::deleteVertex( QgsFeatureId atFeatureId, int atVert
}


int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring )
int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId )
{
if ( !L->hasGeometryType() )
return 5;
Expand Down Expand Up @@ -125,7 +125,8 @@ int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring )
if ( addRingReturnCode == 0 )
{
L->editBuffer()->changeGeometry( f.id(), f.geometry() );

if ( featureId )
*featureId = f.id();
//setModified( true, true );
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/core/qgsvectorlayereditutils.h
Expand Up @@ -47,14 +47,16 @@ class CORE_EXPORT QgsVectorLayerEditUtils
bool deleteVertex( QgsFeatureId atFeatureId, int atVertex );

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

/** Adds a new part polygon to a multipart feature
@return
Expand Down

0 comments on commit 29e2b6a

Please sign in to comment.