Is there a way of auto_increment for the ID column in QGIS
Using the field calculator is the way to go:
No ID was given in
- Digitize every features without any entering any Id.
- Before export, update unique Ids with the expression, '$Id' using the field calculator.
Some ID's already given in
- If you have already ID's you can use '-$Id'. Make sure you just select new Features what means that that are 'NULL' in the id row. Simply do that by ordering the column.
- Now do the steps from the pictures:
Hallelujah! Or Eureka. Or whatever. This can be done. With a shapefile.
- If there isn't already one, add a field to contain the feature id, say "FID", of type Whole number (integer).
- Open Layer Properties (right-click on the layer and choose Properties... or double-click the layer), click on the Attributes Form tab, then under General uncheck Editable and under Defaults in the field Default value type
maximum("FID") + 1
.
By unchecking Editable, you can't enter another value or delete what's there. Note that if there are values without an ID, these values won't be updated. At some point I'll experiment with checking Apply default value on update and revising my formula to check for a zero or NULL value to update only those records when they are edited, not any record with a value greater than 1. (Earlier in this post it was discussed how to update the FID field with unique values, which you will need to do if you added the field after there were already features in the shapefile.)
Note that this is saved with the current map file, not the shapefile, so adding that shapefile multiple times will require you to copy that part of the layer style to the newly added layer. To do this, right-click on the layer, choose Styles > Copy Style > Fields, and right-click on another layer, choose Styles > Paste Style > All Style Categories (or continue to Fields). You can also copy that part of the style to any other layer based on a shapefile, but the ID field must have the same name as the layer you're copying from.
I would like to add to vinayan's post and briefly mention the rownum function, as it is very similar and in some cases might be a little more convenient.
id returns the Feature ID, meaning that it always starts at zero.
rownum returns the number of the row, meaning that it starts at one.
So, basically, if you want the auto-increment to start at 0 go for $id, and if you want it to start at 1 then go for $rownum.