Skip to content

Commit f39a19c

Browse files
committedOct 7, 2011
[API] add QgsVectorLayer::updateFeature()
1 parent cb7d3ed commit f39a19c

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,18 @@ public:
272272
bool featureAtId(int featureId, QgsFeature& f, bool fetchGeometries = true, bool fetchAttributes = true);
273273

274274
/** Adds a feature
275+
@param f feature to add
275276
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
276277
@return True in case of success and False in case of error
277278
*/
278279
bool addFeature(QgsFeature& f, bool alsoUpdateExtent = TRUE);
280+
281+
/** Update an existing feature
282+
@param f feature to update
283+
@return True in case of success and False in case of error
284+
@note added in 1.8
285+
*/
286+
bool updateFeature(QgsFeature& f );
279287

280288

281289
/** Insert a new vertex before the given vertex number,

‎src/core/qgsvectorlayer.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,42 @@ bool QgsVectorLayer::addFeature( QgsFeature& f, bool alsoUpdateExtent )
19471947
return true;
19481948
}
19491949

1950+
bool QgsVectorLayer::updateFeature( QgsFeature &f )
1951+
{
1952+
QgsFeature current;
1953+
if ( !featureAtId( f.id(), current, f.geometry(), !f.attributeMap().isEmpty() ) )
1954+
{
1955+
QgsDebugMsg( QString( "feature %1 could not be retrieved" ).arg( f.id() ) );
1956+
return false;
1957+
}
1958+
1959+
if ( f.geometry() && current.geometry() && f.geometry() != current.geometry() && !f.geometry()->isGeosEqual( *current.geometry() ) )
1960+
{
1961+
if ( !changeGeometry( f.id(), f.geometry() ) )
1962+
{
1963+
QgsDebugMsg( QString( "geometry of feature %1 could not be changed." ).arg( f.id() ) );
1964+
return false;
1965+
}
1966+
}
1967+
1968+
const QgsAttributeMap &fa = f.attributeMap();
1969+
const QgsAttributeMap &ca = current.attributeMap();
1970+
1971+
foreach( int attr, fa.keys() )
1972+
{
1973+
if ( fa.contains( attr ) && ca.contains( attr ) && fa[attr] != ca[attr] )
1974+
{
1975+
if ( !changeAttributeValue( f.id(), attr, fa[attr] ) )
1976+
{
1977+
QgsDebugMsg( QString( "attribute %1 of feature %2 could not be changed." ).arg( attr ).arg( f.id() ) );
1978+
return false;
1979+
}
1980+
}
1981+
}
1982+
1983+
return true;
1984+
}
1985+
19501986

19511987
bool QgsVectorLayer::insertVertex( double x, double y, QgsFeatureId atFeatureId, int beforeVertex )
19521988
{

‎src/core/qgsvectorlayer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
351351
*/
352352
bool addFeature( QgsFeature& f, bool alsoUpdateExtent = true );
353353

354+
/** Updates an existing feature
355+
@param f feature to update
356+
@return True in case of success and False in case of error
357+
@note added in 1.8
358+
*/
359+
bool updateFeature( QgsFeature &f );
354360

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

0 commit comments

Comments
 (0)
Please sign in to comment.