By David Crowther
When using QGIS I would like to automatically add a Unique ID value to each new record in my spatial dataset. Is this possible? And if so how?
Yes, and QGIS is the perfect place to ensure you manage your spatial data correctly. QGIS utilises a series of Widgets which ensure that when a user edits a feature or creates a new record, certain attributes are automatically updated into your spatial datasets.
In this example we can use the Text Edit Widget to automatically create a Feature/Unique ID value as we create new records into our spatial datasets. These Widgets are available within the Attributes Form (or within the FIELDS tab, pre QGIS V3) of the Layer Properties:
Within the Attributes Form, firstly select the Field that you wish to automatically create e.g. FID. If a suitable field does not exist, then ensure that you add this field to the Schema of your spatial data first.
Having chosen the FID Field, within the right hand pane choose the Widget Type to be Text Edit:
Next choose the Expression button for the Default Value. In the Expression Dialog use the following syntax to create a Unique ID - uuid()
Ok the Expression Dialog, and the Default Value of a Unique ID will be added each time a new record is created, with a preview of what that value will look like.
Apply the changes to the Layer Properties and now create a new feature. Once you have digitised the new feature the Attributes Form will auto open and the Unique ID field will be auto populated. Finish the attribute data entry and Save the changes to your data.
If you check the attributes of your source data, in this case a PostGIS Database table, we can see that the Unique ID value has been auto populated.
In an upcoming Blog I will discuss how to maximize the use of Widgets within QGIS to implement consistent Data Management practices, including;
- adding ‘date updated’ to amended records,
- auto calculating areas of reshaped polygons and more!