[GIS] How to get the feature id of features added to dataProviders

pyqgispythonqgis

My question is somehow related to the problem of finding the id of the last added feature: I am looking for a way to supply the feature id and another attribute of each added feature through a signal. The user can change many layers, but only in case of changing a special layer this signal should be emitted/evaluated.

The solution to the question mentioned above is to connect to the featureAdded signal of the layer being edited.

Instead of editing layers I am going to add features to the dataProvider. In this case the signal is not emitted through the layer.

I see three ideas to prove:

  1. Adding an appropriate signal to dataProvider objects (my preferred solution)

  2. Changing layers featureAdded signal to include feature id and the other attribute (second best solution), and fire it manually.

  3. Define an application specific signal and emit this whenever a feature is added to any provider (the easiest way, but filtering signals by this special layer is required)

In all cases one question is: how can I get the feature id of the feature being added to a dataProvider? According to the first and second idea: is it possible to extend the contents of a signal, or to bind one more signal to mapLayer/dataProvider objects?

Best Answer

If it is not absolutely necessary for your code to catch a signal...

You can use the vectorLayer.dataProvider.addFeatures() method to assign the new feature(s) to a variable. Once caught in the variable, iterate through them and call the id() function.

See my answer to a similar question.

Related Question