Skip to content

Instantly share code, notes, and snippets.

@NathanW2
Created October 7, 2011 11:32
Show Gist options
  • Save NathanW2/1270106 to your computer and use it in GitHub Desktop.
Save NathanW2/1270106 to your computer and use it in GitHub Desktop.
mapC = qgis.utils.iface.mapCanvas()
layer = mapC.currentLayer()
provider = layer.dataProvider()
feat = QgsFeature()
provider.nextFeature(feat)
myGeom = QgsGeometry( feat.geometry() )
layer.startEditing()
while provider.nextFeature(feat):
feat.setGeom(myGeom)
layer.updateFeature(feat)
layer.commitChanges()
@jef-n
Copy link

jef-n commented Oct 7, 2011

diff --git a/python/core/qgsvectorlayer.sip b/python/core/qgsvectorlayer.sip
index 98eee7e..28b94ff 100644
--- a/python/core/qgsvectorlayer.sip
+++ b/python/core/qgsvectorlayer.sip
@@ -272,10 +272,17 @@ 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
    
  • */
  • bool updateFeature(QgsFeature& f );

/** Insert a new vertex before the given vertex number,
diff --git a/src/core/qgsvectorlayer.cpp b/src/core/qgsvectorlayer.cpp
index 42683c5..bc8fb74 100644
--- a/src/core/qgsvectorlayer.cpp
+++ b/src/core/qgsvectorlayer.cpp
@@ -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 )
{
diff --git a/src/core/qgsvectorlayer.h b/src/core/qgsvectorlayer.h
index 0db953b..4fc740c 100644
--- a/src/core/qgsvectorlayer.h
+++ b/src/core/qgsvectorlayer.h
@@ -351,6 +351,11 @@ 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
    
  • */
    
  • 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment