Generating attributes in an automatic way with QGIS?
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:
update()
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:
- 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