11

I want to autogenerate IDs while I create new features. So every time I create a new point, its ID will increase by 1 like the following:

Feature 1: id 1
Feature 2: id 2
Feature 3: id 3

How can I attack this?

8

If you right-click your layer and go to Properties > Fields then click the Text Edit, you can set Default values (either by typing in a value or using an expression) which will appear in the attribute table automatically.

Unfortunately, using an expression like $rownum (which should get you unique ids for your features) does not work (my guess is because the feature has to be committed first?). But we can create a function which counts the number of features made and adds 1. You can access the Function Editor as shown in the image:

Function Editor

Then in the Function Editor, create a new file or edit an existing one and use the following code:

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def update(feature, parent):
    layer = qgis.utils.iface.activeLayer()
    x = layer.featureCount()
    return x + 1

Click Load then go to the Expression tab next to it and insert the expression:

update()

Then click OK and you should see a preview value:

Preview

Click OK, Apply etc and now hopefully when you create a feature, it will automatically create a unique id in your chosen field.

6

You can use the plugin "AutoFields" for Automatic attribute updates when creating or modifying vector features

3

I think you can not set default values when creating a new feature but you could try the following work around:

  • when done creating your features go to the attribute table
  • Go to the field calculator and create a new column called id with type Int
  • As value insert @row_number which will yield unique vales according to the row number

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.