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?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
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:
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:
Then click OK and you should see a preview value:
Click OK, Apply etc and now hopefully when you create a feature, it will automatically create a unique id in your chosen field.
You can use the plugin "AutoFields" for Automatic attribute updates when creating or modifying vector features
I think you can not set default values when creating a new feature but you could try the following work around:
idwith type Int
@row_numberwhich will yield unique vales according to the row number