Tuesday, 18 July 2017

pyqgis - What is the proper way to do an automatic rollback of a vector layer edit?


I'm trying to rollback automatically the changes in features of a in-memory vector layer that don't meet certain criteria.


The code I'm using for testing looks like this:


...

def some_function()
tmpLayer = QgsVectorLayer("LineString", "Aristas_tmp", "memory")
tmpLayer.geometryChanged.connect(self.onLineGeometryChange)

return

...

@pyqtSlot(int, QgsGeometry)
def onLineGeometryChange(self, featureId, geom ):
capa_aristas = find_layer("Aristas_tmp")
# Disconects the signal to avoid entering an endless loop
capa_aristas.geometryChanged.disconnect(self.onLineGeometryChange)
capa_aristas.rollBack()

capa_aristas.geometryChanged.connect(self.onLineGeometryChange)
return

(find_layer is a function taken from MMQGIS plugin that iterates through the QgsMapLayerRegistry)


This code has the effect of undoing any change made to the layer, but with some problems:



  1. There are inconsitencies in the GUI, i.e.: the layer leaves edit mode (the pencil in the legend disappears and further edits are not allowed), but the buttons in the digitize toolbar are still enabled.

  2. If enter edit mode again, the last edited feature appears in the destination location of the previous edit, but if I move it again, the rollback moves it to the right place (the initial position)

  3. If I repeat the process 5 or 6 times, QGIS crashes with this message: Fatal: ASSERT: "mChangedGeometries.isEmpty()" in file /tmp/buildd/qgis-1.9.0+git20121010+dd67935~squeeze1/src/core/qgsvectorlayer.cpp, line 4042



(as you can see, I'm running QGIS 1.9.0-master)


I guess I hit some kind of bug, but want to be sure that I'm calling the API in a proper way.


Anyone can confirm if this usage of rollback() is "legal"?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...