I am working on a plugin for QGIS (2.12). It uses data stored in an Spatialite database. When the user creates a new feature, the plugin should:

  • update the feature attribute table (the popup form is disabled)
  • update another (non-geometry) table in the database
  • if the polygon overlaps any existing polygons:
    • update the polygon geometry using the difference method

The modifications do not need any user input, and can happen in 'real-time' or following a commitChanges(), but the user should remain in (or be returned to) an editing session.

What is the best strategy for this? Should I use the provider, the vector layer functions, the iface.vectorLayerTools?

So far, I have not had much success. I have a class that is created on the layer.editingStarted() signal that listens for changes to geometry. It includes the following:

def __init__(self, iface, vlayer, db):
    self.vlayer = vlayer
    self.edit_buffer = vlayer.editBuffer()

def connect_signals

I first tried to use the edit buffer's featureAdded() signal e.g.

def feature_added(self, fid):
    self.vlayer.beginEditCommand('Update attributes')
    self.vlayer.changeAttributeValue(fid, 1, 'new_value')

Using the temporary fids given to the pre-commit features prevented making the changes to the other tables, which needed to reference them. Also, this method caused QGIS to crash/segmentation fault if the Undo command was used (on Linux, and with Undo, Redo on Windows).

Is this a bug, or am I just approaching this in the wrong way?

I have also tried using the commitedFeaturesAdded() signal. This solved the undo/redo issues and allowed me to update the attribute table directly in the database via SQL commands, after the feature was created with the final fid. However, now when I try to change the geometry and check for intersection/overlap again I get stuck in a loop of newly committed features.

Finally, I tried calculating the modified geometry, exporting as Wkt and using GeomFromText to update the vlayer table directly. In this case, the SQL is run without error, but the geometry doesn't seem to be updated.


I have found a way to do this. Changes to the feature geometry (e.g. removing overlaps) and attributes are made to committed new features, only after the committedFeaturesAdded signal has fired. Signals from the EditBuffer can be used to trigger messages but not database changes.

Where modifications fail, it is easier to delete the user feature and add a new one with the modified values.

I have posted code below that form a working example.

"""minimum worked example for changing attributes and geometries.  
to test, set appropriate database and layer names then save as
feature_modifier.py.  You can then type
`import feature_modifier as fm` into console, then run
`fm = feature_modifier.FeatureModifier()`.  
Begin editing and watch signals fire."""

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *

class FeatureModifier(object):
    """Modifies features automatically when user edits are saved."""
    def __init__(self, iface):
        self.iface = iface

    def load_layer(self):
        uri = QgsDataSourceURI()
        db_path = '/path/to/database.sqlite'
        uri.setDataSource('', 'mylayer', 'geometry')
        vlayer = QgsVectorLayer(uri.uri(), 'mylayer', 'spatialite')
                      QgsCoordinateReferenceSystem.EpsgCrsId), False)
        self.vlayer = vlayer

    def connect_signals(self):

    def editing_started(self):
        print('Editing started')
        # Disable attributes dialog
            '/qgis/digitizing/disable_enter_attribute_values_dialog', True)
        self.edit_handler = EditHandler(self.vlayer)

    def editing_stopped(self):
        print('Editing stopped')
        self.edit_handler = None
        # Re-enable attributes dialog
            '/qgis/digitizing/disable_enter_attribute_values_dialog', False)
        if self.vlayer.isEditable() is True:
            # Rolling back changes ends destroys geometry_handler class but
            # layer remains editable.  In this case, recreate it.

    def clean_up(self):

class EditHandler(object):
    Listens for committed feature additions and changes feature attributes.
    def __init__(self, vlayer):
        self.vlayer = vlayer

    def connect_committed_signals(self):
        Connect signals for editing events

    def disconnect_committed_signals(self):
        Disconnect signals for editing events

    def committed_adds(self, layer_id, added_features):
        print('Committed features added to layer {}:'.format(layer_id))
        for feature in added_features:

    def committed_changes(self, layer_id, changed_geometries):
        print('Committed geometries changed in layer {}:'.format(layer_id))
        for fid in changed_geometries.keys():
            self.update_geometry(fid, changed_geometries[fid])

    def committed_deletes(self, layer_id, deleted_fids):
        print('Committed features deleted from layer {}:'.format(layer_id))
        for fid in deleted_fids:

    def replace_feature(self, fid):
        Replace feature with customised geometry and attributes.
        print('Replacing feature {} here.'.format(fid))
        feature = self.vlayer.getFeatures(
        geometry = feature.geometry()

        # Create new feature
        new_feature = QgsFeature(self.vlayer.pendingFields())
        geometry.translate(0, 50)  # Modify original geometry
        new_feature.setAttribute('symbol', 10)  # Customise attributes

        # Update layer by removing old and adding new
        result = self.vlayer.dataProvider().deleteFeatures([fid])
        result, new_features = self.vlayer.dataProvider().addFeatures(
        for f in new_features:
            print('Replacement feature {} added'.format(f.id()))

    def update_geometry(self, fid, geometry):
        Update the geometry of a feature, e.g. jump 100 m east.
        geometry.translate(100, 0)
        self.vlayer.dataProvider().changeGeometryValues({fid: geometry})
  • +1 I'm toal newbie in python, but tested the example and it is that what I was searching for. Thank you for sharing your experiences. Only thing is I have to call 'fm.FeatureModifier(iface)' but everything else worked fine! – Ms. Smith Jun 2 '16 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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