Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix fill ring tool creates more features than needed (fix #13354)
  • Loading branch information
nyalldawson committed Oct 8, 2015
1 parent aa4d65d commit 45a6f71
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
4 changes: 3 additions & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -653,6 +653,8 @@ class QgsVectorLayer : QgsMapLayer
bool deleteSelectedFeatures( int *deletedCount = 0 );

/** 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 @@ -661,7 +663,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::cadCanvasReleaseEvent( 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::cadCanvasReleaseEvent( 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::cadCanvasReleaseEvent( 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->fields(), 0 );

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

if ( QApplication::keyboardModifiers() == Qt::ControlModifier )
Expand Down
8 changes: 4 additions & 4 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1058,16 +1058,16 @@ 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::addRing( QgsCurveV2* ring )
int QgsVectorLayer::addRing( QgsCurveV2* ring, QgsFeatureId* featureId )
{
if ( !mEditBuffer || !mDataProvider )
{
Expand All @@ -1087,7 +1087,7 @@ int QgsVectorLayer::addRing( QgsCurveV2* ring )
}

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

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

/** 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 @@ -1170,15 +1172,17 @@ 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 ring to polygon/multipolygon features (takes ownership)
* @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
6 layer not editable*/
int addRing( QgsCurveV2* ring );
int addRing( QgsCurveV2* ring, QgsFeatureId* featureId = 0 );

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

int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring )
int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring, QgsFeatureId* featureId )
{
QgsLineStringV2* ringLine = new QgsLineStringV2();
QList< QgsPointV2 > ringPoints;
Expand All @@ -114,10 +114,10 @@ int QgsVectorLayerEditUtils::addRing( const QList<QgsPoint>& ring )
ringPoints.append( QgsPointV2( ringIt->x(), ringIt->y() ) );
}
ringLine->setPoints( ringPoints );
return addRing( ringLine );
return addRing( ringLine, featureId );
}

int QgsVectorLayerEditUtils::addRing( QgsCurveV2* ring )
int QgsVectorLayerEditUtils::addRing( QgsCurveV2* ring, QgsFeatureId* featureId )
{
if ( !L->hasGeometryType() )
{
Expand All @@ -137,6 +137,8 @@ int QgsVectorLayerEditUtils::addRing( QgsCurveV2* ring )
if ( addRingReturnCode == 0 )
{
L->editBuffer()->changeGeometry( f.id(), f.geometry() );
if ( featureId )
*featureId = f.id();

//setModified( true, true );
break;
Expand Down
8 changes: 6 additions & 2 deletions src/core/qgsvectorlayereditutils.h
Expand Up @@ -54,24 +54,28 @@ 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 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( QgsCurveV2* ring );
int addRing( QgsCurveV2* ring, QgsFeatureId* featureId = 0 );

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

0 comments on commit 45a6f71

Please sign in to comment.