4

I have the following script that sets the a feature id automatically after a new feature is digitized.

I added the function inside the project's Macro section inside openProject() function:

enter image description here

The function is being triggered ok every time I open the project. My problem now is that every new feature I create gets the same id (9707).

It seems like max varibles' value is not being updated after a new feature is added and without "Toggle editing" or "Saving layer edits".

Current code:

def openProject():
    from qgis.core import QgsMapLayerRegistry
    import os
    # Set active layer
    #layer = qgis.utils.iface.activeLayer()
    layer=None
    for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
        if lyr.name() == "Pozos":
            layer = lyr
            break
    # Define function to select added feature and add attribute to field "Name"
    def update(featureAdded):
        idx = layer.fieldNameIndex('id')
        layer.changeAttributeValue(featureAdded, idx, layer.maximumValue(idx) +1)
    # Connect "featureAdded" event to "select" function
    layer.featureAdded.connect(update)

def saveProject():
    pass

def closeProject():
    pass
  • Just a guess but try replacing max = layer.maximumValue(idx) + 1 with max = int(layer.maximumValue(idx)) + 1. Also, you should post the actual code into the question =) – Joseph May 24 '16 at 13:32
  • 1
    @Joseph edited the OP with the code – Egidi May 24 '16 at 13:49
  • Have you tried to work with the data provider, as in layer.dataProvider().changeAttributeValues()? – BritishSteel May 24 '16 at 13:55
  • @Joseph tried your solution suggestion and didn't work. Same result, all new points get the same id. – Egidi May 25 '16 at 7:11
  • @BritishSteel Thanks for your suggestion, I am trying your solution, but It is my second day using pyQGIS and I am still a bit lost. What arguments would I need to add in changeAttributeValues? "layer.dataProvider().changeAttributeValues(featureAdded, idx, max)" is not working – Egidi May 25 '16 at 8:09
3

After some search I finally found a working solution: As I saw that layer.maximumValue(idx) always kept the same value until I commited changes, I added the number of new elements and that are pending to be commited to this variable. This way I always get serial (autonumeric) number. Idea taken from here:

def openProject():
    from qgis.core import QgsMapLayerRegistry
    import os
    # Set active layer
    #layer = qgis.utils.iface.activeLayer()
    layer=None
    for lyr in QgsMapLayerRegistry.instance().mapLayers().values():
        if lyr.name() == "Pozos":
            layer = lyr
            break
    # Define function to select added feature and add attribute to field "Name"
    def update(featureAdded):
        idx = layer.fieldNameIndex('id')
    if layer.editBuffer():
        layer.changeAttributeValue(featureAdded, idx, layer.maximumValue(idx) + len( lyr.editBuffer().addedFeatures() ))
    # Connect "featureAdded" event to "select" function
    layer.featureAdded.connect(update)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.