I'm using the graphic modeler and 'Add unique value index field' algorithm to assign IDs to unique records in a vector layer which works great, but then I want to add a 2nd field that will autoincrement for each unique value created. I can use the 'Add autoincremental field' algorithm, but then I get 1 to n for my IDs, where I'd like it to re-start for each unique id.

Example: rather than 10-0, 10-1, 10-2, 11-3, 11-4 I'd like to see 10-0, 10-1, 10-2, 11-0, 11-1.

Bonus: If there's a way to start the autoincremental number at 10 instead of 0, that would be even better!


You could create a custom script which increments the value index you created and saves these in a new field. You can create one from:

Processing Toolbox > Scripts > Tools > Create new script

Then use something like the following:

##Field_to_group_increment=field Layer
##New_index_field=string index
##Output=output vector

from qgis.core import QgsField, QgsVectorFileWriter, edit, QgsVectorLayer
from PyQt4.QtCore import QVariant

layer = processing.getObject(Layer)
writer = QgsVectorFileWriter.writeAsVectorFormat(layer, Output, "utf-8", None, "ESRI Shapefile")
output = QgsVectorLayer(Output, 'any_name', 'ogr')

idx = output.fieldNameIndex(Field_to_group_increment)
values = output.uniqueValues(idx)

with edit(output):
    output.addAttribute(QgsField(New_index_field, QVariant.Int))
    for feat in values:
        x = 10
        for feature in output.getFeatures():
            if feat == feature[Field_to_group_increment]:
                feature[New_index_field] = x
                x = x + 1

del writer

Make sure to save the script into your /.qgis2/processing/scripts directory. You can then run the script from the Processing Toolbox or include it in your model.


The interface when you run the script:


The result:


  • 1
    Does it work to put the meve the addAttribute 1. to work directly on the layer and 2. into the with edit(layer) block? The point of with edit(layer):` was to bring devs away from using dataprovider directly. – Matthias Kuhn Dec 1 '17 at 12:28
  • @MatthiasKuhn - Sorry but I think the Markdown format of your comment has confused me. Could you clarify what you meant please? Do you mean directly adding the attribute to the input layer instead of writing a copy and avoid using with edit()? – Joseph Dec 1 '17 at 12:40
  • I meant not to access the dataprovider directly at all. – Matthias Kuhn Dec 1 '17 at 12:51
  • 1
    This was exactly what I was looking for! and thank you very much for the clear and specific examples. Perfect solution - much appreciated! – CEL-ma Dec 1 '17 at 14:18
  • @CEL-ma - Most welcome, glad it helped :) – Joseph Dec 1 '17 at 14:22

If you download the next nightly of the QGIS 3.0 pre-release, it's now supported by the "Add autoincremental field" algorithm:

enter image description here

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.