1

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!

3 Answers 3

3

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:

##Example=name
##Layer=vector
##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
                output.updateFeature(feature)
                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.


Example:

The interface when you run the script:

Interface

The result:

Example

5
  • 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. Commented Dec 1, 2017 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
    Commented Dec 1, 2017 at 12:40
  • I meant not to access the dataprovider directly at all. Commented Dec 1, 2017 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
    Commented Dec 1, 2017 at 14:18
  • @CEL-ma - Most welcome, glad it helped :)
    – Joseph
    Commented Dec 1, 2017 at 14:22
0

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

0

With QGIS 3.x you can use the aggregate expression function "minimum" in a virtual field to autoincrement your numbers:

$id - minimum( $id,group_by:="group_field") + 1

If you want to start autoincremental number at 10 you have to replace "+ 1" with "+ 10".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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