7

Is it possible with PyQGIS to catch "add new feature" event before commiting changes on the layer? or at least to access new features before commit.

1 Answer 1

5

The QgsVectorLayer has quite a number of signals that will may suite your needs. Documentation for 1.8 and master on these links.

Note there are minor changes to the API between 1.8 and master, and it appears that the python bindings have a few issues that are still unresolved.

This sample code defines a couple of listeners, then attaches them to a memory layer. If you start editing the layer you will see each signal triggers a line in the message log.

This code is (mostly) compatible with 1.8 and master and is intended to be pasted into the python console.

from PyQt4.QtGui import *

def logLayerModified( onlyGeometry = None ):
    QgsMessageLog.logMessage( "layer modified" )
    QApplication.beep()

def logFeatureAdded(fid):
    QgsMessageLog.logMessage( "feature added, id = " + str(fid) )
    QApplication.beep() 

def logEditingStarted():
    QgsMessageLog.logMessage( "editing started" )
    QApplication.beep() 

def logCommittedFeaturesAdded( layerId, addedFeatures ):
    message = layerId + " has features added: "
    for feature in addedFeatures:
        message += str( feature.id() ) + ", "
    QgsMessageLog.logMessage( message )
    QApplication.beep()         

layer = QgsVectorLayer( "Point", "Layer Signal Demo", "memory" )    
QgsMapLayerRegistry.instance().addMapLayers( [layer] )

layer.layerModified.connect( logLayerModified )
layer.featureAdded.connect( logFeatureAdded )
layer.editingStarted.connect( logEditingStarted )
layer.committedFeaturesAdded.connect( logCommittedFeaturesAdded )

EDIT: Even with an incomplete set of signals you can probably get the access you require by performing you own analysis.

I have a naive implementation here that will slow down for large feature counts. Depending on your needs you may be able to optimize by reducing the amount of data being handled during the cross referencing. You may get speed gains by omitting the geometry or full attribute table from the query until you have identified the features you are interested in.

Note that this is designed for 1.8, feature iteration has changed significantly for master.

from PyQt4.QtGui import *

def layerModified( onlyGeometry = None ):
    global establishedFeatureIds
    newFeatureIds = []
    layer.select()
    feature = QgsFeature()
    while layer.nextFeature( feature ):
        id = feature.id()
        if not id in  establishedFeatureIds:
            newFeatureIds.append( id )
    message = "New features: "
    for id in newFeatureIds:
        message += str(id) + ", "
    QgsMessageLog.logMessage( message )

def editingStarted():
    global establishedFeatureIds
    establishedFeatureIds = []
    layer.select()
    feature = QgsFeature()
    while layer.nextFeature( feature ):
        establishedFeatureIds.append( feature.id() )

layer = qgis.utils.iface.activeLayer()
if layer is None:
    layer = QgsVectorLayer( "Point", "Cross-reference Demo", "memory" )    
    QgsMapLayerRegistry.instance().addMapLayers( [layer] )

layer.layerModified.connect( layerModified )
layer.editingStarted.connect( editingStarted )
8
  • Hi Kelly, I forgot to mention that I'm using PostgreSQL layer and I tried to handle featureAdded signal with no luck. QObject.connect(layer, SIGNAL("featureAdded(qint64)"), self.featureAdded). However, editingStarted and editingStopped work ok. So, I'm looking for any solution either to catch featureAdded event or at least to have an access to the new features before commit.
    – AlexSV
    Commented Mar 3, 2013 at 19:03
  • Which QGIS version are you targeting? Commented Mar 4, 2013 at 1:43
  • It is 1.8, but think to switch on 1.9 since there are some bugs fixed in the latest build.
    – AlexSV
    Commented Mar 4, 2013 at 10:02
  • I did some tests and yes, featureAdded doesn't work for memory and postgres layers in 1.8 and master. However, thank you for the clue, so I can at least find new features now. Unfortunately it is not very effective with big layers, as you noted. It would be nice if it is possible to pick up all changed features from provider or elsewhere, including new, modified and deleted...
    – AlexSV
    Commented Mar 6, 2013 at 8:39
  • I'm confused now which version I should proceed with, since if I go with 1.8 it will not work with 2.0 later on. Is there any source with the latest PyQGIS API documentation on the Internet (not for 1.8)?
    – AlexSV
    Commented Mar 6, 2013 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.