Skip to content

Commit

Permalink
[API] add QgsVectorLayer::updateFeature()
Browse files Browse the repository at this point in the history
  • Loading branch information
jef-n committed Oct 7, 2011
1 parent cb7d3ed commit f39a19c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -272,10 +272,18 @@ public:
bool featureAtId(int featureId, QgsFeature& f, bool fetchGeometries = true, bool fetchAttributes = true);

/** Adds a feature
@param f feature to add
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
@return True in case of success and False in case of error
*/
bool addFeature(QgsFeature& f, bool alsoUpdateExtent = TRUE);

/** Update an existing feature
@param f feature to update
@return True in case of success and False in case of error
@note added in 1.8
*/
bool updateFeature(QgsFeature& f );


/** Insert a new vertex before the given vertex number,
Expand Down
36 changes: 36 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1947,6 +1947,42 @@ bool QgsVectorLayer::addFeature( QgsFeature& f, bool alsoUpdateExtent )
return true;
}

bool QgsVectorLayer::updateFeature( QgsFeature &f )
{
QgsFeature current;
if ( !featureAtId( f.id(), current, f.geometry(), !f.attributeMap().isEmpty() ) )
{
QgsDebugMsg( QString( "feature %1 could not be retrieved" ).arg( f.id() ) );
return false;
}

if ( f.geometry() && current.geometry() && f.geometry() != current.geometry() && !f.geometry()->isGeosEqual( *current.geometry() ) )
{
if ( !changeGeometry( f.id(), f.geometry() ) )
{
QgsDebugMsg( QString( "geometry of feature %1 could not be changed." ).arg( f.id() ) );
return false;
}
}

const QgsAttributeMap &fa = f.attributeMap();
const QgsAttributeMap &ca = current.attributeMap();

foreach( int attr, fa.keys() )
{
if ( fa.contains( attr ) && ca.contains( attr ) && fa[attr] != ca[attr] )
{
if ( !changeAttributeValue( f.id(), attr, fa[attr] ) )
{
QgsDebugMsg( QString( "attribute %1 of feature %2 could not be changed." ).arg( attr ).arg( f.id() ) );
return false;
}
}
}

return true;
}


bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -351,6 +351,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
bool addFeature( QgsFeature& f, bool alsoUpdateExtent = true );

/** Updates an existing feature
@param f feature to update
@return True in case of success and False in case of error
@note added in 1.8
*/
bool updateFeature( QgsFeature &f );

/** Insert a new vertex before the given vertex number,
* in the given ring, item (first number is index 0), and feature
Expand Down

0 comments on commit f39a19c

Please sign in to comment.