Bug report #4363

Problem with setGeometry() when overwrite geometry

Added by sebastien rey about 9 years ago. Updated about 9 years ago.

Status:Closed
Priority:High
Assignee:-
Category:-
Affected QGIS version: Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data: Copied to github as #:14296

Description

Objective is simple, i want to change the geometry of point 1 by copying the geometry of point 2 into geometry of point1.

I create a simple point layer, with two points.
I select one of this point, and I run this source code into python console :

--

mapC = qgis.utils.iface.mapCanvas()
layer = mapC.currentLayer()
provider = layer.dataProvider()
feat = QgsFeature()
provider.nextFeature(feat)

myGeom = feat.geometry()

while provider.nextFeature(feat):
layer.startEditing()
feat.setGeometry(myGeom) #replace the feature geometry by the geometry of mygeom
layer.commitChanges()

--

Qgis crash after that, but i don't understand why ??
It seems the setGeometry() work when feat is empty, but setGeometry() crash when i try to overwrite the geometry.

Best Regards,
Sr.

History

#1 Updated by Jürgen Fischer about 9 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

Overwrite feat with a new feature invalidate the geometry reference. You need to copy the geometry. But the retrieved feature is not linked to the layer anymore and changes to it are not automatically written back to the layer. You need QgsVectorLayer.changeGeometry(fid, geom) for that.

Like this:

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):
  layer.changeGeometry(feat.id(), myGeom)

layer.commitChanges()

Also available in: Atom PDF