Bug report #4363
Problem with setGeometry() when overwrite geometry
|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|
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()
myGeom = feat.geometry()
feat.setGeometry(myGeom) #replace the feature geometry by the geometry of mygeom
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.
#1 Updated by Jürgen Fischer about 9 years ago
- Resolution set to invalid
- Status changed from Open to Closed
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.
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()