[Qgis-developer] featureAdded(QgsFeatureId) signal

Denis Rouzaud denis.rouzaud at gmail.com
Fri Nov 29 00:01:55 PST 2013


Hi Paolo,

In your code you update the geometry in the provider.
You should not do this since the feature is not there yet, it's still in 
the edit session (edit buffer).

There is two ways to do this: either work totally in the buffer or 
totally in the provider.
There is an agreement that working in the provider in plugins should be 
avoided. Moreover, if I understand correctly your workflow working with 
the buffer seems more appropriate.

Then, you should use
layer.changeGeometry(fid, geom) instead of 
layer.dataProvider().changeGeometries(...)

Hope it helps a bit,

Cheers,

Denis

On 28. 11. 13 23:24, Paolo Corti wrote:
> Hi devs
>
> Within a plugin, I need to make some kind of geometric topological
> manipulation on every feature as soon as it is added to the layer by
> the user.
>
> My best guess was to use the featureAdded(QgsFeatureId) signal and to
> modify the geometry within it.
> Unluckily it looks like the geometry is effectively updated only when
> the edits are saved by the user. In that case the featureAdded is
> emitted again and it works correctly.
>
> Workflow:
> * the user add a feature within an edit session
> * the featureAdded signal is emitted a first time, the qgsfId is
> negative, the changeGeometryValues results to True but nothing happens
> * the user save the edits
> * the featureAdded signal is emitted a second time, the qgsfId is now
> positive, the feature having been stored, changeGeometryValues results
> to True and the geometry is effectively updated
>
> Is there a way to implement what I need (having the feature's geometry
> modified just the first time the signal is emitted)?
>
> Here is the stub code I am using:
>
> QObject.connect(self.mylayer, SIGNAL("featureAdded(QgsFeatureId)"),
> self.featureAdded)
>
> def featureAdded(self, qgsfId):
>          request=QgsFeatureRequest()
>          request.setFilterFid(qgsfId)
>          feat = self.mylayer.getFeatures(request).next()
>          .... (some more code here)
>          self.mylayer.beginEditCommand("Feature manipulation")
>          self.mylayer.dataProvider().changeGeometryValues({ feat.id() :
> newgeom })
>          self.mylayer.endEditCommand()
>          self.canvas.refresh()
>
> thanks in advance
> p
>



More information about the Qgis-developer mailing list